Tony The Lion
Tony The Lion

Reputation: 63310

sed regular expressions address ranges

I have a txt file that looks something like this

-----------------------------------
           RUNNING PROCESSES
-----------------------------------

ftpd
kswapd
init
etc..

---------------------------------
          HOSTNAME
--------------------------------

mypc.local.com

With sed I want to just get one section of this file. So just the RUNNING PROCESSES section, however I seem to be failing to get my regexp right to do so.

I got this far

sed -n '/^-./,/RUNNING PROCESSES/, /[[:space::]]/p' linux.txt | more

however it keeps complaining about

-e expression #1, char 26: unknown commmand `,'

Can anybody help??

Upvotes: 1

Views: 753

Answers (3)

ghostdog74
ghostdog74

Reputation: 343135

Here is another variable of the answer accepted, but not extra call to another sed process

sed -n '/RUNNING PROCESSES/,/HOSTNAME/{s/RUNN.*\|HOSTNAME//;s/--*//;/^$/!p}' file

Upvotes: 0

paxdiablo
paxdiablo

Reputation: 882686

I would probably prefer to use awk for that:

awk '/RUNNING PROCESSES/ {s=2}
     /^---/              {s=s-1}
                         {if(s>0){print}}' linux.txt

That awk will give you:

           RUNNING PROCESSES
-----------------------------------

ftpd
kswapd
init
etc..

You can then pipe that through sed '/^$/d' to filter out the blank lines.

Upvotes: 0

msw
msw

Reputation: 43527

Did you mean:

sed -n '/RUNNING PROCESSES/,/HOSTNAME/p' linux.txt | 
  sed -e '/^[- ]/d' -e '/^$/d'

Upvotes: 2

Related Questions