I want a bash way to read lines from standard input (so I can pipe input to it), and remove just the leading and trailing space characters. Piping to echo does not work.
For example, if the input is:
12 s3c sd wqr
the output should be:
12 s3c sd wqr
I want to avoid writing a python script or similar for something as trivial as this. Any help is appreciated!
You can use sed to trim it. And \s instead of space in a case of TABs. So, here's what I found: sed -r 's/\s*(. *)\s*/\1/' will mostly work, but I think it will not trim the end of the line, since the .
The simple, non-technical, answer is that Ctrl + D terminates the STDIN file and that Ctrl + C terminates the active application. Both are handled in the keyboard driver and apply to all programs reading from the keyboard. To see the difference start a command line shell and enter the wc command with no arguments.
Using head to get the first lines of a stream, and tail to get the last lines in a stream is intuitive. But if you need to skip the first few lines of a stream, then you use tail “-n +k” syntax. And to skip the last lines of a stream head “-n -k” syntax.
You can use sed to trim it.
sed 's/^ *//;s/ *$//'
You can test it really easily on a command line by doing:
echo -n " 12 s3c " | sed 's/^ *//;s/ *$//' && echo c
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With