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.

4
  • 1
    Welcome! Can you provide a sample text and the expected output? Commented Feb 20, 2020 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
    Commented Feb 20, 2020 at 21:51
  • Can the word appear more than once on the line you are interested in?
    – AdminBee
    Commented Feb 21, 2020 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
    Commented Feb 21, 2020 at 10:49

2 Answers 2

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...
3
  • You need a semi colon after the q with bsd sed .
    – fd0
    Commented Feb 20, 2020 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
    Commented Feb 21, 2020 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
    Commented Feb 21, 2020 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.).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .