👤
Home Man
Search
Today's Posts
Register

BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

replacing text in file1 with list from file2

Tags
awk, replace, search, sed

👤 Login to reply

 
Thread Tools Search this Thread
# 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 03: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 to reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk to search field2 in file2 using range of fields file1 and using match to another field in file1 cmccabe Shell Programming and Scripting 6 12-22-2016 09:55 AM
Compare file1 and file2, print matching lines in same order as file1 pathunkathunk UNIX for Dummies Questions & Answers 4 02-10-2015 01:48 AM
Replacing first field of file2 with the second filed of file1 for matching cases ailnilanjan Shell Programming and Scripting 6 11-06-2014 05:14 AM
Pattern Matching & replacing of content in file1 with file2 siramitsharma Shell Programming and Scripting 4 03-09-2013 05:59 AM
Search within file1 numbers from list in file2 Ophiuchus Shell Programming and Scripting 6 09-08-2012 03:25 AM
if matching strings in file1 and file2, add column from file1 to file2 pathunkathunk UNIX for Dummies Questions & Answers 3 07-19-2012 11:02 PM
Delete strings in file1 based on the list of strings in file2 roussine UNIX for Dummies Questions & Answers 7 01-23-2011 10:14 PM
append text from file1 to the end of each line in file2 gc_sw Shell Programming and Scripting 4 12-01-2010 05:18 AM
awk - replacing stings in file1 with column1 in file2 upstate_boy Shell Programming and Scripting 7 11-09-2009 01:32 AM
match value from file1 in file2 myguess21 Shell Programming and Scripting 2 02-21-2008 10:39 AM


All times are GMT -4. The time now is 08:38 AM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
×
UNIX.COM Login
Username:
Password:  
Show Password