Solved

How to replace nth column in a file

Posted on 2013-05-17
13
369 Views
Last Modified: 2013-05-17
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
Comment
Question by:soccerplayer
  • 6
  • 4
  • 3
13 Comments
 

Author Comment

by:soccerplayer
ID: 39176186
More info:
I would like to replace the nth column with a single character such as 'A' in all lines.
0
 
LVL 84

Accepted Solution

by:
ozo earned 250 total points
ID: 39176193
awk -F'|' -v OFS='|' '{$3="A"}1' file
0
 

Author Comment

by:soccerplayer
ID: 39176212
I am getting a syntax error on the 2nd pipe.
0
 
LVL 84

Expert Comment

by:ozo
ID: 39176233
what syntax error are you getting, and in what way is it on the 2nd pipe?
0
 

Author Comment

by:soccerplayer
ID: 39176248
awk: OFS=|{$3="A"}1
awk:         ^ syntax error

This is exactly what is returned.
0
 
LVL 11

Expert Comment

by:tel2
ID: 39176278
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 84

Expert Comment

by:ozo
ID: 39176283
Did your command include the spaces?
0
 

Author Comment

by:soccerplayer
ID: 39176287
I have Linux 2.6.18-348. I typed:

awk -F'|'-v OFS='|''{$3="A"}1' Myfile.dat > junk.dat
0
 

Author Comment

by:soccerplayer
ID: 39176293
I am not sure about the spaces though. I added a space between ' and -v and that gave me a different error.
0
 
LVL 11

Assisted Solution

by:tel2
tel2 earned 250 total points
ID: 39176298
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
 
LVL 11

Expert Comment

by:tel2
ID: 39176312
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
 

Author Closing Comment

by:soccerplayer
ID: 39176342
I typed the solution manually. The 2nd space was the issue. It is hard to tell if it is a space.
0
 
LVL 11

Expert Comment

by:tel2
ID: 39176359
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now