views:

46

answers:

4

hello everybody, I've got strange problem with cut I wrote script, there I have row:

... | cut -d" " -f3,4 >! out

cut recieves this data (I checked it with echo)

   James             James              033333333 0              0.00

but I recieve empty lines in out, can somebody explain why?

A: 

Cut doesn't see multiple spaces as single space, so it matches "nothingness" between spaces.

Do you get empty lines when you leave out >! out part? Ie, are you targeting correct fields?

If your input string uses fixed spacing, you might want to use cut -c 4-10,15-20 | tr -d ' ' to extract character groups 4-10 and 15-20 and remove spaces from them..

mr.b
yes of course, 5 elements, but I didn't know about nothingness, how can I change it I need 4th and 5th fields?
lego69
You could use another form of cut (see my edited post), or you could use awk (see next answer).
mr.b
+1  A: 

If you want fields from a text file, awk is almost always the answer:

... | awk '{print $3" "$4}'

For example:

$ echo 'James     James      033333333 0              0.00' | cut -d" " -f3,4

$ echo 'James     James      033333333 0              0.00' | awk '{print $3" "$4}'
033333333 0
paxdiablo
oh, can I do it without awk, I can't use it, it is part of my project, I do it only with cut, grep, sort, uniq, head, tail
lego69
None of those commands will compress multiple spaces into one. Are you sure it's not tabs in the input?
paxdiablo
yes, I'm sure, but still how can I do it without awk, I don't have fixed spacing?
lego69
A: 

You need to compress out the sequences of spaces, so that each string of spaces is replaced by a single space. The tr command's -s (squeeze) option is perfect for this:

$ ... | tr -s " " | cut -d" " -f3,4 >! out
unwind
A: 
... | grep -o "[^ ]*"

will extract fields, each on separate line. Then you might head/tail them. Not sure about putting them on the same line again.

mr.b