24

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?

Devang Kamdar
  • 5,617
  • 8
  • 24
  • 16
  • sorry for the formatting lemme correct few lines above so they make sense ... 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 ... 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 ... Hope the new lines work this time :) – Devang Kamdar May 13 '09 at 10:57
  • nope :( not sure why its not showing newlines as entered :( – Devang Kamdar May 13 '09 at 10:57
  • You mean the man pages with ZERO usage examples didn't make it clear? – Chance Feb 22 '12 at 17:22

5 Answers5

33

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

vkrishna
  • 744
  • 1
  • 6
  • 10
11

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
alex vasi
  • 5,304
  • 28
  • 31
7

why not use grep abc.pl | awk '{print $3, $4}'?

alamar
  • 18,729
  • 4
  • 64
  • 97
  • not really working the way I am expecting it to show results. Its showing partial list ... also am curious whats wrong with the way I am using cut? – Devang Kamdar May 13 '09 at 11:01
  • I've never in fact used cut. Your task shouts for awk or perl! – alamar May 13 '09 at 12:09
  • Cool ... Thanks alamar ... however I am gonna give the correct answer to Alex's response, as he really answered what I was looking for. I wish there was a way to select two answers ... but then it would cause many other probs. So thanks for your help. Really appreciate it :) – Devang Kamdar May 13 '09 at 12:18
  • No problem, I don't really care about that scoring thing. – alamar May 13 '09 at 13:02
  • good to know ... but its just that if someone else comes looking for an answer to the same problem, reading both the posts would give complete solution. Anyways ... assuming they would look at other replies too if their problem is not solved by looking at first one :) – Devang Kamdar May 13 '09 at 13:39
0

Or you can try eg Ruby:

grep abc.pl * | ruby -ne 'p $_.chomp.split("abc.pl").last'
Jose Alban
  • 7,286
  • 2
  • 34
  • 19
-1

$ grep abc.pl * | cut -d' ' -f3-999

In that case just use the space character as the delimiter.

wdingus
  • 56
  • 5