copy & replace text

 
Thread Tools Search this Thread
Operating Systems Linux Red Hat copy & replace text
# 1  
Old 04-13-2011
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:
Code:
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

Last edited by Franklin52; 04-13-2011 at 05:15 AM.. Reason: Please use code tags, thank you
# 2  
Old 04-13-2011
Can you post an example of both files and the desired output?

Please use code tags.
# 3  
Old 04-13-2011
Hi ,

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

Code:
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
# 4  
Old 04-13-2011
It is the content of each file:
file1.txt:
Quote:
55:00 99:00 88:00 11:00 77:00
file2.sh: >>"a script file"
Code:
#!/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:

Code:
#!/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 ----------

Quote:
Originally Posted by cgkmal
Hi ,

if your awk script gives you the word to replace in the other file, it could be used as follow:
Code:
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
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.
# 5  
Old 04-13-2011
Try this:
Code:
awk 'NR==FNR{split($0,a); next}/at/{$2=a[++c]}1' file1 file2 > newfile


Last edited by Franklin52; 04-13-2011 at 06:48 AM.. Reason: Adding next statement
This User Gave Thanks to Franklin52 For This Post:
# 6  
Old 04-13-2011
Code:
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 Smilie

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 :

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


Last edited by ctsgnb; 04-13-2011 at 06:42 AM..
These 2 Users Gave Thanks to ctsgnb For This Post:
# 7  
Old 04-13-2011
the command :
Code:
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??
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

find & Replace text using two non-unique delimiters.

I can find and replace text when the delimiters are unique. What I cannot do is replace text using two NON-unique delimiters: Ex., "This html code <text blah >contains <garbage blah blah >. All tags must go,<text > but some must be replaced with <garbage blah blah > without erasing other... (5 Replies)
Discussion started by: bedtime
5 Replies

2. Shell Programming and Scripting

Compare File & Copy Replace if Successful

Hi All, I have written a shell script that creates a backup of my MySQL database. The script performs the following functions: Creates a Backup of the MySQL database Compresses the Backup Copies the Backup to a Remote Server Send an E-Mail displaying the size of the Backup Removes any... (6 Replies)
Discussion started by: SalientAnimal
6 Replies

3. Shell Programming and Scripting

Replace dashes positions 351-357 & 024-043 with 0 & replace " " if exis with 04 at position 381-382

I need to replace dashes (i.e. -) if present from positions 351-357 with zero (i.e. 0), I also need to replace dash (i.e “-“) if present between position 024-043 with zero (i.e. 0) & I replace " " (i.e. 2 space characters) if present at position 381-382 with "04". Total length of record is 413.... (11 Replies)
Discussion started by: lancesunny
11 Replies

4. Shell Programming and Scripting

awk + gsub to search multiple input values & replace with located string + extra text

Hi all. I have the following command that is successfully searching for any one of the strings on all lines of a file and replacing it with the instructed value. cat inputFile | awk '{gsub(/aaa|bbb|ccc|ddd/,"1234")}1' > outputFile This does in fact replace any occurrence of aaa, bbb,... (2 Replies)
Discussion started by: dazhoop
2 Replies

5. Shell Programming and Scripting

Copy & Append text

Hi, I have a huge text file that contains contents like below. echo 2VPMUM1CMP01_2011-05-10_18_CPU_Stats_1.txt awk -F, '{sub ("%","",$8);sum+=100-$8;if (100-$8>max){max=100-$8}}END{printf "max=%.1f%\navg=%.1f%\n",max,sum/NR}' echo 2VPMUM1CMP01_2011-05-11_10_CPU_Stats_1.txt awk -F,... (2 Replies)
Discussion started by: SunilB2011
2 Replies

6. Shell Programming and Scripting

Replace & sign to &amp word

Hi, I have text file abc.txt. In this file, I have the following data. Input: Mr Smith &amp Mrs Smith Mr Smith &apos Mrs Smith Mr Smith & Mrs Smith Mr Smith& Mrs Smith Mr Smith &Mrs Smith Output: Mr Smith &amp Mrs Smith Mr Smith &apos Mrs Smith Mr Smith &amp Mrs Smith Mr Smith&amp... (4 Replies)
Discussion started by: naveed
4 Replies

7. Homework & Coursework Questions

[Scripting]Find & replace using user input then replacing text after

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: (o) Checkout an auto part: should prompt the user for the name of the auto part and borrower's name: Name:... (2 Replies)
Discussion started by: SlapnutsGT
2 Replies

8. Shell Programming and Scripting

How do I search first&second string & copy all content between them to other file?

Hi All, How do I search first string & second string and copy all content between them from one file to another file? Please help me.. Thanks In Advance. Regards, Pankaj (12 Replies)
Discussion started by: pankajp
12 Replies

9. Shell Programming and Scripting

find, copy and replace text in bash or sh

here is my prob .. i have a very large text files and i need to locate specific lines, copy them and then replace a single word in the replaced text example find all lines that contain '/etc', copy the line immediately below (not at the end of the file) and then replace '/etc' with '/root'... (1 Reply)
Discussion started by: jmvbxx
1 Replies

10. Shell Programming and Scripting

XML Copy & replace problem

I probably could have done this at one time, but, the years and no need has left my scripting skills lacking and I'm unable to work this problem out. https://www.unix.com/images/smilies/frown.gif :( Using Linux, have a great many xml files in which there may be multiple occurrence of a line of... (13 Replies)
Discussion started by: xenixuser
13 Replies
Login or Register to Ask a Question