如何从管道分隔文件中打印字段?

时间:2021-08-24 20:20:32

I have a file with fields separated by pipe characters and I want to print only the second field. This attempt fails:

我有一个文件,其中的字段由管道字符分隔,我想只打印第二个字段。此尝试失败:

$ cat file | awk -F| '{print $2}'
awk: syntax error near line 1
awk: bailing out near line 1
bash: {print $2}: command not found

Is there a way to do this?

有没有办法做到这一点?

4 个解决方案

#1


9  

The key point here is that the pipe character (|) must be escaped to the shell. Use "\|" or "'|'" to protect it from shell interpertation and allow it to be passed to awk on the command line.

这里的关键点是管道符(|)必须转义到shell。使用“\ |”或“'|'”以保护它免受shell干涉,并允许它在命令行上传递给awk。


Reading the comments I see that the original poster presents a simplified version of the original problem which involved filtering file before selecting and printing the fields. A pass through grep was used and the result piped into awk for field selection. That accounts for the wholly unnecessary cat file that appears in the question (it replaces the grep <pattern> file).

阅读评论我看到原始海报提供了原始问题的简化版本,其中涉及在选择和打印字段之前过滤文件。使用了通过grep的传递,并将结果通过管道输入awk进行字段选择。这说明了问题中出现的完全不必要的cat文件(它取代了grep 文件)。

Fine, that will work. However, awk is largely a pattern matching tool on its own, and can be trusted to find and work on the matching lines without needing to invoke grep. Use something like:

很好,这将工作。但是,awk本身就是一个模式匹配工具,可以信任查找和处理匹配的行而无需调用grep。使用类似的东西:

awk -F\| '/<pattern>/{print $2;}{next;}' file

The /<pattern>/ bit tells awk to perform the action that follows on lines that match <pattern>.

/ /位告诉awk在匹配 的行上执行后续操作。

The lost-looking {next;} is a default action skipping to the next line in the input. It does not seem to be necessary, but I have this habit from long ago...

丢失的{next;}是跳过输入中下一行的默认操作。它似乎没有必要,但我很久以前就有这种习惯......

#2


17  

Or just use one command:

或者只使用一个命令:

cut -d '|' -f FIELDNUMBER

#3


3  

The pipe character needs to be escaped so that the shell doesn't interpret it. A simple solution:

管道角色需要进行转义,以便shell不会解释它。简单的解决方案:

$ awk -F\| '{print $2}' file

Another choice would be to quote the character:

另一个选择是引用这个角色:

$ awk -F'|' '{print $2}' file

#4


0  

And 'file' contains no pipe symbols, so it prints nothing. You should either use 'cat file' or simply list the file after the awk program.

并且'file'不包含管道符号,因此它不会打印任何内容。你应该使用'cat file'或者只是在awk程序之后列出文件。

#1


9  

The key point here is that the pipe character (|) must be escaped to the shell. Use "\|" or "'|'" to protect it from shell interpertation and allow it to be passed to awk on the command line.

这里的关键点是管道符(|)必须转义到shell。使用“\ |”或“'|'”以保护它免受shell干涉,并允许它在命令行上传递给awk。


Reading the comments I see that the original poster presents a simplified version of the original problem which involved filtering file before selecting and printing the fields. A pass through grep was used and the result piped into awk for field selection. That accounts for the wholly unnecessary cat file that appears in the question (it replaces the grep <pattern> file).

阅读评论我看到原始海报提供了原始问题的简化版本,其中涉及在选择和打印字段之前过滤文件。使用了通过grep的传递,并将结果通过管道输入awk进行字段选择。这说明了问题中出现的完全不必要的cat文件(它取代了grep 文件)。

Fine, that will work. However, awk is largely a pattern matching tool on its own, and can be trusted to find and work on the matching lines without needing to invoke grep. Use something like:

很好,这将工作。但是,awk本身就是一个模式匹配工具,可以信任查找和处理匹配的行而无需调用grep。使用类似的东西:

awk -F\| '/<pattern>/{print $2;}{next;}' file

The /<pattern>/ bit tells awk to perform the action that follows on lines that match <pattern>.

/ /位告诉awk在匹配 的行上执行后续操作。

The lost-looking {next;} is a default action skipping to the next line in the input. It does not seem to be necessary, but I have this habit from long ago...

丢失的{next;}是跳过输入中下一行的默认操作。它似乎没有必要,但我很久以前就有这种习惯......

#2


17  

Or just use one command:

或者只使用一个命令:

cut -d '|' -f FIELDNUMBER

#3


3  

The pipe character needs to be escaped so that the shell doesn't interpret it. A simple solution:

管道角色需要进行转义,以便shell不会解释它。简单的解决方案:

$ awk -F\| '{print $2}' file

Another choice would be to quote the character:

另一个选择是引用这个角色:

$ awk -F'|' '{print $2}' file

#4


0  

And 'file' contains no pipe symbols, so it prints nothing. You should either use 'cat file' or simply list the file after the awk program.

并且'file'不包含管道符号,因此它不会打印任何内容。你应该使用'cat file'或者只是在awk程序之后列出文件。