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
  • 730,956
  • 141
  • 904
  • 1,278
user2079266
  • 49
  • 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
  • 204,365
  • 48
  • 270
  • 300
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
  • 32,370
  • 6
  • 56
  • 63
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
  • 8,765
  • 9
  • 49
  • 56
1

With newlines :

echo coobas | sed 's:c:f:
s:s:r:'
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
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 Quénot
  • 173,512
  • 41
  • 224
  • 223
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
  • 29,770
  • 54
  • 146
  • 240