问题描述
我有一个问题用AWK简单地从流删除字段,如下图所示:
I have an issue with using AWK to simply remove a field from a stream, illustrated below:
1 int blah (void)
2 {
3 if (foo) {
4 printf ("blah\n");
5 }
6 return 0;
7 }
我用下面的code,除去第一场:
I use the following code to remove the first field:
$的awk'{$ 1 =;打印}'example.out
$ awk '{ $1=""; print }' example.out
int blah (void)
{
if (foo) {
printf ("blah\n");
}
return 0;
}
为什么会出现这种情况?这是因为AWK删除所有空白 - ?可这是prevented
Why is this the case? Is this because AWK removes all whitespace - can this be prevented?
亲切的问候提前
推荐答案
包含一个描述如何做到这一点。网站还链接了的含有3解决问题的方案。据我所知,如果分配给字段,则输出字段分隔符是用来连接各个领域在一起。因此,+
suddendly被折叠到一个空格。用一粒盐拿去吧,我敢不awk的专家。例如,尝试分配:
来变量 OFS
和冒号代替空格,将导致输出字段之间
Contains a description how to do it. It also links to http://student.northpark.edu/pemente/awk/awktail.txt which contains 3 solutions to the problem. As far as i know, if you assign to a field, then the output field separator is used to concatenate all fields together. So " "+
suddendly is collapsed to one space. Take it with a grain of salt though, i'm no awk expert. For example, try assigning :
to the variable OFS
, and colons instead of spaces will result in between fields in the output:
echo a b c | awk 'BEGIN{ OFS = ":" } { $1=""; print }'
$ :b:c
如果您使用摹 awk中,那么你可以使用它的 gensub
扩展,我发现pretty直截了当使用方法:
If you use gawk, then you can use its gensub
extension which i find pretty straight forward to use:
print gensub($1 "[\t ]*(.*)", "\\1", 1);
这篇关于保持原始格式POST通过AWK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!