Solved

How to replace nth column in a file

Posted on 2013-05-17
13
409 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
remove spaces from a file name linux 3 65
Parse DNS log 3 74
Powershell to pull all Service accounts 1 63
add some character at the end of line in vi 7 91
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…
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…
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…

856 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