awk - 如何使用 awk 使用空格作为字段分隔符?

标签 awk

因此,我尝试将列与字段分隔符一起输出。

但在我的情况下它失败了。

awk.txt(Input file)

Sr No Name Sub Marks
1) Amit Physics 80
2) Rahul Maths 90
3) Shyam Biology 87
4) Kedar English 85
5) Hari History 89

我尝试过的 awk 命令如下:-
 awk -F ' ' '{print $2 $3;}' awk.txt > output.txt

获得的输出:
NoName
AmitPhysics
RahulMaths
ShyamBiology
KedarEnglish
HariHistory

预期输出:
    Name Sub 
    Amit Physics 
    Rahul Maths 
    Shyam Biology 
    Kedar English 
    Hari History 

最佳答案

awk '{print $3,$4;}' awk.txt > output.txt

不需要指示字段分隔符。字段编号从 1 开始,因此您需要第三个和第四个字段。对于打印 OFS(输出字段分隔符),请使用逗号。

或者:
awk 'BEGIN{FS=OFS=" ";}{print $3,$4;}' awk.txt > output.txt

关于awk - 如何使用 awk 使用空格作为字段分隔符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45589981/

相关文章:

awk - 如何将文本文件中的每一行另存为新文件

xml - 如何在 Mac 上使用脚本更改 XML 属性的特定值

sed - grep/sed : How to print *something* when nothing matches

regex - 按模式查找线条,仅保留模式但保留不匹配的线条

bash - 如何在 awk 中用 "."替换重复的行?

linux - 如何在 Linux 上使用不同的工作表创建 CSV 文件

awk - 我的 awk 脚本有问题。 -condition : using variable in awk. - 目标:打印特定列开始在文本中计数变量

linux - 如何将第一列中的文本附加到所有列 linux

从 stdin 导入 MySQL

bash - awk 或 Sed : Replace text between special characters (delimiters)