hello,
i have a undelimited file which contains 229 byte records. i want to change column 23 - 26 with a new value and also change the sign of the data in colulmn 30 - 70. i've tried SED for the first change, but nothing happens:
sed 's/\(^.\{22\}\).\{4\}\(.*\)/\0603\2/' inputfile
heres an example of my record -but it doesn't display the whole 229 bytes - not sure why
99999999999999930531130531AUD 176938000.000000 S1HHHHHHH079928 ST HHHHHHH079928
99999999999999930531130531AUD -205866000.000000 M19887777777766 MA 9887777777766
99999999999999930531130531CAD 16179000.000000 S1HHHHHHH079904 ST HHHHHHH079904
99999999999999930531130531CAD -15374000.000000 M19887777777742 MA 9887777777742
this is what i want
99999999999999930531130603AUD -176938000.000000 S1HHHHHHH079928 ST HHHHHHH079928
99999999999999930531130603AUD 205866000.000000 M19887777777766 MA 9887777777766
99999999999999930531130603CAD -16179000.000000 S1HHHHHHH079904 ST HHHHHHH079904
99999999999999930531130603CAD 15374000.000000 M19887777777742 MA 9887777777742
make the code changes and keep the full record of 229 bytes thanks!
can you help with both requests - thanks!
Please give us a few lines from inputfile and provide the desired output as well. It isn't obvious whether you're asking us to change a + to a - and a - to a + or if you want us to insert or remove a -. We also need to see if you're counting from 0 or from 1.
---------- Post updated at 15:13 ---------- Previous update was at 14:30 ----------
Try:
sed -n 's/\(^.\{22\}\)..../\10603/
s/\(^.\{29\}\)\( *\)-/\1\2s/
s/\(^.\{29\}\)\( *\)[ +]\([0-9.]\)/\1\2-\3/
s/\(^.\{29\}\)\( *\)s/\1\2 /
p' inputfile
It looks like you pasted everything into 1 line instead of keeping the five lines I posted.
If you insist on making it a single line, you can try:
sed -n 's/\(^.\{22\}\)..../\10603/;s/\(^.\{29\}\)\( *\)-/\1\2s/;s/\(^.\{29\}\)\( *\)[ +]\([0-9.]\)/\1\2-\3/;s/\(^.\{29\}\)\( *\)s/\1\2 /;p' inputfile
but I MUCH prefer code where I can more easily see the relationship between the changes made by the last three substitute commands than having something that hides those relationships on a single line.
Both of them produce the output:
99999999999999930531130603AUD -176938000.000000 S1HHHHHHH079928 ST HHHHHHH079928
99999999999999930531130603AUD 205866000.000000 M19887777777766 MA 9887777777766
99999999999999930531130603CAD -16179000.000000 S1HHHHHHH079904 ST HHHHHHH079904
99999999999999930531130603CAD 15374000.000000 M19887777777742 MA 9887777777742
with the sed on OS X with the latest update to your input. In addition to changing a - to a space and a space to a - in the last column before the first digit in that field, it will also change a + to a - in that position. This should work with any sed utility that conforms to the POSIX standards and the Single UNIX Specification.
obviously, i dont know what i am doing. when i execute that command; it just displays the output on the screen as it runs thru each record, but doens't make the changes to the file?
You will note that the . in \([0-9.]\) in the 3rd line of this sed command takes care of this case. Are you saying it didn't work when you tried it? I tested cases like this before I posted the code and tried the two lines above in addition to my former tests. It works as desired when I try it.
What OS are you using?
Please use the CODE tag when posting code (as I corrected it in the quote from your last message) instead of using the ICODE tag whenever you are posting long lines that shouldn't be folded. And, never use the ICODE tag to mark multiple lines of code or data that should be displayed as a group.