Unix/Linux Go Back    


Shell Programming and Scripting 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

Shell Programming and Scripting


Tags
awk, replace, search, sed

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 07-22-2008   -   Original Discussion by dovetail
dovetail's Unix or Linux Image
dovetail dovetail is offline
Registered User
 
Join Date: Jul 2008
Last Activity: 28 October 2008, 10:54 AM EDT
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Unix or Linux 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!
Sponsored Links
    #2  
Old Unix and Linux 07-22-2008   -   Original Discussion by dovetail
jim mcnamara's Unix or Linux Image
jim mcnamara jim mcnamara is online now Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 22 May 2018, 9:42 AM EDT
Location: NM
Posts: 11,381
Thanks: 614
Thanked 1,158 Times in 1,067 Posts
How do you know which ip address in the second file goes with a given T???CLK???? pattern in file1?
Sponsored Links
    #3  
Old Unix and Linux 07-22-2008   -   Original Discussion by dovetail
dovetail's Unix or Linux Image
dovetail dovetail is offline
Registered User
 
Join Date: Jul 2008
Last Activity: 28 October 2008, 10:54 AM EDT
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
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 Unix and Linux 07-23-2008   -   Original Discussion by dovetail
summer_cherry's Unix or Linux Image
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
 
Join Date: Jun 2007
Last Activity: 11 November 2016, 3:44 AM EST
Location: Beijing China
Posts: 1,305
Thanks: 0
Thanked 26 Times in 26 Posts
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..
Sponsored Links
    #5  
Old Unix and Linux 07-23-2008   -   Original Discussion by dovetail
era's Unix or Linux Image
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
 
Join Date: Mar 2008
Last Activity: 28 March 2011, 6:41 AM EDT
Location: /there/is/only/bin/sh
Posts: 3,653
Thanks: 0
Thanked 11 Times in 9 Posts
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.
Sponsored Links
    #6  
Old Unix and Linux 07-23-2008   -   Original Discussion by dovetail
dovetail's Unix or Linux Image
dovetail dovetail is offline
Registered User
 
Join Date: Jul 2008
Last Activity: 28 October 2008, 10:54 AM EDT
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
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....
Sponsored Links
    #7  
Old Unix and Linux 07-23-2008   -   Original Discussion by dovetail
dovetail's Unix or Linux Image
dovetail dovetail is offline
Registered User
 
Join Date: Jul 2008
Last Activity: 28 October 2008, 10:54 AM EDT
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
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....
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
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
grep -f file1 file2 vijay_0209 Shell Programming and Scripting 7 03-05-2009 04:48 AM
Awk Compare File1 File2 on f2 RacerX Shell Programming and Scripting 4 10-27-2008 09:50 AM



All times are GMT -4. The time now is 10:05 AM.