printing a text until a keyword is found


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting printing a text until a keyword is found
# 1  
Old 02-01-2012
printing a text until a keyword is found

Hi,
here's the problem:
Code:
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
Code:
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 either cut out the whole line, (including earth), or it will just cut out mars. But I need to print the text just before mars.

Cheers!
# 2  
Old 02-01-2012
Code:
  cat inputfile | tr '\n' '|' | sed "s/\(.*\)mars.*/\1/" | tr '|' '\n'

Or
Code:
sed -n '1,/mars/p' inputfile | sed "s/\(.*\)mars.*/\1/"


Last edited by balajesuri; 02-01-2012 at 09:01 PM..
# 3  
Old 02-01-2012
Quote:
Originally Posted by balajesuri
Code:
sed -n '1,/mars/p' inputfile | sed "s/\(.*\)mars.*/\1/"

Only one sed is really necessary:

Code:
sed 's/mars.*//; T; q;' input-file

If a substitution isn't made the T command causes the rest of the script to be skipped. When a substitution is made, the q (quit) is executed. By default, the contents of the buffer are printed when the end of the script is reached, and before the process terminates because of the 'q.' So, lines without 'mars' are printed in their entirety, and lines with mars are printed with mars, and all tokens that follow, deleted.

If you want to delete the whitespace between the previous token and 'mars,' a small tweek is needed:

Code:
sed 's/[ \t][^ \t]*mars.*//; T; q;' input-file

These 2 Users Gave Thanks to agama For This Post:
# 4  
Old 02-02-2012
solution found

Hi everyone,
thank you all so much for the help so far.

I tested all of the versions but only
Code:
 cat inputfile | tr '\n' '|' | sed "s/\(.*\)mars.*/\1/" | tr '|' '\n'

seemed to work properly.

Code:
sed 's/[ \t][^ \t]*mars.*//; T; q;' input-file

Didn't compile at all because it couldn't find the commands "T" and "q".

Moreover, the first oneliner above did not provide the desired results with special cases, when the keyword is in the file multiple times, like:
Code:
"jupiter
mars
mars
sun
earth"

The desired result should be
Quote:
"jupiter"
But the actual result is
Code:
"jupiter
mars"

since sed only takes the last keyword and not the first. I don't understand why though, because I thought sed starts from the beginning of the file / line and then executes the substitution with the first hit. Can anyone help?

Cheers!

EDIT:
I think I found a valid solution. It is ugly and big but it works .. if anyone has a better solution, please let me know.
Here it is:
Code:
sed -n "1,/$keyword/p" $file | tr '\n' '|' | sed "s/\(.*\)$keyword.*/\1/" | tr '|' '\n'

Cheers!

Last edited by icantfindauser; 02-02-2012 at 11:47 AM.. Reason: typo
# 5  
Old 02-02-2012
Quote:
Originally Posted by icantfindauser
H
Code:
sed 's/[ \t][^ \t]*mars.*//; T; q;' input-file

Didn't compile at all because it couldn't find the commands "T" and "q".

Ok, guessing you're on FreeBSD or a Sun box....


Try this -- still beats multiple translate commands in terms of efficiency:


Code:
sed '/mars/{s/[ \t][^ \t]*mars.*//; q;}' input-file

# 6  
Old 02-02-2012
Hi agama,

yea sorry not for specifying, I'm on a MBP (MacBook Pro) with underlying Darwin.
Your command works pretty well besides the fact, that it deletes everything after the last appearance of the keyword (mars), like I described in one of my earlier posts.
For example:
Code:
"jupiter
earth
sun
mars
mars
saturn"

With your code, the output would be:
Code:
"jupiter
earth
sun
mars"

But it should be:
Code:
"jupiter
 earth
 sun"

Cheers!

Last edited by icantfindauser; 02-02-2012 at 05:47 PM.. Reason: typo
# 7  
Old 02-02-2012
Quote:
Originally Posted by agama
Ok, guessing you're on FreeBSD or a Sun box....[..]
T is a GNU extension, so it will only work with GNU sed..

Try:
Code:
sed '/mars/{s/[ \t]*mars.*/"/;q;}' infile

but the " will be on the new line in your last example...
Otherwise try:
Code:
awk '{sub(/[[:space:]]*mars.*/,"\"")}1' RS= infile


Last edited by Scrutinizer; 02-02-2012 at 07:35 PM..
This User Gave Thanks to Scrutinizer For This Post:
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. 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

4. 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

5. 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

6. Shell Programming and Scripting

Printing the line number of first column found

Hello, I have a question on how to find the line number of the first column that contains specific data. I know how to print all the line numbers of those columns, but haven't been able to figure out how to print only the first one that is found. For example, if my data has four columns: 115... (3 Replies)
Discussion started by: user553
3 Replies

7. Shell Programming and Scripting

Remove keyword found in title

Hello All, I have a bunch of files that have the following format, where the title is INPUT.txt and contains the following text: INPUT-FILLER1 204 INPUT-FILLER2 FILLER6-INPUT 5 FILLER-INPUT I want to go through the directory and remove the keyword INPUT. For example, my output would be... (5 Replies)
Discussion started by: jl487
5 Replies

8. Shell Programming and Scripting

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: 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.... (5 Replies)
Discussion started by: frankbullitt78
5 Replies

9. Shell Programming and Scripting

read the first 10 lines below a keyword found by grep

Hello Everyone, i need to read specific number of lines ( always serialized ; i.e from 10 to 20 or from 34 to 44 ) in a file , where the first line is found by grep 'ing a keyword. example file.txt ------------------------------------------------------------------ --header this is the... (7 Replies)
Discussion started by: alain.kazan
7 Replies

10. 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
Login or Register to Ask a Question