2

I'm learning sed and I'm currently stuck with the following problem:

I want to extract the lines found between ^C's from the following output:

banner exec ^C
This is
the
banner
^C
banner motd ^C
This is
the MOTD
banner
^C
!
line con 0
 exec-timeout 0 0
 privilege level 15
 logging synchronous

I'm using this as a reference, but it doesn't seem like a good idea if you don't know how many lines you will be capturing. What would be the best way to deal with this?

  • which ^Cs are you trying to extract between? When you write ^C do you mean CTRL+C or do you mean literal circumflex and capital C? – gogoud Jul 4 '16 at 16:35
  • I mean literal ^C – Luis E Jul 4 '16 at 16:36
  • As requested by gogoud, you should clarify how you wish it to be extracted as that part is unclear. – Julie Pelletier Jul 4 '16 at 16:41
2

something like this should work, assuming source data is in file test.txt, this uses grep to exclude the lines that contain the '^C':

sed -n '/\^C/,/\^C/p' test.txt|grep -v '\^C'

Output from your example:

This is
the
banner
This is
the MOTD
banner
  • This works as well and is more readable – Luis E Jul 4 '16 at 16:45
1

So, I figured it out. It seems like this does the trick:

sed -n '/\^C/,/\^C/{/\^C/!{p}}' input

Apparently you can run sed between two matched regexes, so I just put the same regex as beginning and end /\^C/,/\^C/ and then printed the lines, skipping the last line containing that same pattern.

I don't know if this is the best method, but it seems to work.

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.