Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easy Bash Cut Delimiter

Tags:

bash

I have this..

$input = "echo     a       b                c   d"
echo -e "$input" | cut -d " " -f 2-

but I just want a simple cut that will get rid of echo as well as print

a b c d #(single space) only
like image 253
Wai Chin Avatar asked Dec 20 '25 11:12

Wai Chin


2 Answers

echo -e "$input" | tr -s ' ' | cut -d " " -f2-

Also gets rid of the 'echo'.

like image 183
Tom Naessens Avatar answered Dec 23 '25 03:12

Tom Naessens


You don't need any tools besides what bash provides built-in.

[ghoti@pc ~]$ input="echo     a       b                c   d"
[ghoti@pc ~]$ output=${input//  / }
[ghoti@pc ~]$ echo $output
echo a b c d
[ghoti@pc ~]$ echo ${output#* }
a b c d
[ghoti@pc ~]$ 

Up-side: you avoid the extra overhead of pipes.

Down-side: you need to assign an extra variable, because you can't do complex pattern expansion within complex pattern expansion (i.e. echo ${${input//  / }#* } won't work).

like image 42
ghoti Avatar answered Dec 23 '25 03:12

ghoti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!