Redian新闻
>
怎样打印出grep后的部分内容?
avatar
怎样打印出grep后的部分内容?# Unix - 噫吁兮,危乎高哉
t*l
1
for example,
1.grep "User Name:" user.txt
User Name: John
User Name: Mary
User Name: Michael
现在我想只打印出冒号后面的名字,并使之间隔显示在同一行里.怎么实现?
thanks.
avatar
D*e
2
grep "User Name:" user.txt | awk -F "\:" '{print $2}' | tr '\n' ' '

【在 t*******l 的大作中提到】
: for example,
: 1.grep "User Name:" user.txt
: User Name: John
: User Name: Mary
: User Name: Michael
: 现在我想只打印出冒号后面的名字,并使之间隔显示在同一行里.怎么实现?
: thanks.

avatar
t*l
3
doesn't work :))

【在 D**e 的大作中提到】
: grep "User Name:" user.txt | awk -F "\:" '{print $2}' | tr '\n' ' '
avatar
s*s
4
no space after -F, and no need to use tr

【在 t*******l 的大作中提到】
: doesn't work :))
avatar
t*l
5
this time it works, but nothing came out....

【在 s**s 的大作中提到】
: no space after -F, and no need to use tr
avatar
D*e
6
what's the matter?

【在 t*******l 的大作中提到】
: doesn't work :))
avatar
t*l
7
awk: syntax error near line 1
awk: bailing out near line 1

【在 D**e 的大作中提到】
: what's the matter?
avatar
D*e
8
how do you chop off the newline without tr?

【在 s**s 的大作中提到】
: no space after -F, and no need to use tr
avatar
D*e
9
leave spaces where appropriate.

【在 t*******l 的大作中提到】
: awk: syntax error near line 1
: awk: bailing out near line 1

avatar
c*z
10
grep xxx |awk -F":" '{printf("%s ",$2);}'
the ' is the one next to L, the same key of "

【在 t*******l 的大作中提到】
: awk: syntax error near line 1
: awk: bailing out near line 1

avatar
c*t
11
so many awk fans. Sigh, I don't know awk, but other common commands
work well too.
echo `grep xxx | cut -d: -f2`
Of course, it runs two instead of one, but I would assume that the
bottleneck was the grep part...
avatar
c*z
12
cut is ok here, but awk is more powerful and can handle more complicated cas
es.
as to master a unix cmd tools is really not easy, to know how to use awk is
a better idea for me :)
anyway, gawk is available on almost all unix platform ( and i'm using gawk )

【在 c*****t 的大作中提到】
: so many awk fans. Sigh, I don't know awk, but other common commands
: work well too.
: echo `grep xxx | cut -d: -f2`
: Of course, it runs two instead of one, but I would assume that the
: bottleneck was the grep part...

avatar
t*l
13
how to use it?:)

【在 c*****z 的大作中提到】
: cut is ok here, but awk is more powerful and can handle more complicated cas
: es.
: as to master a unix cmd tools is really not easy, to know how to use awk is
: a better idea for me :)
: anyway, gawk is available on almost all unix platform ( and i'm using gawk )

avatar
w*n
14
use Perl, it can do anything that sed/awk or shell can do and many more.
perl -nle '/xxx/ && /:\s*(\w+)/ && print $1'

【在 c*****z 的大作中提到】
: cut is ok here, but awk is more powerful and can handle more complicated cas
: es.
: as to master a unix cmd tools is really not easy, to know how to use awk is
: a better idea for me :)
: anyway, gawk is available on almost all unix platform ( and i'm using gawk )

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。