awk Help - Beginner

Hi,

I think I need to use AWK - however I have no experience of it. Can someone help please?

I have a file like this but with many more records - it is fixed width

THIS15021X            799999 XX           00000099999 00008888888 XX 15022013
THISQ15021X      999999 XX           00000099999 00008888888 XX 15022013
BLAS15021X          9999999XX        00000199999 00008888888 XX 15022013

I want the file to look like this again it is fixed width

THIS15021X         0799999 XX          00000099999 00008888888 XX 15022013
THISQ15021X     0999999 XX          00000099999 00008888888 XX 15022013
BLAS15021X        9999999 XX          00000199999 00008888888 XX 15022013

I want to add a leading 0 to the 2nd coloumn if it contains the word this at the beginning

awk '/^THIS/{$2="0"$2}1' file

In shell with built-in commands:

while read word1 word2 otherwords
do
  case $word1 in
  THIS*) word2="0$word2";;
  esac
  printf "%s %s %s\n" "$word1" "$word2" "$otherwords"
done < file > newfile

In shell by using awk:

awk '/^THIS/ {$2="0"$2} {print}' < file > newfile

Both solutions read from file and direct their output to newfile .

Thanks, that is very close - however I have noticed a funny problem:

So this is the actual file:

THIS15021X  123455 X            00000038600 00009999999 XX 15022013
THIS15021X   123456 XX           00000004260 00009999999 XX 15022013
THIS15021X   123457 XX            00000002240 00009999999 XX 15022013
THIS15021X   123458 XX            00000000800 00009999999 XX 15022013
THIS15021X   123459 XX            00000023445 00009999999 XX 15022013
THIS15021X   123460 XX            00000006632 00009999999 XX 15022013
BLAS15021X  1123456 XX            00000178839 00009999999 XX 15022013

However after running the commans, specificall the first one (note I am using a space before the 0 to move it along to the right position - the problem occurs without this space as well):

 awk '/CHEQ/ {$2=" 0"$2;}1' < file.txt > tempfile.txt
  

I am receiving this, the "BLAS" record is now incorrect:

THIS15021X   0123455 XX            00000038600 00009999999 XX 15022013
THIS15021X   0123456 XX            00000004260 00009999999 XX 15022013
THIS15021X   0123457 XX            00000002240 00009999999 XX 15022013
THIS15021X   0123458 XX            00000000800 00009999999 XX 15022013
THIS15021X   0123459 XX            00000023445 00009999999 XX 15022013
THIS15021X   0123460 XX            00000006632 00009999999 XX 15022013
BLAS15021X   1123456 XX                               00000178839 00009999999 XX 15022013

Will I need to specify the order in it's entirety $1 $2 $3 or something?

I think all the proposals up to now will lose the fixed width property which the requestor needed to keep, esp. if you just add a "0" in front of $2 the record size is gone.
Not sure, though, how to do it. Break it into lines with fmt , modify with awk or sed , remove <linefeed> with tr ?
Were there single lines, this might work:

$ sed 's:\(^THIS[^ ]*  \) :\10:' file

Can you talk me through that sed command - with a slight edit I "think" I get what I want:

> sed 's:\(^THIS[^ ]* \) :\00:' file

Specifically the 10, which I replaced to 00... - simply adds a space followed by a 0 to the location I require.

This is more close to your initial requirement:
if there is THIS at the beginning of the line, insert a 0 at the beginning of the 2nd column.
The only additional requirement: columns are separated by one or many space characters.

sed '/^THIS/ s/ \([^ ]\)/ 0\1/' file

Yes of course: The sed "s"ubstitute command searchs every line for a pattern to replace. Parentheses mark subpatterns, which in turn can be referenced by \1 .. \9 in the replacement string: sed 's /(pat)te(rn)/\2xy\1/ will yield rnxypat So you can't use \0 as you did. As I'm not sure what exactly you were aiming for, I can't help further. You may want to consider MadeInGermany's proposal which is a bit more flexible.