Replacing values in a file based on values in another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing values in a file based on values in another file
# 1  
Old 07-01-2010
Replacing values in a file based on values in another file

Hi

I have 2 files:-

1. List of files which consists of names of some output files.
2. A delimited file; delimted by "|"

I want to replace the value of the $23 (23rd column) in the delimited file with name in the first file. It is always position to position. Meaning first row of the first file to replace the $23 in the first row of second file and so on. Both the files will have equal number of lines.

e.g.

Code:
File 1:
=======
AAABBBCC_HHGGTT_34_20100701113142_001_D_00_0.pdf
11122233_HHGGTT_34_20100701113142_001_D_00_0.pdf
55566677_HHGGTT_34_20100701113142_001_D_00_0.pdf
88899900_HHGGTT_34_20100701113142_001_D_00_0.pdf

File 2:
=======
1|10|HHHH|9|112222||0040400||445566|S|O|X||||20100701113142-31.12.2009|667553AT|GST||||00001|2010_01_4010093_001_00_aa.pdf|
1|11|HHHH|9|112222||0040400||445566|S|O|X||||20100701113142-31.12.2009|667553AT|GST||||00002|2010_01_4010162_001_00_aa.pdf|
1|17|HHHH|9|112222||0040400||445566|S|O|X||||20100701113142-31.12.2009|667553AT|GST||||00003|2010_01_4010163_001_00_aa.pdf|
1|22|HHHH|9|112222||0040400||445566|S|O|X||||20100701113142-31.12.2009|667553AT|GST||||00004|2010_01_4010164_001_00_aa.pdf|


Desired Output:
================

1|10|HHHH|9|112222||0040400||445566|S|O|X||||20100701113142-31.12.2009|667553AT|GST||||00001|AAABBBCC_HHGGTT_34_20100701113142_001_D_00_0.pdf|
1|11|HHHH|9|112222||0040400||445566|S|O|X||||20100701113142-31.12.2009|667553AT|GST||||00002|11122233_HHGGTT_34_20100701113142_001_D_00_0.pdf|
1|17|HHHH|9|112222||0040400||445566|S|O|X||||20100701113142-31.12.2009|667553AT|GST||||00003|55566677_HHGGTT_34_20100701113142_001_D_00_0.pdf|
1|22|HHHH|9|112222||0040400||445566|S|O|X||||20100701113142-31.12.2009|667553AT|GST||||00004|88899900_HHGGTT_34_20100701113142_001_D_00_0.pdf|

Can somebody please help.
# 2  
Old 07-01-2010
Hi

Code:
sed 's/\(.*\)|\(.*|\)/\1/' file2 | paste -d"|" - file1 | sed 's/.*/&|/'

Guru.
This User Gave Thanks to guruprasadpr For This Post:
# 3  
Old 07-01-2010
Try,

Code:
$ nawk -F '|' 'BEGIN { i=1;OFS="|" } NR==FNR { _[i]=$1; i++ ; next } { $(NF-1)=_[FNR]; print }' file_1 file_2

This User Gave Thanks to agn For This Post:
# 4  
Old 07-01-2010
hi
Try This,

Code:
cut -d'|' -f1-22 file2 > temp ; paste -d'|' temp file1 | sed 's/$/|/g'

# 5  
Old 07-01-2010
Thanks Guru and AGN. Both the codes work perfectly fine.Smilie
# 6  
Old 07-01-2010
What is the PERL code for the same output. Actually i'm searching of it
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replacing values inside a file.

Good day guys, I'm having trouble in creating a logic when it comes to replacing the values inside a file. I tried using sed command but it just doesn't work the way I want it to be. Here is what I'm trying to achieve. If my input file contains the values below. NAME++GUEST1 ++GUESS2++... (3 Replies)
Discussion started by: asdfghjkl
3 Replies

2. Shell Programming and Scripting

awk to look up values in File 2 from File 1, & printingNth field of File1 based value of File2 $2

I have two files which are the output of a multiple choice vocab test (60 separate questions) from 104 people (there are some missing responses) and the question list. I have the item list in one file (File1) Item,Stimulus,Choice1,Choice2,Choice3,Choice4,Correct... (5 Replies)
Discussion started by: samonl
5 Replies

3. Shell Programming and Scripting

awk file to read values from Db2 table replacing hard coded values

Hi, I want to replace a chain of if-else statement in an old AWK file with values from Db2 table or CSV file. The part of code is below... if (start_new_rec=="true"){ exclude_user="false"; user=toupper($6); match(user, "XXXXX."); if (RSTART ==2 ) { ... (9 Replies)
Discussion started by: asandy1234
9 Replies

4. Shell Programming and Scripting

Replacing string values from a File

I want to replace string values from a file to a file For eg : File1 has 30 lines of string with values, those specific values needs to be changed in file2 and remaining values in file2 should be as it is. For example: From file (File1) cluster.name=secondaryCluster To replace File... (9 Replies)
Discussion started by: sriram003
9 Replies

5. Shell Programming and Scripting

Replacing column 1 in one file with values in other file

Please help me with an shell / awk script to achieve following; File-1: ABCDW01 12322 23322 BDADW01 22232 24453 EDFAW00 32232 23422 and so on, notice that the first coloumn is a code and the another file contains the real value of each entry in the first colum above but not in a... (4 Replies)
Discussion started by: digipak
4 Replies

6. UNIX for Dummies Questions & Answers

count values based on contents of another file

Hello, I have two files as shown below: test1 678 679 689 690 710 test2 1 678 654 800 676 791 689 900 I want to get a count of lines from test2 whose columns bound the values in test1 I tried running the code below; however am getting wrong results. (3 Replies)
Discussion started by: Gussifinknottle
3 Replies

7. UNIX for Dummies Questions & Answers

Find and Replace based on values in an file

I have a file in which I want to do multiple find and replace of strings. For a single replace I can implement: sed -i 's/old/new/' <input_file> I have a second file that contains the old and the new values like the arbitrary example below: old new xyz pqr ab 756 rst pqr... (3 Replies)
Discussion started by: Gussifinknottle
3 Replies

8. Shell Programming and Scripting

File manipulation based on values in file

Hi, using Shell to do some file manipulation here. Input - input.txt "2006/2007", "string1a","string2v","stringf3" "2006/2007", "string12b","string30c","string10d" "2006/2007", "string22","string22","string11" "2007/2008", "string1a","string2v","stringf3" "2007/2008",... (5 Replies)
Discussion started by: pgop
5 Replies

9. Shell Programming and Scripting

Replacing File values

Currently I am using the tr command in 3 scenarios for a ksh script. 1) Replacing any spaces in the file with a ~ tr ' ' '~' <$orignalFile> $newFile 2) After certain processing is done at the end of the scirpt i convert the Tilde back to spaces tr ' ' '~' <$newFile> $newFile2 3) Last... (4 Replies)
Discussion started by: hgjdv
4 Replies

10. Shell Programming and Scripting

extract from a file based on values in another file

Hello, I have two files that have delimited entries as shown below. I would like to use either Perl or Shell script to extract all the rows in File 1 corresponding to values in File 2 and output it to another File. File 1 ------- 1 36 24 Object1 2 45 36 Object2 3 96 ... (1 Reply)
Discussion started by: Gussifinknottle
1 Replies
Login or Register to Ask a Question