grep 输出仅打印 bash 脚本中的一行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19005945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
grep output prints only single line in bash script
提问by user2815333
How can I get the results from grep to print on their own line in a bash script?
如何从 grep 获取结果以在 bash 脚本中打印在自己的行上?
When using grep in the terminal, the output appears how I wish it would appear.
在终端中使用 grep 时,输出会以我希望的方式出现。
For instance:
例如:
$ whois x.x.85.72 | grep 'OrgName\|NetRange\|inetnum\|IPv4'
NetRange: x.x.85.64 - x.x.85.95
NetRange: x.x.0.0 - x.x.255.255
OrgName: xxxxx Technologies Inc.
When using the same grep command in bash it prints out on one line.
在 bash 中使用相同的 grep 命令时,它会在一行上打印出来。
The output of my bash script:
我的 bash 脚本的输出:
$ lookup xx.com
xx.com resolves to: x.x.85.72
NetRange: x.x.85.64 - x.x.85.95 NetRange: x.x.0.0 - x.x.255.255 OrgName:xxxxx Technologies Inc.
My bash script:
我的 bash 脚本:
#! /bin/bash
VAR1=""
IP=`net lookup $VAR1`
echo $VAR1 resolves to: $IP
RANGE=`whois $IP | grep 'OrgName\|NetRange\|inetnum\|IPv4'`
echo $RANGE
aside from a solution, can anyone tell me why it does this?
除了解决方案,谁能告诉我为什么这样做?
Thanks a bunch!
谢谢一堆!
回答by fedorqui 'SO stop harming'
You need to quote the variable to have the format preserved:
您需要引用变量以保留格式:
echo "$RANGE"
instead of
代替
echo $RANGE
All together:
全部一起:
#!/bin/bash <--- be careful, you have an space after ! in your code
VAR1=""
IP=$(net lookup $VAR1) #<--- note I use $() rather than ``
echo $VAR1 resolves to: $IP
RANGE=$(whois $IP | grep 'OrgName\|NetRange\|inetnum\|IPv4')
echo "$RANGE"
Example
例子
Given this:
鉴于这种:
$ date; date
Wed Sep 25 15:18:39 CEST 2013
Wed Sep 25 15:18:39 CEST 2013
Let's print its result with and without quotes:
让我们打印带引号和不带引号的结果:
$ myvar=$(date; date)
$ echo $myvar
Wed Sep 25 15:18:45 CEST 2013 Wed Sep 25 15:18:45 CEST 2013
$ echo "$myvar"
Wed Sep 25 15:18:45 CEST 2013
Wed Sep 25 15:18:45 CEST 2013
回答by Chris Seymour
Quoting is very important in the shell, you need to quote all your variables to preserve the newlines:
引用在 shell 中非常重要,您需要引用所有变量以保留换行符:
#!/bin/bash
VAR1=""
IP=$(net lookup "$VAR1")
echo "$VAR1 resolves to: $IP"
RANGE=$(whois "$IP" | egrep 'OrgName|NetRange|inetnum|IPv4')
echo "$RANGE"
Read man bash
under the quoting section. Also using $()
is much clearer than using backticks and it allows nesting.
man bash
在引用部分下阅读。也使用$()
比使用反引号更清晰,它允许嵌套。
回答by Atropo
You're missing the quote of the $RANGE
variable.
您缺少$RANGE
变量的引号。
You should use:
你应该使用:
echo "$RANGE"
Without the quote
the newline isn't preserved.
如果没有quote
换行符,则不会保留。
回答by Vlad
Replace:
代替:
RANGE=`whois $IP | grep 'OrgName\|NetRange\|inetnum\|IPv4'`
echo $RANGE
with
和
whois $IP | grep 'OrgName\|NetRange\|inetnum\|IPv4'
or
或者
RANGE=`whois $IP | grep 'OrgName\|NetRange\|inetnum\|IPv4'`
echo "$RANGE"