简体   繁体   English

awk如何打印其余部分

[英]awk how to print the rest

my file contains lines like this 我的文件包含这样的行

any1 aaa bbb ccc

The delimiter is space. 分隔符是空格。 the number of words in the line is unknown 该行中的单词数未知

I want to put the first word into a var1 . 我想将第一个单词放入var1 It's simple with 这很简单

awk '{print $1}'

Now I want to put the rest of the line into a var2 with awk. 现在,我想将其余的行放入带有awk的var2

How I can print the rest of the line with awk ? 如何使用awk打印其余的行?

Better to use read here: 最好在这里read

s="any1 aaa bbb ccc"
read var1 var2 <<< "$s"
echo "$var1"
any1
echo "$var2"
aaa bbb ccc

For awk only solution use: 对于仅awk解决方案,请使用:

echo "$s" | awk '{print $1; print substr($0, index($0, " ")+1)}'
any1
aaa bbb ccc
$ var=$(awk '{sub(/^[^[:space:]]+[[:space:]]+/,"")}1' file)
$ echo "$var"
aaa bbb ccc

or in general to skip some number of fields use a RE interval: 或通常使用RE间隔跳过一些字段:

$ awk '{sub(/^[[:space:]]*([^[:space:]]+[[:space:]]+){1}/,"")}1' file
aaa bbb ccc
$ awk '{sub(/^[[:space:]]*([^[:space:]]+[[:space:]]+){2}/,"")}1' file
bbb ccc
$ awk '{sub(/^[[:space:]]*([^[:space:]]+[[:space:]]+){3}/,"")}1' file
ccc

Note that doing this gets much more complicated if you have a FS that's more than a single char, and the above is just for the default FS since it additionally skips any leading blanks if present (remove the first [[:space:]]* if you have a non-default but still single-char FS). 请注意,如果您拥有一个不止一个字符的FS,则此操作将变得更加复杂,以上内容仅适用于默认FS,因为它还会跳过所有前导空白(如果存在的话)(删除第一个[[:space:]]*如果您使用的是非默认值但仍为单字符FS)。

awk解决方案:

awk '{$1 = ""; print $0;}'`

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

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