I'm trying to figure out how to use sed (I realise I could use other tools but I'd like to know how to do it in sed, if it really makes more sense in another tool please advise) to edit a file containing lines like this:
1234561234567809912345612345678 00000999988STRING ONE EX30 0600000001 K XXYY
1122331122334409922334466554433 00000123499STRING TWO EX99 0600000002 K XXYY
as well as a number of other lines which are of different formats.
the strings STRING ONE
and STRING TWO
are in positions 47-64 (including their following spaces).
I want to change text in positions 65-80 to another value dependant on the contents of positions 47-64.
So for the STRING ONE line, chars 65-80 would be amended to "AAAABBBBCCCC " (4 trailing spaces).
In the STRING TWO line, chars 65-80 would be amended to "XXXYYYZZZ " (4 trailing spaces).
I've got this far:
sed 's/^\(.\{64\}\)EX/\1234567890 /'
which will substitute "EX" at char 65 to "234567890 " but this
- doesn't account for picking the correct lines (with STRING ONE or STRING TWO)
- only substitutes "EX"
& there are other things I'm not following here.
- Why the dot between the open bracket & the backslash curly bracket?
- why precede the 1 with a backslash (which seems to be necessary but causes the 1 not to be in the substitute string)?
I expect I could get this done using grep & manipulating output to temporary files or environment variables but would be nice to know if I can do it more elegantly.