[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 644
  • Last Modified:

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.
0
soccerplayer
Asked:
soccerplayer
  • 6
  • 4
  • 3
2 Solutions
 
soccerplayerAuthor Commented:
More info:
I would like to replace the nth column with a single character such as 'A' in all lines.
0
 
ozoCommented:
awk -F'|' -v OFS='|' '{$3="A"}1' file
0
 
soccerplayerAuthor Commented:
I am getting a syntax error on the 2nd pipe.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
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
 
tel2Commented:
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now