Linux 在字符串上使用 awk

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10959133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 06:44:42  来源:igfitidea点击:

using awk on a string

linuxbashawk

提问by monucool

can I use awk to extract the first column or any column on a string? Actually i am using a file and reading it to a variable I want to use AWK on that variable and do my job.

我可以使用 awk 提取字符串中的第一列或任何列吗?实际上,我正在使用一个文件并将其读取到一个变量中,我想在该变量上使用 AWK 并完成我的工作。

How is it possible? Any suggestions.

这怎么可能?有什么建议。

采纳答案by jedwards

Print first column*:

打印第一列*:

<some output producing command> | awk '{print }'

Print second column:

打印第二列:

<some output producing command> | awk '{print }'

etc.

等等。

Where <some output producing command>is like cat filename.txtor echo $VAR, etc.

<some output producing command>cat filename.txtecho $VAR等在哪里。

e.g. ls -l | awk '{print $9}'extracts the ninth column, which is like an ... awkward way of ls -1

例如ls -l | awk '{print $9}'提取第九列,这就像一个......尴尬的方式ls -1

*Columns are defined by the separating whitespace.

*列由分隔空格定义。



EDIT: If your text is already in a variable, something like:

编辑:如果您的文本已经在一个变量中,例如:

VAR2=$(echo $VAR | awk '{print }')

would work, provided you change 9 to the desired column.

可以工作,前提是您将 9 更改为所需的列。