Wai Chin
Wai Chin

Reputation: 107

Easy Bash Cut Delimiter

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

Upvotes: 2

Views: 10232

Answers (4)

ghoti
ghoti

Reputation: 46826

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).

Upvotes: 4

perreal
perreal

Reputation: 97938

With sed:

sed -e 's/[ ]*[^ ]*[ ]*\(.*\)/\1/' -e 's/[ ]*/ /g' -e 's/^ *//' input_file

Upvotes: 1

chepner
chepner

Reputation: 530990

A little roundabout, but interesting:

( set -- $input; shift; echo $@ )

Upvotes: 3

Tom Naessens
Tom Naessens

Reputation: 1837

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

Also gets rid of the 'echo'.

Upvotes: 5

Related Questions