copy & replace text

how can i copy a certain word from a text file then use this word to replace in another text file??
i tried to use something like:

awk '{print "Hit the",$1,"with your",$2}'  /aaa/qqqq.txt > uu.txt

but i can't add an argument to point to the second file which i will replace in.

please help me

Can you post an example of both files and the desired output?

Please use code tags.

Hi ,

if your awk script gives you the word to replace in the other file, it could be used as follow:

word_to_replace=$(awk '{print "Hit the",$1,"with your",$2}'  /aaa/qqqq.txt)

sed "s/pattern/$word_to_replace/g" file2

Hope this helps.

Regards

It is the content of each file:
file1.txt:

file2.sh: >>"a script file"

#!/bin/sh
at 11:11
cp /qq/event.call /var/spool/asterisk/outgoing/
chmod 777 /var/spool/asterisk/outgoing/event.call
at 22:22
cp /qq/event.call /var/spool/asterisk/outgoing/
chmod 777 /var/spool/asterisk/outgoing/event.call
at 33:33
cp /qq/event.call /var/spool/asterisk/outgoing/
chmod 777 /var/spool/asterisk/outgoing/event.call
at 44:44
cp /qq/event.call /var/spool/asterisk/outgoing/
chmod 777 /var/spool/asterisk/outgoing/event.call
at 55:55
cp /qq/event.call /var/spool/asterisk/outgoing/
chmod 777 /var/spool/asterisk/outgoing/event.call

and the outputfile.sh will be:

#!/bin/sh
at 55:00
cp /qq/event.call /var/spool/asterisk/outgoing/
chmod 777 /var/spool/asterisk/outgoing/event.call
at  99:00 
cp /qq/event.call /var/spool/asterisk/outgoing/
chmod 777 /var/spool/asterisk/outgoing/event.call
at 88:00 
cp /qq/event.call /var/spool/asterisk/outgoing/
chmod 777 /var/spool/asterisk/outgoing/event.call
at 11:00 
cp /qq/event.call /var/spool/asterisk/outgoing/
chmod 777 /var/spool/asterisk/outgoing/event.call
at 77:00
cp /qq/event.call /var/spool/asterisk/outgoing/
chmod 777 /var/spool/asterisk/outgoing/event.call

---------- Post updated at 03:52 AM ---------- Previous update was at 03:38 AM ----------

this solution in very close to what i need, but how can i replace the "pattern" in the "sed" command to be a a certain word and liene number??, for more details please see my example post.

Try this:

awk 'NR==FNR{split($0,a); next}/at/{$2=a[++c]}1' file1 file2 > newfile
1 Like
xargs -n1 <file1 | nawk 'NR==FNR{A[++i]=$1}NR!=FNR{if (/^at/) sub($2,A[++j],$0); print $0}' - file2.sh >output.sh

Ok Franklin's code is better :slight_smile:

Just need to take care about the 1 that will make the content of file1 printed as well (what we do not want)
to avoid this (assuming this file1 is just made of 1 line) you can go with :

awk 'NR==FNR{split($0,a);getline}/at/{$2=a[++c]}1' file1 file2 > newfile
2 Likes

the command :

awk 'NR==FNR{split($0,a);getline}/at/{$2=a[++c]}1' file1 file2 > newfile

is very useful but still printing the content of file1 to the output, how can i avoid this??

This way

awk 'NR==FNR{split($0,a)}NR!=FNR{if(/at/)$2=a[++c];print}' file1 file2 >output

or more simply, you can also give a try to

awk 'NR==FNR{split($0,a)}/at/{$2=a[++c]}NR!=FNR' file1 file2 >output
1 Like

Thank u very much