Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use a string as a delimiter in unix cut command?

If I want to cut a list of text using a string as a delimiter, is that possible? For example I have a directory where a list of shell scripts call same perl script say

abc.pl

So when I do

$grep abc.pl * 

in that directory, it gives me following results

xyz.sh: abc.pl 1 2
xyz2.sh: abc.pl 2
mno.sh: abc.pl 3
pqr.sh: abc.pl 4 5

I basically want all the output after "abc.pl" (to check what range arguments are being passed to the perl right now)

When I tried

$grep abc.pl * | cut -d'abc.pl' -f2

OR

$grep abc.pl * | cut -d'abc\.pl' -f2

its giving me

cut: invalid delimiter

When I read man for cut it states

delim can be a multi-byte character.

What am I doing/interpreting wrong here?

like image 342
Devang Kamdar Avatar asked May 13 '09 10:05

Devang Kamdar


2 Answers

Try using this.

$grep abc.pl * | awk -F 'abc.pl' '{print $2}'

-F fs --field-separator fs Use fs for the input field separator (the value of the FS predefined variable).

like image 156
vkrishna Avatar answered Oct 21 '22 12:10

vkrishna


When I read man for cut it states ... delim can be a multi-byte character.

Multi-byte, but just one character, not a string.

canti:~$ ll | cut --delimiter="delim" -f 1,2
cut: the delimiter must be a single character
Try `cut --help' for more information.

canti:~$ cut --version  
cut (GNU coreutils) 5.97

You can specify only output delimiter as a string (useless in this case):

 --output-delimiter=STRING                                                          
        use STRING as the output delimiter the default is to use the input delimiter
like image 30
alex vasi Avatar answered Oct 21 '22 14:10

alex vasi