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:
sed 's/\S*\(*+\)\S*//g'
sed 's/ //g'
sed 's/x.*//'
into a single command?
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:
sed 's/\S*\(*+\)\S*//g'
sed 's/ //g'
sed 's/x.*//'
into a single command?
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.*//'
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.
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.*//'
Without sed
but just grep
:
$ xrandr | grep -oP '^\s+\K\d+(?=.*?\*)'
1440
or with perl :
$ xrandr | perl -lne 'print $1 if /^\s+(\d+)(?=.*?\*)/'
1440
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