0

Here is a script that prints the text of the full line that 'word' was on after 'word' is removed, selecting all occurences of 'word':

sed -ne 's/word//p'  file.txt >> newfile.txt

How can I print only text of the line after the first occurence of 'word', and remove 'word' of the printed line?

I am using OSX.

Here is a sample text:

Blalala 

'Omfoem From

balanf

word I want this output

word Not this output

Omfoem527

Here is the output I expect:

 I want this output

UPDATE: Here is the script I have, which does what I need:

sed -n 's/^.*word //p'  file.txt > temp.txt
sed -n '1p' temp.txt >> newfile.txt
rm temp.txt

First line prints the text of the full line that 'word' was on after 'word' is removed, selecting all occurences of 'word' in the file temp.txt. I have also added ^.* in case there is any other text before 'word'.
Second line prints only the first line of the file temp.txt into newfile.txt Third line removes temp.txt file.

Might not be the most efficient but it works.

  • 1
    Welcome! Can you provide a sample text and the expected output? – schrodigerscatcuriosity Feb 20 at 19:32
  • 2
    That doesn't print the text after word, it prints the text of the full line that word was on after word is removed. It's extremely unclear what you're trying to do, please do provide useful (i.e. with multiple "word"s per line and multiple such lines per file) sample input and expected output. – Ed Morton Feb 20 at 21:51
  • Can the word appear more than once on the line you are interested in? – AdminBee Feb 21 at 10:18
  • @AdminBee I haven't come across this at the moment in my text, tipically I only have the pattern 'word' once in a line. – Charlie Feb 21 at 10:49
2

Try this,

sed -n '/word/{n;p;q}' file
  • n -> Read/append the next line of input into the pattern space.
  • p -> Print the current pattern space.
  • q -> Immediately quit the sed script without processing any more input, for only first occurrence...
| improve this answer | |
  • You need a semi colon after the q with bsd sed . – fd0 Feb 20 at 20:03
  • Thanks for all the explainations, very useful. The following script works: sed -n '/word/{n;p;q;}' file But is there a way to only output the text after 'word', i.e. excluding 'word' to get 'I want this output' only as an output (see sample text and expected output I added). – Charlie Feb 21 at 9:57
  • @Charlie can u roll back this question and ask a new question... Since there is a huge difference between those two... – Siva Feb 21 at 10:10
0

Assuming you only process one file, AND that the pattern can occur only once on the relevant line, the following will work:

awk -F"word" 'NF>1 {print $2; exit}' file

This will use the pattern "word" as field separator and check if there is more than one such "field". If so, print the second field which amounts to the text after word and stop the execution.

If you want to process more than one file, you can use

awk -F"word" 'NF>1 {print $2; nextfile}' file1 file2 ...

but this will be less portable (doesn't work on mawk, e.g.).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.