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
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):
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:
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.
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.