开发者

problems with cut (unix)

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 empt开发者_如何学Cy lines in out, can somebody explain why?


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


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


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜