0

I made this script:

xrandr | grep '*' | sed 's/\S*\(*+\)\S*//g'| sed 's/ //g' | sed 's/x.*//'

How can I combine the three sed commands:

  1. sed 's/\S*\(*+\)\S*//g'
  2. sed 's/ //g'
  3. sed 's/x.*//'

into a single command?

Jonathan Leffler
  • 698,132
  • 130
  • 858
  • 1,229
user2079266
  • 29
  • 1
  • 3

6 Answers6

3

With -e:

xrandr | grep '*' | sed -e 's/\S*\(*+\)\S*//g' -e 's/ //g' -e 's/x.*//'

Note that the grep is not necessary:

xrandr | sed -e '/\*/!d' -e 's/\S*\(*+\)\S*//g' -e 's/ //g' -e 's/x.*//'
William Pursell
  • 190,037
  • 45
  • 260
  • 285
3

You could put those commands in a file called sedscr for example, one per line as such:

s/x.*//
s/ //g
s/\S*\(*+\)\S*//g

And then call:

xrandr | grep '*' | sed -f sedscr

I prefer this method, just in case I want to add more commands in the future.

squiguy
  • 30,745
  • 6
  • 53
  • 59
1

You can simply consider all sed commands as script and add a ; between commands :

xrandr | grep '*' | sed 's/\S*\(*+\)\S*//g ; s/ //g ; s/x.*//'
Zulu
  • 7,776
  • 9
  • 44
  • 55
1

With newlines :

echo coobas | sed 's:c:f:
s:s:r:'
Gilles Quenot
  • 154,891
  • 35
  • 213
  • 206
0

Without sed but just grep :

$ xrandr | grep -oP '^\s+\K\d+(?=.*?\*)'
1440

or with :

$ xrandr | perl -lne 'print $1 if /^\s+(\d+)(?=.*?\*)/'
1440
Gilles Quenot
  • 154,891
  • 35
  • 213
  • 206
0

Another thing to consider is to make a sed config file (call it config.sed) listing all replacement rules. E.g.:

1,/^END/{
   s/x.*//
   s/ //g
   s/\S*\(*+\)\S*//g
}

and then run

sed -f config.sed filein.txt > fileout.txt
amphibient
  • 27,548
  • 48
  • 136
  • 231