保持原始格式POST通过AWK [英] Keeping original format POST passing through AWK

查看:122
本文介绍了保持原始格式POST通过AWK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题用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?

亲切的问候提前

推荐答案

http://awk.freeshell.org/ RangeOfFields

包含一个描述如何做到这一点。网站还链接了 http://student.northpark.edu/pemente/awk/awktail.txt 的含有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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆