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.
soccerplayerAsked:
Who is Participating?
 
ozoConnect With a Mentor Commented:
awk -F'|' -v OFS='|' '{$3="A"}1' file
0
 
soccerplayerAuthor Commented:
More info:
I would like to replace the nth column with a single character such as 'A' in all lines.
0
 
soccerplayerAuthor Commented:
I am getting a syntax error on the 2nd pipe.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
ozoCommented:
what syntax error are you getting, and in what way is it on the 2nd pipe?
0
 
soccerplayerAuthor Commented:
awk: OFS=|{$3="A"}1
awk:         ^ syntax error

This is exactly what is returned.
0
 
tel2Commented:
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?
0
 
ozoCommented:
Did your command include the spaces?
0
 
soccerplayerAuthor Commented:
I have Linux 2.6.18-348. I typed:

awk -F'|'-v OFS='|''{$3="A"}1' Myfile.dat > junk.dat
0
 
soccerplayerAuthor Commented:
I am not sure about the spaces though. I added a space between ' and -v and that gave me a different error.
0
 
tel2Connect With a Mentor Commented:
I'm glad I asked.

You've removed 2 spaces from ozo's solution.  Copy and paste this:
    awk -F'|' -v OFS='|' '{$3="A"}1' Myfile.dat > junk.dat
0
 
tel2Commented:
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?
0
 
soccerplayerAuthor Commented:
I typed the solution manually. The 2nd space was the issue. It is hard to tell if it is a space.
0
 
tel2Commented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.