Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
06-03-2009, 11:17 AM
|
#1
|
LQ Newbie
Registered: Jun 2009
Posts: 16
Rep:
|
awk to replace particular field
awk to replace particular field
i have bad data for instance, the use of capital letters in the middle of the word.
I identified the errors, made a list and put it in a file error_correction.txt
Incorrect,Correct
VeNOM,Venom
nos,NOS
.
.
.
My data file looks like this:
vgr,bugatti veron,,3.5,Venom,6,.......,....
vgr,lamborgini,,3.5,nos,6,.......,....
abc,bugatti veron,,3.5,Venom,6,.......,.......
.
.
.
.
I need to replace the terms in a the 5th field with that from the file.
How do i do this using awk??
Edit/Delete Message Reply With Quote Multi-Quote This Message Quick reply to this message
|
|
|
06-03-2009, 02:56 PM
|
#2
|
Senior Member
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039
Rep:
|
This screams homework to me, however here is a start for you:
Code:
awk -F',' -vOFS=',' '( $5 == v_test ) {$5 = v_replace} {print} ' v_test=VeNOM v_replace=Venom data_file
|
|
|
06-03-2009, 07:36 PM
|
#3
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
Code:
awk -F"," 'FNR==NR{a[$1]=$2;next}
( $5 in a ){
$5=a[$5];
#<do something
}' error_correction file
|
|
|
06-04-2009, 06:45 AM
|
#4
|
LQ Newbie
Registered: Jun 2009
Posts: 16
Original Poster
Rep:
|
thanks a lot ghost
There seemed to be one problem.
The value seemed to be changing inside the loop but the files remained unchanged!
The change was not being saved.
How do i save it?
|
|
|
06-04-2009, 08:16 AM
|
#5
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
awk cannot change the input file. You have to redirect the output to a new file and then rename it as the original one:
Code:
$ awk blah blah blah input_file > output_file
$ mv output_file input_file
|
|
|
06-04-2009, 09:01 AM
|
#6
|
LQ Newbie
Registered: Jun 2009
Posts: 16
Original Poster
Rep:
|
thanks man
|
|
|
06-05-2009, 05:01 AM
|
#7
|
LQ Newbie
Registered: Jun 2009
Posts: 16
Original Poster
Rep:
|
hey is it possible to use two delimiters for example , for one file and ; for the other?
|
|
|
06-05-2009, 05:19 AM
|
#8
|
LQ Newbie
Registered: Jun 2009
Posts: 16
Original Poster
Rep:
|
Quote:
Originally Posted by colucix
awk cannot change the input file. You have to redirect the output to a new file and then rename it as the original one:
Code:
$ awk blah blah blah input_file > output_file
$ mv output_file input_file
|
Hey when i do this,
the field seprater ie. "," is being removed!
The line i get is seprated only by spaces
|
|
|
06-05-2009, 07:14 AM
|
#9
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
Well, the program "blah blah blah" does not work, indeed!
Seriously now: if you want a comma as Output Field Separator you have to specify it using:
at the beginning of the awk program. Please, post the actual code you're using, if you need more advices.
|
|
|
06-05-2009, 07:15 AM
|
#10
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
Quote:
Originally Posted by vgr12386
hey is it possible to use two delimiters for example , for one file and ; for the other?
|
I don't really know, but maybe after the "next" statement you can change the value of FS (the input field separator). Again, please re-post the input files and the awk code you're using now. Thx.
|
|
|
All times are GMT -5. The time now is 02:37 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|