LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 06-03-2009, 11:17 AM   #1
vgr12386
LQ Newbie
 
Registered: Jun 2009
Posts: 16

Rep: Reputation: 0
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
 
Old 06-03-2009, 02:56 PM   #2
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
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
 
Old 06-03-2009, 07:36 PM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
awk -F"," 'FNR==NR{a[$1]=$2;next}
( $5 in a ){
     $5=a[$5]; 
     #<do something 
}' error_correction file
 
Old 06-04-2009, 06:45 AM   #4
vgr12386
LQ Newbie
 
Registered: Jun 2009
Posts: 16

Original Poster
Rep: Reputation: 0
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?
 
Old 06-04-2009, 08:16 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984
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
 
Old 06-04-2009, 09:01 AM   #6
vgr12386
LQ Newbie
 
Registered: Jun 2009
Posts: 16

Original Poster
Rep: Reputation: 0
thanks man
 
Old 06-05-2009, 05:01 AM   #7
vgr12386
LQ Newbie
 
Registered: Jun 2009
Posts: 16

Original Poster
Rep: Reputation: 0
hey is it possible to use two delimiters for example , for one file and ; for the other?
 
Old 06-05-2009, 05:19 AM   #8
vgr12386
LQ Newbie
 
Registered: Jun 2009
Posts: 16

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by colucix View Post
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
 
Old 06-05-2009, 07:14 AM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984
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:
Code:
BEGIN{OFS=","}
at the beginning of the awk program. Please, post the actual code you're using, if you need more advices.
 
Old 06-05-2009, 07:15 AM   #10
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984
Quote:
Originally Posted by vgr12386 View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
deleting a field using awk jkeertir Linux - Newbie 5 04-13-2008 10:55 PM
How to parameterize which field awk should print? dbland07666 Linux - Newbie 2 10-29-2007 03:49 PM
Multiple Field Seperators in Awk... TheDarktrooper Programming 6 05-06-2004 04:50 AM
My field separator changes when using awk Helene Programming 3 05-01-2004 08:10 AM
Two field seperators in awk?? Astro Programming 2 11-09-2003 10:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration