The UNIX and Linux Forums  


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
Replacing words in a fast way phil_heath Shell Programming and Scripting 5 07-27-2009 04:30 AM
awk after words flekzout Shell Programming and Scripting 6 05-11-2009 01:58 PM
Sed replacing words with abbreviations mauler123 Shell Programming and Scripting 4 03-03-2009 08:41 AM
Get the words.. kakashi_jet Shell Programming and Scripting 10 07-31-2006 10:30 AM
Replacing words in a file adam85 High Level Programming 1 04-07-2006 12:27 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-01-2009
kylle345 kylle345 is offline
Registered User
  
 

Join Date: May 2009
Posts: 53
Replacing words

Hi, I have am using a file that contains names that I want to replace.

Basically file 1 looks like this


Code:
jack
joe
james
john

I have another file (file 2) that looks like this


Code:
jack      2345
joe       6848
james   3342
john     3432

Basically I want to replace column1 from file1 with column 2 from file 2 if its a match. The two files are of different length. Is there a fast way of doing this?
  #2 (permalink)  
Old 10-01-2009
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131

Code:
nawk 'FNR==NR{f2[$1]=$2;next} $1=($1 in f2)?f2[$1]:$1}' file2 file1

  #3 (permalink)  
Old 10-01-2009
kylle345 kylle345 is offline
Registered User
  
 

Join Date: May 2009
Posts: 53
hey i got an error

looks like this


Code:
awk: extra } at source line 1
 context is
        FNR==NR{f2[$1]=$2;next} $1=($1 in >>>  f2)?f2[$1]:$1} <<< 
awk: syntax error at source line 1
        extra }
awk: bailing out at source line 1

  #4 (permalink)  
Old 10-01-2009
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
sorry:

Code:
nawk 'FNR==NR{f2[$1]=$2;next} $1=($1 in f2)?f2[$1]:$1' file2 file1

  #5 (permalink)  
Old 10-01-2009
kylle345 kylle345 is offline
Registered User
  
 

Join Date: May 2009
Posts: 53
thank you so much! works fine
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 03:24 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
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