shell script to find and replace a line using a identifier


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shell script to find and replace a line using a identifier
# 1  
Old 05-06-2009
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  
Old 05-06-2009
Where exactly do you need to replace any line? As far as your example goes, you just want a numerical sort on file2.
# 3  
Old 05-06-2009
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  
Old 05-06-2009
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  
Old 05-06-2009
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  
Old 05-06-2009
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  
Old 05-06-2009
Use gawk, nawk or /usr/xpg4/bin/awk on Solaris:

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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script using awk to find and replace a line, how to ignore comment lines

Hello, I have some code that works more or less. This is called by a make file to adjust some hard-coded definitions in the src code. The script generated some values by looking at some of the src files and then writes those values to specific locations in other files. The awk code is used to... (3 Replies)
Discussion started by: LMHmedchem
3 Replies

2. Shell Programming and Scripting

Shell Script to find common lines and replace next line

I want to find common line in two files and replace the next line of first file with the next line of second file. (sed,awk,perl,bash any solution is welcomed ) Case Ignored. Multiple Occurrence of same line. File 1: hgacdavd sndm,ACNMSDC msgid "Rome" msgstr "" kgcksdcgfkdsb... (4 Replies)
Discussion started by: madira
4 Replies

3. Shell Programming and Scripting

Find and Replace in Shell script

Friends, I have more than 1000 lines in text file which needs to be converted as UPPERCASE by adding _ com.sun.url=www.sun.com com.ssl.port=808 com.ui.path=/apps/ssi Expected output com.sun.url=_COM.SUN.URL_ com.ssl.port=_COM.SSL.PORT_ com.ui.path=_COM.UI.PATH_ Thanks in... (4 Replies)
Discussion started by: baluchen
4 Replies

4. UNIX for Dummies Questions & Answers

Script to find line in one file and replace in another

Hey Guys, im looking for a script that will work under OSX. What i want to do is copy information from one file (Specific LIne) and write it to a certain line in another. To be more specific... I want the hostname of a mac to be gathered ( i assume its stored in a .plist file somewhere) and... (2 Replies)
Discussion started by: padgo
2 Replies

5. Shell Programming and Scripting

Identifier In Shell script

Hi, I have a shell script and inside that shell script it calls a local .env file to set the environment for the shell script ,but the thing is that i got a error while running the script like ./myscript.sh it gives DB_NAME=dvcl021: is not an identifier that DB_Name is accessed from my .env... (6 Replies)
Discussion started by: malickhat
6 Replies

6. UNIX for Dummies Questions & Answers

Shell Script Unique Identifier Question

i All I have scripting question. I have a file "out.txt" which is generated by another script the file contains the following my_identifier8859574 logout The number is generated in the script and I have put the my_identifier bit in front of it as a unique identifier I now have... (7 Replies)
Discussion started by: grahambo2005
7 Replies

7. Shell Programming and Scripting

how can i replace / with new line in shell script or sed ?

1. how can i replace ' / ' with new line in shell script or sed ? 2. how can set a conditon untill null in while loop while ( i== null ) do ...... done (3 Replies)
Discussion started by: mail2sant
3 Replies

8. Shell Programming and Scripting

Find and Replace in multiple files (Shell script)

hi guys, Suppose you have 100 files in a folder and you want to replace all occurances of a word say "ABCD" in those files with "DCBA", how would you do it ??? jatin (13 Replies)
Discussion started by: jatins_s
13 Replies

9. UNIX for Advanced & Expert Users

find a shell and replace the line

I need a shell which makes a search of an UNIX script and them modifies. :confused: vi $(grep -l 5 $(find . -name 'vellon.bcf' -print)) (1 Reply)
Discussion started by: jvellon
1 Replies

10. UNIX for Advanced & Expert Users

how to replace a line in a file using shell script

I have a property file in which the DB name is specified and when i run my servers they will point to the DB specified in that property file. Now i'm gonna write a script which will start all the services. But before that i just want to dynamically change the DB name in that property file by... (3 Replies)
Discussion started by: cs_sakthi
3 Replies
Login or Register to Ask a Question