I am trying to do some text file manipulation in Linux.
I have a file, called names.txt that looks like this:
A1
X12
B4
Y5
C10
Z23
B8
C3
Z6
And I need it to look like this:
A01
B04
B08
C03
C10
X12
Y05
Z06
Z23
GOAL: I need to zero-pad the single digits and then alphabetize the results, and save to file sorted_names.txt
I'm thinking I need to count the number of characters per line first, and if the number of characters is less than 2, then add a zero. Lastly I would need to sort alphabetically.
For starters, I think I do this to count the number of characters per line:
cat names.txt | while read line
do
count=$(echo $line | wc -c)
echo $line $count
done
Then my thought was to loop through count:
for COUNT in $count
if [( $COUNT = "3" )];
then
echo doZeroPadHere
fi
Is it important to you to do it using only built-in Bash features? Because it seems easier to use sed
and sort
:
<names.txt sed 's/^\([A-Z]\)\([0-9]\)$/\10\2/' | sort >sorted_names.txt