简体   繁体   English

awk命令:如何告诉它区分空格和制表符分隔符

[英]awk command: how to tell it to distinguish between space and tab delimiter

I used this command, awk '{print $1}' "$line" , to get a column of data from a file that looks like this:(underscores represent tab as a delimiter, example is a three-column data) 我使用以下命令awk '{print $1}' "$line"从看起来像这样的文件中获取数据列:(下划线表示制表符为定界符,示例是三列数据)

Joe Jonas_____8_____45  
Cersei Lann_____4_____23  
Bo Tox_____6_____28  
Mis Ter Yo_____7_____89  

When I run the command, say to get the first column, the output, however, showed it like this: 当我运行命令时,说要获取第一列,但是输出显示如下:

Joe  
Cersei  
Bo  
Mis  

How can I make the command to ignore the spaces within the data of the first column? 如何使命令忽略第一列数据中的空格?

Change the field separator (-F) to tab(s): 将字段分隔符(-F)更改为选项卡:

$ awk -F$'\t*' '{gsub(/ /,"_",$1); print $1,$2}' test2.in
Joe_Jonas 8
Cersei_Lann 4
Bo_Tox 6
Mis_Ter_Yo 7

EDIT: Use gsub on first column to replace space with underscore. 编辑:在第一列上使用gsub用下划线替换空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM