Solved

How to replace nth column in a file

Posted on 2013-05-17
13
449 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 12

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
 
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 12

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 12

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 12

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

Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Utilizing an array to gracefully append to a list of EmailAddresses
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

734 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