Search a file with keywords


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search a file with keywords
# 1  
Old 02-17-2010
Search a file with keywords

Hi All

I have a file of format
Code:
[EM]
asdf
asf
first
sec

[US]
endi
asdk
rt

[SKP]
123
ferf
dfg
ijglkp
jhgddd

I need help writing an expression that if the file is searched with US as keyword it should return
endi asdk rt
if SKP is searched it should return
123 ferf dfg ijglkp jhgddd

Thank you for help
Abdul
# 2  
Old 02-18-2010
The first argument to the following script is the keyword (EM, US, SKP ... without brackets). The second argument is an optional filename. If provided, the file's contents are searched. If not provided, standard input is read.

Code:
#!/bin/sh

awk 'i{if (!NF) exit; print} /\['"$1"'\]/{i=1}' "${2:--}" | paste -d' ' -s -

# 3  
Old 03-10-2010
search file with keywords

Thanks for reply I am trying to get the output in variable like below , but its throwing an error .

Code:
mail_list=awk 'i{if (!NF) exit; print} /\['"$1"'\]/{i=1}' "${2:--}" | paste -d' ' -s -
echo $mail_list

# 4  
Old 03-10-2010
Wow ... talk about a blast from the past Smilie

Try:
Code:
mail_list=$(awk 'i{if (!NF) exit; print} /\['"$1"'\]/{i=1}' "${2:--}" | paste -d' ' -s -)

If you would like to know more about how that works, read about command substitution @ Shell Command Language

Regards,
Alister
# 5  
Old 03-10-2010
Quote:
Originally Posted by mailabdulbari

Code:
mail_list=$(awk 'i{if (!NF) exit; print} /\['"$1"'\]/{i=1}' "${2:--}" | paste -d' ' -s -)

Code:
mail_list=$(awk 'i{if (!NF) exit; printf $0 FS} /\['"$1"'\]/{i=1}' "${2:--}" )

# 6  
Old 03-10-2010
Thanks guys
# 7  
Old 03-10-2010
Code:
awk -v RS="" -v str="US"  '  $1 ~ str { OFS=" "; $1=""; print } '  file

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search files in directory for keywords using bash

I have ~100 text files in a directory that I am trying to parse and output to a new file. I am looking for the words chr,start,stop,ref,alt in each of the files. Those fields should appear somewhere in those files. The first two fields of each new set of rows is also printed. Since this is on a... (7 Replies)
Discussion started by: cmccabe
7 Replies

2. Shell Programming and Scripting

Perl - use search keywords from array and search a file and print 3rd field when matched

Hi , I have been trying to write a perl script to do this job. But i am not able to achieve the desired result. Below is my code. my $current_value=12345; my @users=("bob","ben","tom","harry"); open DBLIST,"<","/var/tmp/DBinfo"; my @input = <DBLIST>; foreach (@users) { my... (11 Replies)
Discussion started by: chidori
11 Replies

3. Shell Programming and Scripting

search between keywords and make a single line

have a very big file where need to format it like below example file: abcd today is great day; search keyword 'abcd' and append to it all words till we reach ; to make it a single line. output should look like. abcd today is great day; There are many occurrence of such... (2 Replies)
Discussion started by: giri4332
2 Replies

4. UNIX for Advanced & Expert Users

Need to search for keywords within files modified at a certain time

I have a huge list of files in an Unix directory (around 10000 files). I need to be able to search for a certain keyword only within files that are modified between certain date and time, say for e.g 2012-08-20 12:30 to 2012-08-20 12:40 Can someone let me know what would be the fastest way... (10 Replies)
Discussion started by: virtual123
10 Replies

5. Shell Programming and Scripting

How to recursively search for a list of keywords in a given directory?

Hi all, how to recursively search for a list of keywords in a given directory?? for example: suppose i have kept all the keywords in a file called "procnamelist" (in separate line) and i have to search recursively in a directory called "target/dir" if i am not doing recursive search then... (4 Replies)
Discussion started by: neelmani
4 Replies

6. Shell Programming and Scripting

copy range of lines in a file based on keywords from another file

Hi Guys, I have the following problem. I have original file (org.txt) that looks like this module v_1(.....) //arbitrary number of text lines endmodule module v_2(....) //arbitrary number of text lines endmodule module v_3(...) //arbitrary number of text lines endmodule module... (6 Replies)
Discussion started by: kaaliakahn
6 Replies

7. Shell Programming and Scripting

searching keywords in file

hey guys, Hey all, I'm doing a project currently and want to index words in a webpage. So there would be a file with webpage content and a file with list of words, I want an output file with true and false that would show which word exists in the webpage. example: Webpage content... (2 Replies)
Discussion started by: Johanni
2 Replies

8. Shell Programming and Scripting

How to search for keywords in subsequent lines

Hi all, I am looking for a coomand to search for the keywords in susequenct lines. Keyword1 in a line and Keyword2 in the very next line. Once i found the combination ineed to print the lines with patterns and the line above and one below. I am giving an example here: Keywords are :ERROR and... (12 Replies)
Discussion started by: rdhanek
12 Replies

9. Shell Programming and Scripting

Search and replace words between two keywords

Hi, I have a file which contains the following : select * from test where test_id=1; select id from test1, test2 where test_id=1 and test_id=2; select * from test1, test2, test3 where test_id=4 and test2_id where in (select test2_id from test2); select id1, id2 from test ... (6 Replies)
Discussion started by: vrrajeeb
6 Replies
Login or Register to Ask a Question