nQuote
asked on
How to replace nth column in a file
I have several files, one of them is as follows:
A|2|Fghg|2|12/31/2013 01:00 PM\r\n
B|3|Ghkf|2|11/23/2012 02:00 PM\r\n
I would like to replace the nth column with a different value. For the example above, n=3.
There are no end-of-file terminators and there are DOS record delimiters.
Any help appreciated.
A|2|Fghg|2|12/31/2013 01:00 PM\r\n
B|3|Ghkf|2|11/23/2012 02:00 PM\r\n
I would like to replace the nth column with a different value. For the example above, n=3.
There are no end-of-file terminators and there are DOS record delimiters.
Any help appreciated.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I am getting a syntax error on the 2nd pipe.
what syntax error are you getting, and in what way is it on the 2nd pipe?
ASKER
awk: OFS=|{$3="A"}1
awk: ^ syntax error
This is exactly what is returned.
awk: ^ syntax error
This is exactly what is returned.
Hi soccerplayer,
Could you please copy & paste your session (i.e. the command + error message). ozo's solution works for me.
Also, what flavour of Linux are you running this in?
Could you please copy & paste your session (i.e. the command + error message). ozo's solution works for me.
Also, what flavour of Linux are you running this in?
Did your command include the spaces?
ASKER
I have Linux 2.6.18-348. I typed:
awk -F'|'-v OFS='|''{$3="A"}1' Myfile.dat > junk.dat
awk -F'|'-v OFS='|''{$3="A"}1' Myfile.dat > junk.dat
ASKER
I am not sure about the spaces though. I added a space between ' and -v and that gave me a different error.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
BTW soccerplayer, when you originally got the error, was that the result of a copy/paste of ozo's solution, or did you type it manually?
ASKER
I typed the solution manually. The 2nd space was the issue. It is hard to tell if it is a space.
Thanks for the points, soccerplayer, although I think ozo's effort was worth a lot more than mine, especially since he did ask if your command included spaces (though I didn't spot that comment until after I'd posted mine - sorry ozo).
I typed the solution manually.
In future, I would recommend you copy/paste solutions wherever possible. This not only saves typing time, but debugging time, as you have seen above.
> The 2nd space was the issue. It is hard to tell if it is a space.
I think you'll find that both spaces were needed, coz if you omit the one before the "-v", it will fail with a different error.
If you want to remove a space, you could change:
...Myfile.dat > junk.dat
to:
...Myfile.dat >junk.dat
which is a commonly accepted alternative coding style...I think.
Later.
tel2
I typed the solution manually.
In future, I would recommend you copy/paste solutions wherever possible. This not only saves typing time, but debugging time, as you have seen above.
> The 2nd space was the issue. It is hard to tell if it is a space.
I think you'll find that both spaces were needed, coz if you omit the one before the "-v", it will fail with a different error.
If you want to remove a space, you could change:
...Myfile.dat > junk.dat
to:
...Myfile.dat >junk.dat
which is a commonly accepted alternative coding style...I think.
Later.
tel2
ASKER
I would like to replace the nth column with a single character such as 'A' in all lines.