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
Shell Script Unique Identifier Question grahambo2005 UNIX for Dummies Questions & Answers 7 07-22-2008 12:41 PM
how can i replace / with new line in shell script or sed ? mail2sant Shell Programming and Scripting 3 04-25-2008 01:59 AM
Problem with shell script...ORA-00904:invalid identifier bhagat.singh-j Shell Programming and Scripting 12 11-13-2006 10:33 AM
find a shell and replace the line jvellon UNIX for Advanced & Expert Users 1 06-13-2006 09:44 PM
how to replace a line in a file using shell script cs_sakthi UNIX for Advanced & Expert Users 3 01-27-2002 11:57 PM

Closed Thread
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 05-06-2009
kenray kenray is offline
Registered User
  
 

Join Date: May 2009
Posts: 8
shell script to find and replace a line using a identifier

Hi all

im having trouble starting with a shell script, i hope someone here can help me

i have 2 files

file1:

404905.jpg 516167
404906.jpg 516168
404917.psd 516183
404947.pdf 516250

file2:

516250 /tmp/RecyclePoster18241.pdf
516167 /tmp/ReunionCardFINAL.jpg
516168 /tmp/MPP-3013594-05012009/ReunionBack.jpg
516183 /tmp/Stamp8x5.psd

my output woulbe like:

ouput:

516250 516250 /tmp/RecyclePoster18241.pdf
516167 404905.jpg /tmp/ReunionCardFINAL.jpg
516168 404906.jpg /tmp/MPP-3013594-05012009/ReunionBack.jpg
516183 404917.psd /tmp/Stamp8x5.psd

note that file1 and file2 is not in particular order it could be
516183 404917.psd
404947.pdf 516250

and so on... although numbers 516183 and 404947 are unique which can be use as an identifier for the output

thanks in advance

Last edited by kenray; 05-06-2009 at 09:42 AM..
  #2 (permalink)  
Old 05-06-2009
pludi's Avatar
pludi pludi is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2008
Location: .at
Posts: 1,873
Where exactly do you need to replace any line? As far as your example goes, you just want a numerical sort on file2.
  #3 (permalink)  
Old 05-06-2009
kenray kenray is offline
Registered User
  
 

Join Date: May 2009
Posts: 8
hi thanks for the reply,

example in line 1 of file1 which is:
404905.jpg 516167

i need to replace 404905.jpg with /tmp/ReunionCardFINAL.jpg using file2 line which would result in

516167 /tmp/ReunionCardFINAL.jpg


although lines may not be in order, im not sure if while read statement can do job but anything
similar to the output is fine
  #4 (permalink)  
Old 05-06-2009
pludi's Avatar
pludi pludi is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2008
Location: .at
Posts: 1,873
Again, if your file1 is
Code:
404905.jpg 516167
404906.jpg 516168
404917.psd 516183
404947.pdf 516250
and file2 is
Code:
516250 /tmp/RecyclePoster18241.pdf
516167 /tmp/ReunionCardFINAL.jpg
516168 /tmp/MPP-3013594-05012009/ReunionBack.jpg
516183 /tmp/Stamp8x5.psd
I don't see any difference between your desired output and what sort -n file2 would produce.

Please be a bit more precise on what you want/need to do.
  #5 (permalink)  
Old 05-06-2009
kenray kenray is offline
Registered User
  
 

Join Date: May 2009
Posts: 8
ahh yes, sorry for that

file1:

404905.jpg 516167
404906.jpg 516168
404917.psd 516183
404947.pdf 516250

file2:

516250 /tmp/RecyclePoster18241.pdf
516167 /tmp/ReunionCardFINAL.jpg
516168 /tmp/MPP-3013594-05012009/ReunionBack.jpg
516183 /tmp/Stamp8x5.psd

ouput:

516250 404947.pdf /tmp/RecyclePoster18241.pdf
516167 404905.jpg /tmp/ReunionCardFINAL.jpg
516168 404906.jpg /tmp/MPP-3013594-05012009/ReunionBack.jpg
516183 404917.psd /tmp/Stamp8x5.psd
  #6 (permalink)  
Old 05-06-2009
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,539
if you have Python can able to use it, here's an alternative solution
Code:
#!/usr/bin/env python
d={}
for line in open("file1"):
    line= line.strip().split()
    d[line[-1]]=line[0]
for line in open("file2"):
    line= line.strip().split()
    print line[0],d[line[0]],''.join(line[1:])
output:
Code:
# ./test.py
516250 404947.pdf /tmp/RecyclePoster18241.pdf
516167 404905.jpg /tmp/ReunionCardFINAL.jpg
516168 404906.jpg /tmp/MPP-3013594-05012009/ReunionBack.jpg
516183 404917.psd /tmp/Stamp8x5.psd
  #7 (permalink)  
Old 05-06-2009
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,877
Use gawk, nawk or /usr/xpg4/bin/awk on Solaris:

Code:
awk 'NR == FNR { _[$2] = $1; next }
$1 = $1 FS _[$1]' file1 file2
Closed Thread

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:55 AM.


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