Replacing grepped text.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing grepped text.
# 1  
Old 09-20-2011
Replacing grepped text.

Dear Friends,
I need your help once again.
I have a flat file from which I have grepped something and kept in a separate file for some reason. I want to replace these grepped srtings with the strings in another file.

E.g.

Actual_file.txt
COLOR: 33179 Lakme SEPT2011
Lot No: BR25324

COLOR: 93389 Ravish AUG2011
Lot No: 5443-SV

COLOR: 33288 Lakme AUG2011
Lot No: BR25324

COLOR: 43570 Lakme NOV2010
Lot No: BR25324

COLOR: 33479 Maybilline SEPT2011
Lot No: MHDTT


Grepped_file.txt
COLOR: 33179 Lakme SEPT2011
COLOR: 93389 Ravish AUG2011
COLOR: 33288 Lakme AUG2011
COLOR: 43570 Lakme NOV2010
COLOR: 33479 Maybilline SEPT2011


Text_to_be_replaced_by.txt
Maroon: 33179 Lakme SEPT2011
Lavender: 93389 Ravish AUG2011
Olive Brown: 33288 Lakme AUG2011
Tan: 43570 Lakme NOV2010
Night Pink: 33479 Maybilline SEPT2011



Final_expected_output.txt
Maroon: 33179 Lakme SEPT2011
Lot No: BR25324

Lavender: 93389 Ravish AUG2011
Lot No: 5443-SV

Olive Brown: 33288 Lakme AUG2011
Lot No: BR25324

Tan: 43570 Lakme NOV2010
Lot No: BR25324

Night Pink: 33479 Maybilline SEPT2011
Lot No: MHDTT
# 2  
Old 09-20-2011
you posted more than 100 posts in this forum. But still you are not following the code tags Smilie

Code:
 
$ ./test.sh 
Maroon: 33179 Lakme SEPT2011
Lot No: BR25324
Lavender: 93389 Ravish AUG2011
Lot No: 5443-SV
Olive Brown: 33288 Lakme AUG2011
Lot No: BR25324
Tan: 43570 Lakme NOV2010
Lot No: BR25324
Night Pink: 33479 Maybilline SEPT2011
Lot No: MHDTT
bash-3.00$ cat test.sh
while read line
do 
word1=$(echo $line|cut -d: -f1); 
word2=$(echo $line | cut -d: -f2); 
nawk -v word1="$word1" -v word2="$word2" ' $0~word2 {print word1 ":" word2;getline;print $0}' test
done < replace

$ cat replace
Maroon: 33179 Lakme SEPT2011
Lavender: 93389 Ravish AUG2011
Olive Brown: 33288 Lakme AUG2011
Tan: 43570 Lakme NOV2010
Night Pink: 33479 Maybilline SEPT2011

# 3  
Old 09-20-2011
Dear Itkamaraj,
Sorry for not following tags.

The solution that you have given can not be understood by us :-(
Can you please simplify it?
# 4  
Old 09-20-2011
If the records in the file are ordered and always same :

Code:
$ cat x3
Maroon: 33179 Lakme SEPT2011
Lavender: 93389 Ravish AUG2011
Olive Brown: 33288 Lakme AUG2011
Tan: 43570 Lakme NOV2010
Night Pink: 33479 Maybilline SEPT2011
$
$
$ 
$ cat x4
COLOR: 33179 Lakme SEPT2011
Lot No: BR25324
 
COLOR: 93389 Ravish AUG2011
Lot No: 5443-SV
 
COLOR: 33288 Lakme AUG2011
Lot No: BR25324
 
COLOR: 43570 Lakme NOV2010
Lot No: BR25324
 
COLOR: 33479 Maybilline SEPT2011
Lot No: MHDTT
$ 
$ 
$ 
$ awk 'BEGIN {FS =":"; OFS=":"} NR==FNR {_[$2] = $1; next} /COLOR/ {print _[$2],$2} ! /COLOR/ {print $0}' x3 x4 
Maroon: 33179 Lakme SEPT2011
Lot No: BR25324
 
Lavender: 93389 Ravish AUG2011
Lot No: 5443-SV
 
Olive Brown: 33288 Lakme AUG2011
Lot No: BR25324
 
Tan: 43570 Lakme NOV2010
Lot No: BR25324
 
Night Pink: 33479 Maybilline SEPT2011
Lot No: MHDTT
$
$

# 5  
Old 09-20-2011
Hi friend,

Thanks for providing alternate solution
I tried your solution and now the final output file (x4) has the containt of input tile i.e. x3. :-(
Lot No: is missing.
In short the solution has only copied countaints of x3 to x4.
Please guide. :-(

---------- Post updated at 01:37 PM ---------- Previous update was at 01:30 PM ----------

Sorry Anchal... My mistake... your solution is working fine for us.

Thank you very very much
God bless you
Take care
Bye
Anu.
# 6  
Old 09-20-2011
What u didnt understand ?

Code:
 
-------------Actual File--------------------
$ cat actual.txt 
COLOR: 33179 Lakme SEPT2011
Lot No: BR25324
COLOR: 93389 Ravish AUG2011
Lot No: 5443-SV
COLOR: 33288 Lakme AUG2011
Lot No: BR25324
COLOR: 43570 Lakme NOV2010
Lot No: BR25324
COLOR: 33479 Maybilline SEPT2011
Lot No: MHDTT
--------------Replace File----------------------
$ cat replace.txt 
Maroon: 33179 Lakme SEPT2011
Lavender: 93389 Ravish AUG2011
Olive Brown: 33288 Lakme AUG2011
Tan: 43570 Lakme NOV2010
Night Pink: 33479 Maybilline SEPT2011
--------------Script----------------------------
$ cat test.sh
while read line
do 
word1=$(echo $line|cut -d: -f1); 
word2=$(echo $line | cut -d: -f2); 
nawk -v word1="$word1" -v word2="$word2" '$0~word2{print word1 ":" word2;getline;print $0}' actual.txt 
done < replace.txt
---------Output------------------------------------
$ ./test.sh 
Maroon: 33179 Lakme SEPT2011
Lot No: BR25324
Lavender: 93389 Ravish AUG2011
Lot No: 5443-SV
Olive Brown: 33288 Lakme AUG2011
Lot No: BR25324
Tan: 43570 Lakme NOV2010
Lot No: BR25324
Night Pink: 33479 Maybilline SEPT2011
Lot No: MHDTT

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Print the grepped string alongwith the filename

We have C shell and we are executing the below script: #!/bin/csh -f if ($#argv != 2) then echo "Usage $0 DirecotryPath Inputfilename" exit 1 endif set dir=$1 set fname=$2 echo $dir foreach line ( `cat $fname` ) echo \ ======================================== >>... (2 Replies)
Discussion started by: donisback
2 Replies

2. UNIX for Dummies Questions & Answers

passing grepped date var into code

Hi Everyone, I am a bit new at learning this bash syntax. I have a problem at work that needs to be addressed. We find we are spending quite a bit of time killing old processes created by oracle replication that have been restarted later in the week. Because the replication takes time to... (3 Replies)
Discussion started by: bdby
3 Replies

3. Shell Programming and Scripting

How to get a number from a grepped sentence of a file?

I want get a number(ID) from a sentence which has been grepped from file using error number. For Example: #!/bin/ksh echo "Enter RRS ID: " read rrs echo "Enter error number:" read err scp -pr ptc-avdbamdw102:/home/icsprd/M3logs/Accurate/logs/corp_post/$rrs.*.err.txt $HOME/daemon_mail/... (7 Replies)
Discussion started by: JayDoshi
7 Replies

4. Shell Programming and Scripting

Replacing text on every third line

I have file like this "copy table_name from filea.txt on node replace delimiter '|';" "copy table_name from fileb.txt on node replace delimiter '|';" "copy table_name from filec.txt on node replace delimiter'|';" "copy table_name from filee.txt on node replace delimiter '|';" "copy... (1 Reply)
Discussion started by: nnani
1 Replies

5. Shell Programming and Scripting

Unix Script to email grepped results

I am following the tread number 81556 to grep a file and send the results in email. #/bin/bash /bin/grep -i 'invite sip' /var/log/asterisk/full if echo "found" | mail -s 'invite sip' mail@gmail.com When I just build it to grep and mail, it works fine. However, the if statement causes... (4 Replies)
Discussion started by: klysdale
4 Replies

6. Shell Programming and Scripting

Puzzle: file name grepped from text file yields "no such file"

Dear all, In a bash script, I grep a filename from an UTF8 encoded file: LIST=`grep ^source $FILE | tr "\t" " " | cut -d " " -f 2 | sed -e 's,~,\$HOME,g'` The result is # echo $LIST $HOME/.mail_aliases_seminaire_MMMG Then I try to access it: #ls $LIST ls: cannot access... (3 Replies)
Discussion started by: josce
3 Replies

7. Shell Programming and Scripting

Matching and replacing text

Im new to using perl scripting, and i was wondering if anyone could help me, I need to create a cgi file that when it runs it opens a file looks through all the words, and replace the word hello with goodbye, ive been looking around and someone said to use an If statement, but ive found no other... (2 Replies)
Discussion started by: xzen123
2 Replies

8. Shell Programming and Scripting

Replacing Text in Text file

Hi Guys, I am needing some help writing a shell script to replace the following in a text file /opt/was/apps/was61 with some other path eg /usr/blan/blah/blah. I know that i can do it using sed or perl but just having difficulty writing the escape characters for it All Help... (3 Replies)
Discussion started by: cgilchrist
3 Replies

9. Shell Programming and Scripting

Replacing text

I was using the following code to replace the path names and it works when it is echo "$PWD/$f" | sed -e 's/^.*chris\.domain\.com/chris.domain.com/' IN fact it works great However I tried to incorporate a variable echo "$PWD/$f" | sed -e... (3 Replies)
Discussion started by: chrchcol
3 Replies

10. Shell Programming and Scripting

replacing text

hey how can i change part of a file i hve to do in a masses so mv or cp is not practical. I have to change xxx_rrr to xxx_yyy pls help thank (2 Replies)
Discussion started by: ajaya
2 Replies
Login or Register to Ask a Question