1

I am looking for a sed command to match pattern and copy pattern line and n number of lines after this line. pattern is 01, 02, 03 sequence.

Example:

group-title=01- line1
http://www.yahoo.com
group-title=02- local3
http://www.altavista.com
group-title=01- koko1
http://www.bbc.com

Required output:

group-title=01- line1
http://www.yahoo.com
group-title=01- koko1
http://www.bbc.com

I tried

sed -n "/01- /p" orignal.txt > copyof.txt

but this only copy line which matches pattern. I have to copy the 2nd line also as second line has its link.

Compo
  • 36,585
  • 5
  • 27
  • 39
Rizwan.A
  • 21
  • 7

3 Answers3

2

Use grep:

grep -A1 'group-title=01-' orig.txt > copy.txt

Here, GNU grep uses option:

-A num, --after-context=num : Print num lines of trailing context after each match. See also the -B and -C options.

Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47
2

grep -A is the better way to do this (see Timur Shtatland's answer), but if you really want to do it with sed, here's how:

sed -n '/01- / {p;n;p;}'      # print match + next line
sed -n '/01- / {p;n;p;n;p;}'  # print match + next 2 lines

Explanation: when it finds a matching line, {p;n;p;} will print that line, get the next line, and print that.

Note that this won't notice additional matches within the extra lines being printed. So for example if there are two matching lines in a row, it'll print them but not the next line as you might expect (and grep -A will do).

Gordon Davisson
  • 118,432
  • 16
  • 123
  • 151
  • what if 01-[space] also have "01-[space] line1" , means what if I want to include "" double qoute in my search pattern. any solution acceptable eiter sed or grep, i am using windows – Rizwan.A Mar 09 '23 at 21:55
  • @Rizwan.A The easiest way to include double-quotes in the search pattern is to use single-quotes around the entire argument (I'll edit my answer to show this). Alternately, you can include double-quotes inside a double-quoted argument by escaping them (e.g. `sed -n "/01- \"double-quoted text\"/ {p;n;p;}"`). – Gordon Davisson Mar 09 '23 at 23:37
  • my search pattern like this ```group-title="01- channel1" ``` and I have to search ```group-title="01- *" ```, * means any text after 01- , can you please help me in this specific pattern – Rizwan.A Mar 10 '23 at 11:45
  • @Rizwan.A `sed` uses regular expressions (the "basic" variant by default) rather than wildcard patterns, so the match-anything pattern is `.*` rather than just `*`. You might also want to add `^` at the beginning to require that it start at the beginning of the line. So if I understand right, the command would be `sed -n '/^group-title="01- .*"/ {p;n;p;}'` – Gordon Davisson Mar 10 '23 at 19:22
  • thanks for reply but .* not working , and pattern is not in beginning, its some where in middle of line so ^ is not applicable. thanks. command tried by me with your help in my windows is ```sed -n "/group-title="01- .*"/ {p;n;p;}" sourcefile.txt > destinationfile.txt``` – Rizwan.A Mar 11 '23 at 07:08
  • @Rizwan.A You need to either put single-quotes around the argument (now double-quotes like you have here), or escape the double-quotes inside the pattern. Try `sed -n '/group-title="01- .*"/ {p;n;p;}' sourcefile.txt > destinationfile.txt` – Gordon Davisson Mar 11 '23 at 08:47
  • facing error ```sed: -e expression #1, char 1: unknown command: `'' ``` for information I am using sed on Windows cmd not in Linux/ Unix, windows works with double qoute – Rizwan.A Mar 11 '23 at 11:00
  • @Rizwan.A Oh, I'm afraid I'm not very familiar with Windows CMD; If I'm reading [this](https://ss64.com/nt/syntax-esc.html) right, you might need to use outer double-quotes, and escape the double-quotes in the pattern with `^` instead of `\`. If that doesn't work I'm afraid I just don't know. – Gordon Davisson Mar 11 '23 at 21:00
  • ok its done, lots of thanks for your expert help. I use following to fulfil my need with you help. ```sed -n "/01- \"double-quoted text\"/ {p;n;p;}" ``` and made slight change in it ```sed -n "$!N;/group-title=\"01- News\"/p" sourcefile.txt >> d:\destinationfile.txt``` to fulfill my need I used multiple lines. – Rizwan.A Mar 14 '23 at 06:59
0

Using GNU sed

$ sed -Ez ':a;s/(([^=]*=)01-[^=]*)\20[02-9][^=]*\n/\1/;ta' input_file
group-title=01- line1
http://www.yahoo.com
group-title=01- koko1
http://www.bbc.com
HatLess
  • 10,622
  • 5
  • 14
  • 32