?
Solved

How to replace nth column in a file

Posted on 2013-05-17
13
Medium Priority
?
517 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 1000 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
Understanding Linux Permissions

Linux for beginners: How to view the permissions associated with files and directories and also how you can change them.

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

Industry Leaders: 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!

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
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…

719 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