用awk消除不必要的输出和sed [英] eliminate unwanted output using awk and sed

查看:313
本文介绍了用awk消除不必要的输出和sed的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从以下命令我怎么能消除所有的前

这是发生线

 所有者RepoName CreatedDate

edit命令:

 查找/选择/网站/ -name'.log.txt| xargs的猫| AWK'{$ NF =;打印$ 0}'| SED'1I所有者RepoName CreatedDate|列-t

的输出是

 发现:文件系统循环检测; `/nfs/.snapshot/nightly.4'具有相同的设备号和inode作为目录这是在文件系统层次高2等级。
发现:文件系统循环检测; `/nfs/.snapshot/nightly.5'具有相同的设备号和inode作为目录这是在文件系统层次高2等级。
发现:文件系统循环检测; `/nfs/.snapshot/nightly.6'具有相同的设备号和inode作为目录这是在文件系统层次高2等级。
业主RepoName CreatedDate
VAL ABC周五3月16日17时01分07秒PDT
P1 repo_pc周三3月21日11时34分42秒PDT
新FM周一03月19日0时15分51秒PD

需要的输出仅为:

 所有者RepoName CreatedDate
VAL ABC周五3月16日17时01分07秒PDT
P1 repo_pc周三3月21日11时34分42秒PDT
新FM周一03月19日0时15分51秒PD


解决方案

这些发现错误将在标准错误,所以完全绕过链,你要的错误与 2 - 重定向;的/ dev /空,虽然这种意志prevent你看到了find命令任何其他错误。

 查找/选择/网站/ -name'.log.txt'2  - ;的/ dev / null的| xargs的猫| AWK'{$ NF =;打印$ 0}'| xargs的的sed/文件系统/ D| SED'1I所有者RepoName CreatedDate|列-t

在一般以这样一个复杂的命令,你应该打破它,当你有错误,这样就可以制定出哪里出了问题的来源。

让我们分手了这个命令来看看它在做什么:

查找/选择/网站/ -name'.log.txt'2 - ;的/ dev / null的 - 找到的所有文件在/ opt /网站/名为.LOG .TXT

xargs的猫 - 得到他们的所有内容,一前一后

的awk'{$ NF =;打印$ 0} - 删除最后一列

xargs的的sed/文件系统/ D - 对待每个条目为一个文件,并删除这些文件中的内容,包含文件系统的任何行

sed的'1I所有者RepoName CreatedDate - 插入业主RepoName CreatedDate第一行

列-t - 给定的数据转换成表格

我建议建立的命令,并检查输出是在每个阶段正确的。

有几件事情是令人惊讶的关于你的命令:


  1. 将找到一个寻找的正是.log.txt而不是扩展名的文件。

  2. 第二xargs的来电 - 转换.log.txt文件的内容转换成文件名

From the following command how can i eliminate all the lines that occur before

 Owner     RepoName             CreatedDate

EDIT Command:

find /opt/site/ -name '.log.txt' | xargs cat | awk '{$NF=""; print $0}' | sed '1i Owner RepoName CreatedDate' | column -t

The output is

find: Filesystem loop detected; `/nfs/.snapshot/nightly.4' has the same device number and inode as a directory which is 2 levels higher in the filesystem hierarchy.
find: Filesystem loop detected; `/nfs/.snapshot/nightly.5' has the same device number and inode as a directory which is 2 levels higher in the filesystem hierarchy.
find: Filesystem loop detected; `/nfs/.snapshot/nightly.6' has the same device number and inode as a directory which is 2 levels higher in the filesystem hierarchy.
Owner     RepoName             CreatedDate
val        abc                  Fri          Mar  16  17:01:07  PDT
p1         repo_pc              Wed          Mar  21  11:34:42  PDT
New        fm                   Mon          Mar  19  00:15:51  PD 

Required output is only:

Owner     RepoName             CreatedDate
val        abc                  Fri          Mar  16  17:01:07  PDT
p1         repo_pc              Wed          Mar  21  11:34:42  PDT
New        fm                   Mon          Mar  19  00:15:51  PD 

解决方案

Those find errors will be on stderr, so bypass your chain entirely, you'll want to redirect the errors with 2>/dev/null, although that will prevent you seeing any other errors in the find command.

find /opt/site/ -name '.log.txt' 2>/dev/null | xargs cat | awk '{$NF=""; print $0}' | xargs sed "/Filesystem/d" | sed '1i Owner RepoName CreatedDate' | column -t

In general with a complicated command like this, you should break it down when you have errors so that you can work out where the problem is coming from.

Let's split up this command to see what it's doing:

find /opt/site/ -name '.log.txt' 2>/dev/null - find all the files under /opt/site/ named .log.txt

xargs cat - get all their contents, one after the other

awk '{$NF=""; print $0}' - delete the last column

xargs sed "/Filesystem/d" - Treat each entry as a file and delete any lines containing Filesystem from the contents of those files.

sed '1i Owner RepoName CreatedDate' - Insert Owner RepoName CreatedDate on the first line

column -t - Convert the given data into a table

I'd suggest building up the command, and checking the output is correct at each stage.

Several things are surprising about your command:

  1. The find one looks for files that are exactly .log.txt rather than an extension.
  2. The second xargs call - converting the contents of the .log.txt files into filenames.

这篇关于用awk消除不必要的输出和sed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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