Filter text after keyword


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Filter text after keyword
# 1  
Old 12-15-2010
Question Filter text after keyword

I've got a little problem to solve and can't find a way to solve it. If have text line like the following:

Code:
keyword1: text1 keyword2: text2 keyword3: text3

Now I need a script or command, which gives me the text for the corresponding keyword. The text can consist of only one or more words. Keywords are always one word followed by a :

Thanks for help!
# 2  
Old 12-15-2010
you can start with below Perl code:-

Code:
cat infile.txt
keyword1: text1 keyword2: text2 keyword3: text3

Code:
perl -wlne '
s/(.*):\s+(.*)\s+(.*):\s+(.*)\s+(.*):\s+(.*)// ;
print $1,$2,"\n",$3,$4,"\n",$5,$6 ;
' infile.txt

# 3  
Old 12-15-2010
Modified your input to work with more than one word as text - not pretty but working:
Code:
$> cat infile
keyword1: text1 keyword2: text2 text3 keyword3: text4 text5 text6
$> read I; sed -e 's/ key/\nkey/g' infile| awk -v i=$I '$1 ~ i {$1=""; sub(/^[ \t]*/,""); print}'
keyword2
text2 text3

# 4  
Old 12-15-2010
Code:
$ awk 1 RS=' ' file | paste - -
keyword1:       text1
keyword2:       text2
keyword3:       text3

# 5  
Old 12-15-2010
Code:
 
sed -e 's/[^ ]*:/:FSXXXX&FSXXXX/g' -e 's/FSXXXX /FSXXXX/g' inputFile | awk -F ":FSXXXX" '{print $2, $3, $4, $5, $6, $7}'

Here $2, $4 and $6 are keywords one, two and three.
$3, $5, $7 are corresponding values.
i.e.
keyword1=$(sed -e 's/[^ ]*:/:FSXXXX&FSXXXX/g' -e 's/FSXXXX /FSXXXX/g' inputFile | awk -F ":FSXXXX" '{print $2}')
value1=$(sed -e 's/[^ ]*:/:FSXXXX&FSXXXX/g' -e 's/FSXXXX /FSXXXX/g' inputFile | awk -F ":FSXXXX" '{print $3}')
value2=$(sed -e 's/[^ ]*:/:FSXXXX&FSXXXX/g' -e 's/FSXXXX /FSXXXX/g' inputFile | awk -F ":FSXXXX" '{print $5}')
.
.
For input
Code:
 
fname: abc lname: dsfdf address: djfkhk djfhdj dsjfdjf

Code:
 
echo $(sed -e 's/[^ ]*:/:FSXXXX&FSXXXX/g' -e 's/FSXXXX /FSXXXX/g' inputFile | awk -F ":FSXXXX" '{print $7}')

will be
Code:
 
djfkhk djfhdj dsjfdjf


Last edited by anurag.singh; 12-15-2010 at 12:07 PM..
# 6  
Old 12-15-2010
Code:
$ a="keyword1: text1 keyword2: text2 keyword3: text3"
$ key=keyword2
$ echo "${a##*$key: }" | sed 's/ .*//'
text2

also
Code:
$ echo "${a##*$key: }" | nawk '$0=$1'
text2

or
Code:
$ echo "${a##*$key: }" | awk '{print$1}'
text2

Code:
sed -n "/$key/{s/.*$key: //;s/ .*//;p;q}" infile

Code:
# cat infile
keyword1: text1 keyword2: text2 keyword3: text3
# read key
keyword2
# sed -n "/$key/{s/.*$key: //;s/ .*//;p;q}" infile
text2


Last edited by ctsgnb; 12-15-2010 at 01:57 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Append a specific keyword in a text file into a new column

All, I have some sample text file(.csv) in the below format. In my actual file there are at least 100K rows. date 03/25/2016 A,B,C D,E,F date 03/26/2016 1,2,3 4,5,6 date 03/27/2016 6,4,3 4,5,6 I require the following output where in the date appeared at different locations need to... (3 Replies)
Discussion started by: ks_reddy
3 Replies

2. Shell Programming and Scripting

Sorting a text file with respect to Function/Keyword

Hello Experts, I am truly a beginner in shell and perl . Need an urgent help with sorting a file. please help. wouldn't mind whether in perl or shell script. Here are the details. ------------------------------------------------------ Input Text file EX:... (9 Replies)
Discussion started by: pradyumnajpn10
9 Replies

3. UNIX for Dummies Questions & Answers

Filter records in a huge text file from a filter text file

Hi Folks, I have a text file with lots of rows with duplicates in the first column, i want to filter out records based on filter columns in a different filter text file. bash scripting is what i need. Data.txt Name OrderID Quantity Sam 123 300 Jay 342 498 Kev 78 2500 Sam 420 50 Vic 10... (3 Replies)
Discussion started by: tech_frk
3 Replies

4. Shell Programming and Scripting

Search for a Keyword in file and replace another keyword or add at the end of line

Hi I want to implement something like this: if( keyword1 exists) then check if(keyword2 exists in the same line) then replace keyword 2 with New_Keyword else Add New_Keyword at the end of line end if eg: Check for Keyword JUNGLE and add/replace... (7 Replies)
Discussion started by: dashing201
7 Replies

5. Shell Programming and Scripting

Keyword search/replace for two text files?

What is the best way (bash/awk/sed?) to read in two text files and do a keyword search/replace? file1.txt: San Francisco Los Angeles Seattle Dallas file2.txt: I love Los Angeles. Coming to Dallas was the right choice. San Francisco is fun. Go to Seattle in the summer. ... (3 Replies)
Discussion started by: pxalpine
3 Replies

6. Shell Programming and Scripting

extract lines from text after keyword

I have a text and I want to extract the 4 lines following a keyword! For example if I have this text and the keyword is AAA hello helloo AAA one two three four helloooo hellooo I want the output to be one two three four (7 Replies)
Discussion started by: stekanius
7 Replies

7. Shell Programming and Scripting

printing a text until a keyword is found

Hi, here's the problem: text="hello1 hello2 world earth mars jupiter planet"how do I print the text until it finds the keyword "mars" so that the desired output is output="hello1 hello2 world earth" I have rtfm of sed and I think the problem is, that if I find the word "mars" it will... (7 Replies)
Discussion started by: icantfindauser
7 Replies

8. Shell Programming and Scripting

Extract lines of text based on a specific keyword

I regularly extract lines of text from files based on the presence of a particular keyword; I place the extracted lines into another text file. This takes about 2 hours to complete using the "sort" command then Kate's find & highlight facility. I've been reading the forum & googling and can find... (4 Replies)
Discussion started by: DionDeVille
4 Replies

9. Shell Programming and Scripting

Filter some text

Dear experts, Below i have mentioned two alarms with MAJOR severity, i am intrested only with alarm contains text tre , actually i want to filter out alarm which is highlighted in bold text in some other file for further processing. I need all above three line of the alarm containing text tre... (3 Replies)
Discussion started by: Danish Shakil
3 Replies

10. Windows & DOS: Issues & Discussions

Filter data from text file

Hi All We have got a text file, which has data dumped from 60 tables. From these 60 tables of data we need data from 4 tables only. I tried assigning line numbers to filter out data, but it is not working as intended. below is the sample file ----Table1----- 3,dfs,43,df 4,sd,5,edd... (18 Replies)
Discussion started by: b_sri
18 Replies
Login or Register to Ask a Question