Select a particular column using awk or cut or perl

后端 未结 4 1164
天涯浪人
天涯浪人 2021-02-04 03:20

I have a requirement to select the 7th column from a tab delimited file. eg:

cat filename | awk \'{print $7}\'

The issue is that the data in th

4条回答
  •  长发绾君心
    2021-02-04 03:47

    This might work for you (GNU sed):

    sed -r 's/(([^\t]*)\t?){7}.*/\2/' file
    

    This substitute command selects everything in the line and returns the 7th non-tab. In sed the last thing grouped by (...) will be returned in the lefthand side of the substitution by using a back-reference. In this case the first back-reference would return both the non-tab characters and the tab character (if present N.B. the ? meta-character which either one or none of the proceeding pattern).The .* just swallows up what was left on the line if any.

提交回复
热议问题