replacing text in file1 with list from file2


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replacing text in file1 with list from file2
# 1  
Old 07-22-2008
Question replacing text in file1 with list from file2

I am trying to automate a process of searching through a set of files and replace all occurrences of a formatted text with the next item in the list of a second file. Basically i need to replace all instances of T????CLK???? with an IP address from a list in a second file. the second file is one IP per line with no special formatting other then xxx.xxx.xxx.xxx

Thanks in advance Everyone!
# 2  
Old 07-22-2008
How do you know which ip address in the second file goes with a given T???CLK???? pattern in file1?
# 3  
Old 07-22-2008
The IP that gets inserted does not matter as long as each one is used only once. If its read out of order that would not matter to me.
# 4  
Old 07-23-2008
Hope below can help you.

Below perl demo is to replace this with a array file by file. If reach out of the array, then stop replace.


Code:
@arr=(This,THis,THIS);
$n=0;
opendir(DIRECTORY, '.') or die "Can't open current directory.";
@files=readdir(DIRECTORY);
closedir DIRECTORY;
for($i=2;$i<$#files;$i++){
print "Begin change file:",$files[$i],"\n";
open FH,"<$files[$i]";
while(<FH>){
if($arr[$n]){
$_=~s/this/$arr[$n]/;
}
print $_;
}
close(FH);
print "End change file:",$files[$i],"\n\n";
$n++;
}



below awk demo is to replace this with value in another replace file file by file, if reach out of the end of replace file, will use the last one to replace the rest of file.
Code:
n=0
for i in *
do
        temp=""
        n=`expr $n + 1`
        temp=`cat ../replace | head -"$n" |tail -1`
        cat $i | nawk -v t="$temp" '{
                gsub(/this/,t,$0)
                print $0
        }'
done


Last edited by summer_cherry; 07-23-2008 at 04:36 AM..
# 5  
Old 07-23-2008
As a stylistic comment, you might want to avoid the Useless Use of Cat and Head | Tail.

Code:
n=0
for i in *
do
        temp=""
        n=`expr $n + 1`
        temp=`sed -n "${n}p" ../replace`
        nawk -v t="$temp" '{
                gsub(/this/,t,$0)
                print $0
        }' "$i" >tmp
       # Don't replace file if no change
       cmp "$i" tmp >/dev/null && continue
       mv "$i" "$i"~
       mv tmp "$i"
done
rm -f tmp

I also added the storage of the results in a temporary file, and backing up of the original.
# 6  
Old 07-23-2008
Thanks guys, I am still having an issue. OK a couple:
1) using the win32 versions of sed/awk do iI need to switch the / to \
2) I have never used awk before and don't understand is that a script to throw at awk or just a UNIX shell script?

sorry for the stupid questions, gotta learn somehow....
# 7  
Old 07-23-2008
Thanks guys, I am still having an issue. OK a couple:
1) using the win32 versions of sed/awk do iI need to switch the / to \
2) I have never used awk before and don't understand is that a script to throw at awk or just a UNIX shell script?

sorry for the stupid questions, gotta learn somehow....
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk- Indexing a list of numbers in file2 to print certain rows in file1

Hi Does anyone know of an efficient way to index a column of data in file2 to print the coresponding row in file1 which corresponds to the data in file2 AND 30 rows preceding and after the row in file1. For example suppose you have a list of numbers in file2 (single column) as follows:... (6 Replies)
Discussion started by: Geneanalyst
6 Replies

2. Shell Programming and Scripting

awk to search field2 in file2 using range of fields file1 and using match to another field in file1

I am trying to use awk to find all the $2 values in file2 which is ~30MB and tab-delimited, that are between $2 and $3 in file1 which is ~2GB and tab-delimited. I have just found out that I need to use $1 and $2 and $3 from file1 and $1 and $2of file2 must match $1 of file1 and be in the range... (6 Replies)
Discussion started by: cmccabe
6 Replies

3. UNIX for Dummies Questions & Answers

Compare file1 and file2, print matching lines in same order as file1

I want to print only the lines in file2 that match file1, in the same order as they appear in file 1 file1 file2 desired output: I'm getting the lines to match awk 'FNR==NR {a++}; FNR!=NR && a' file1 file2 but they are in sorted order, which is not what I want: Can anyone... (4 Replies)
Discussion started by: pathunkathunk
4 Replies

4. Shell Programming and Scripting

Replacing first field of file2 with the second filed of file1 for matching cases

Dear All, Need your help..:D I am not regular on shell scripts..:( I have 2 files.. Content of file1 cellRef 4};"4038_2_MTNL_KALAMBOLI" cellRef 1020};"4112_3_RAINBOW_BLDG" cellRef 134};"4049_2_TATA_HOSPITAL" cellRef 1003};"4242_3_HITESH_CONSTRUCTION" cellRef... (6 Replies)
Discussion started by: ailnilanjan
6 Replies

5. Shell Programming and Scripting

Pattern Matching & replacing of content in file1 with file2

I have file 1 & file 2 with content mentioned below. I want to get the output as shown in file3. Requirement: check the content of column 1 & column 2, if value of column 1 in file1 matches with first column of file2 then remaining columns(2&3) of file2 should get replaced, also if value of... (4 Replies)
Discussion started by: siramitsharma
4 Replies

6. Shell Programming and Scripting

Search within file1 numbers from list in file2

Hello to all, I hope somebody could help me with this: I have this File1 (real has 5 million of lines): Number Category --------------- -------------------------------------- 8734060355 3 8734060356 ... (6 Replies)
Discussion started by: Ophiuchus
6 Replies

7. UNIX for Dummies Questions & Answers

if matching strings in file1 and file2, add column from file1 to file2

I have very limited coding skills but I'm wondering if someone could help me with this. There are many threads about matching strings in two files, but I have no idea how to add a column from one file to another based on a matching string. I'm looking to match column1 in file1 to the number... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

8. UNIX for Dummies Questions & Answers

Delete strings in file1 based on the list of strings in file2

Hello guys, should be a very easy questn for you: I need to delete strings in file1 based on the list of strings in file2. like file2: word1_word2_ word3_word5_ word3_word4_ word6_word7_ file1: word1_word2_otherwords..,word3_word5_others... (7 Replies)
Discussion started by: roussine
7 Replies

9. Shell Programming and Scripting

append text from file1 to the end of each line in file2

hi; my file2.txt:portname=1;list=10.11;l- portname=2;list=10.12;l- portname=3;list=10.13;l- ... my file1.txt:;"{'sector=%27'}"\&> so; i want to see:portname=1;list=10.11;l-;"{'sector=%27'}"\&> portname=2;list=10.12;l-;"{'sector=%27'}"\&> portname=3;list=10.13;l-;"{'sector=%27'}"\&>... (4 Replies)
Discussion started by: gc_sw
4 Replies

10. Shell Programming and Scripting

awk - replacing stings in file1 with column1 in file2

Hello, I've never used awk before, but from what I've read, it will best suit what I'm trying to do. I have 2 files. I need to replace strings in file1 with the first column of a matching string in file2. Below are examples: File1: random-string1 1112 1232 3213 2131 random-string2... (7 Replies)
Discussion started by: upstate_boy
7 Replies
Login or Register to Ask a Question