The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
replace a field in a CSV file prvnrk Shell Programming and Scripting 2 11-12-2008 08:51 AM
replace single field out of 60 ajp7701 Shell Programming and Scripting 2 04-21-2008 07:17 PM
Replace field in csv pcboss Linux 4 01-19-2008 06:24 PM
Replace password field using ed/sed munch UNIX for Dummies Questions & Answers 6 04-30-2007 11:01 PM
Replace 10th Field by 2 PradeepRed Shell Programming and Scripting 2 01-19-2006 03:46 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-03-2009
VGR VGR is offline
Registered User
  
 

Join Date: Mar 2008
Location: kolkata
Posts: 34
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??
  #2 (permalink)  
Old 06-03-2009
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Wink An example to get you started...

Code:
> echo "867 5309 VerNOM 1234 Abcd"
867 5309 VerNOM 1234 Abcd

> echo "867 5309 VerNOM 1234 Abcd" | awk -v v1="VerNOM" -v v2="Vernom" '{gsub(v1,v2,$3);print}'
867 5309 Vernom 1234 Abcd
  #3 (permalink)  
Old 06-04-2009
VGR VGR is offline
Registered User
  
 

Join Date: Mar 2008
Location: kolkata
Posts: 34
Thanks Joeyg...
I was not quiet sure how to pull up values from another file from inside the awk!
how Do i use an ass. array[hash] inside awk??
  #4 (permalink)  
Old 06-04-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,212
Try this:

Code:
awk -F, '
NR==FNR{a[$1]=$2;next}
a[$5]{$5=a[$5]}
1' error_correction.txt datafile
Regards
  #5 (permalink)  
Old 06-04-2009
VGR VGR is offline
Registered User
  
 

Join Date: Mar 2008
Location: kolkata
Posts: 34
im not quiet sure why this is happening...
the value changes inside the loop but is not being saved to the file!
  #6 (permalink)  
Old 06-04-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,212
The file is can not be edited "in place" with awk. Redirect the output to a temporary file and replace the original file with it.

Code:
awk '<commands>' error_correction.txt datafile > tempfile
mv tempfile datafile
  #7 (permalink)  
Old 06-04-2009
VGR VGR is offline
Registered User
  
 

Join Date: Mar 2008
Location: kolkata
Posts: 34
cool...
awesome stuff
thanks a ton man....
Sponsored Links
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 11:12 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0