Search a pattern and add new line below


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search a pattern and add new line below
# 1  
Old 03-30-2012
Search a pattern and add new line below

Hi,

I have 2 files like below.

File A:

apple
mango

File B:

start
abc
def
apple
ghi
end
start
cba
fed
mango
ihg
end

Now i need to search the contents of File A in File B and add a new line "is a fruit" where ever the content matches. So my required output will be like below.

start
abc
def
apple
is a fruit
ghi
end
start
cba
fed
mango
is a fruit
ihg
end

Please help me out in writing a shell script for this.
# 2  
Old 03-30-2012
Hi

Code:
awk 'NR==FNR{a[$0];next}{print;if($0 in a){print "is a fruit";}}' FileA FileB

Guru.
# 3  
Old 03-30-2012
Hi Guru,

Thanks much for the reply !!!

I tried with the following script

Code:
#! /bin/ksh
for i in `cat FileA.txt`
do
sed "/$i/a\is a fruit" FileB.txt >> FileC.txt
done

This adds the line but it added the contents of FileB twice to the new file FileC. So my output came is like below.

start
abc
def
apple
is a fruit
ghi
end
start
cba
fed
mango
ihg
end
start
abc
def
apple
ghi
end
start
cba
fed
mango
is a fruit
ihg
end

Can you help me out in sorting this out ?

Last edited by Franklin52; 03-30-2012 at 03:51 AM.. Reason: Please use code tags for code and data samples, thank you
# 4  
Old 03-30-2012
for me works this:
Code:
while read Zeile
do 
 echo $Zeile
 if `grep -q $Zeile FileA`
 then 
   echo "this is a fruit"
 fi
done < FileB

or as i used it:
Code:
while read Zeile; do echo $Zeile; if `grep -q $Zeile FileA`; then echo "this is a fruit"; fi; done < FileB

# 5  
Old 03-30-2012
No double posting!
Thread closed continue here:
https://www.unix.com/unix-dummies-que...g-pattern.html
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search a pattern in a line and remove another pattern

Hi, I want to search a pattern in a text file and remove another pattern in that file. my text file look like this 0.000000 1.970000 F 303 - 1.970000 2.080000 VH VH + 2.080000 2.250000 VH VH + 2.250000 2.330000 VH L - 2.330000 2.360000 F H + 2.360000 2.410000 L VL - 2.410000 ... (6 Replies)
Discussion started by: sreejithalokkan
6 Replies

2. Shell Programming and Scripting

Search for a pattern that is in the middle of the line.

Hello, How do I use sed to search for a pattern in the middle of a line and delete the whole line. The command I have is as below. sed -i '/soft nofile/ d' /etc/security/limits.conf I have some *'s and spaces in front of the string. With the above command I'm not able to delete it. Any... (3 Replies)
Discussion started by: jawaugh
3 Replies

3. Shell Programming and Scripting

How to search pattern and add that pattern in next line

Hi All, I am new to shell scripting and need help in scripting using CSH. Here is what I am trying to so, 1. Search a specific string e.g. "task" from "task (input1, out1)". 2. Extract the arguements "input1" and "out1" 3. Add them in separate lines below. eg. "int input1" , " integer out1" ... (7 Replies)
Discussion started by: deshiashish
7 Replies

4. Shell Programming and Scripting

search more than one pattern with perl on same line

Hi friends, I want to search for some hex error codes in some files. After the hex error code is found, the occurences would be counted. Afterwards the found hex errorcode would be cat into a separate file. Here is my code: #!/usr/bin/perl use File::Basename; my $find = $ARGV; my... (2 Replies)
Discussion started by: sdohn
2 Replies

5. Shell Programming and Scripting

search a pattern and replace the whole line

Hi All, I have a requirement where I have to find a pattern in a file and comment the whole line containing the search pattern. Any ideas in shell is welcome. Thanks in advance, Regards, Arun (3 Replies)
Discussion started by: arun_maffy
3 Replies

6. UNIX for Dummies Questions & Answers

grep line pattern search

Hello everyone, I have been trying to get a list of all files containing a line of this type: };#followed by anything with any spaces (0 or more or 0 or more tabs) before the } and between each of the characters. I have been trying this : grep '*}*;*#*' *.c but I have not been fully... (1 Reply)
Discussion started by: gio001
1 Replies

7. Shell Programming and Scripting

Print the line within the search pattern

Hi Guys, I had file as typedef struct { char TrailerType1; char TrailerTxt1; }Trailer; typedef struct { char PfigMoneyType; char PfigMoneyvalue; }PfigMoney; i need to print the lines within the search pattern. if i give the search pattern as... (3 Replies)
Discussion started by: manosubsulo
3 Replies

8. UNIX for Dummies Questions & Answers

modify a particular pattern starting from second line of the search pattern

Hi, I think you ppl did not get my question correctly, let me explain I have 1.txt with following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433 ** ** ** In file 2.txt I have the following entries as shown: ... (1 Reply)
Discussion started by: imas
1 Replies

9. UNIX for Dummies Questions & Answers

modify a particular pattern starting from second line of the search pattern

Hi, I am new to this forum and i would like to get help in this issue. I have a file 1.txt as shown: apple banana orange apple grapes banana orange grapes orange .... Now i would like to search for pattern say apple or orange and then put a # at the beginning of the pattern... (2 Replies)
Discussion started by: imas
2 Replies

10. Shell Programming and Scripting

Search for a pattern in part of the line

Hi guys, I need to search for a particular pattern within the first 5 chars of the line. If the pattern is found then output the whole line. Unfortunately grep only searches the whole line For example if i am searching for aaaaa in these lines aaaaabbbbbccccc bbbbbcccccaaaaa grep... (6 Replies)
Discussion started by: pineapples
6 Replies
Login or Register to Ask a Question