Add lines after searching for a pattern


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Add lines after searching for a pattern
# 1  
Old 03-30-2012
Add lines after searching for a pattern

Hi,

I have 2 files like below.

File A:

Code:
apple
mango

File B:

Code:
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.

Code:
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.

Moderator's Comments:
Mod Comment Please use code tags

Last edited by Scrutinizer; 03-30-2012 at 06:11 AM..
# 2  
Old 03-30-2012
Code:
while read first
do 
  while read second
  do
     echo $second >> newfile.txt
     [ "$second" = "$first" ] && echo "is a fruit" >> newfile.txt
  done<secondfile.txt
done<firstfile.txt


Last edited by Scrutinizer; 03-30-2012 at 06:14 AM.. Reason: Don't forget code tags, thank you
# 3  
Old 03-30-2012
awk

Hi,

Try this one,

Code:
awk 'BEGIN{while(getline line <"file1"){pat=pat"|"line;}sub(/^\|/,"",pat);}$0~pat{$0=$0"\nis a fruit";}1' file

Cheers,
RangaSmilie
# 4  
Old 03-30-2012
Try this one using perl

Code:
my %fruit_name;
open(FRUIT,File_A) or die "Cannot open File_A File \n";
open(REC,File_B) or die "Cannot open File_B File \n";
while(my $friut_line = <FRUIT>) {
 $friut_line =~s/[\n\r]//g;
 $fruit_name{$friut_line} = 'is a fruit';
}
while(my $line=<REC>) {
$line =~s/[\n\r]//g;
print "$line\n";
 if (exists $fruit_name{$line} ) {
  print "$fruit_name{$line}\n";
 }
}

Thank you,
Senthil
# 5  
Old 03-30-2012
Code:
awk 'NR==FNR{A[$1];next}1;$1 in A{print "is a fruit"}' fileA fileB

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

2. Shell Programming and Scripting

Searching for pattern and remove the lines

Hi , I want to remove the specific pattern and remove those lines from file using shell script. i want to remove these lines <?xml version='1.0' encoding='UTF-8'?> <row_set> </row_set> my input file has content like this. file name: sample.xml <?xml version='1.0'... (4 Replies)
Discussion started by: nukala_2
4 Replies

3. Shell Programming and Scripting

Searching for a pattern and extracting records related to that pattern

Hi there, Looking forward to your advice for the below: I have a file which contains 2 paragraphs related to a particular pattern. I have to search for those paragraphs from a log file and then print a particular line from those paragraphs. Sample: I have one file with the fixed... (3 Replies)
Discussion started by: danish0909
3 Replies

4. Shell Programming and Scripting

pattern searching

Hi, Can you please help me out here? I am trying develop a search pattern to extract certain words from the two strings below. I want to extract ericsson_msc_live from the 2 strings and then the date, which is a part of the filename just before the .jar extension. ... (19 Replies)
Discussion started by: danish0909
19 Replies

5. UNIX for Dummies Questions & Answers

searching pattern in VI

in my file i have somthing likre kpk_12 kpk_1 kpk_1.tcl kpk_3.tcl kpk kpk kpk i want search only kpk i am using this cmd /kpk ...results it is showing all . any cmd is ther other then this to search exactword in this example kpk it shoulsnot show kpk_* etc Thanks in Advance ... (2 Replies)
Discussion started by: prakumar
2 Replies

6. Shell Programming and Scripting

awk to find pattern and add lines

My file goes like this: SID_LIST_HOSTNAME_LISTENER_3 = (SID_LIST = (SID_DESC = (SID_NAME = ORA0008) (ORACLE_HOME = /opt/oracle/product/ORA0008) (ENVS = "LD_LIBRARY_PATH=/opt/oracle/product/ORA0008/lib") ) (SID_DESC = (SID_NAME = ORA0007) ... (4 Replies)
Discussion started by: jpsingh
4 Replies

7. Shell Programming and Scripting

Searching for a pattern

How do I search for a pattern - N/A in a particular column using awk? (11 Replies)
Discussion started by: rabiu
11 Replies

8. Shell Programming and Scripting

Searching a pattern in file and deleting th ewhole line containing the pattern

Hi All, Please can someone assist in the script I have made that searches a pattern in a file and delete the whole line containing the pattern. #!bin/sh # The pattern that user want to add to the files echo "Enter the pattern of the redirect" read value # check if the user has... (1 Reply)
Discussion started by: Shazin
1 Replies

9. Shell Programming and Scripting

searching for a pattern

can anybode tell me ? I want to search for a pattern present in a whole directory and subdirectories's files containg " crat" I tried grep -r "crat" */* ; is it right ? (3 Replies)
Discussion started by: pranabrana
3 Replies

10. Shell Programming and Scripting

Pattern searching pattern in c files

I have a problem in searching a specific pattern in c files. My requirement: I have to find all the division operator in all cfiles. The problem is, the multi line comments and single line comments will also have forward slash in it. Even after avoiding these comments also, if both... (6 Replies)
Discussion started by: murthybptl
6 Replies
Login or Register to Ask a Question