I read some data out of a file, used grep for the only two columns needed, and redirected the output into a variable.
我从文件中读取一些数据,使用grep只需要两列,并将输出重定向到变量。
My script looks like this:
我的脚本看起来像这样:
#!/bin/bash
cat hosts.cfg | grep 'address\|host_name' | sed -e 's/\<address\>//g' | while read line; do
echo $line | sed 's/host_name//g' | sed -r 's/\s+//g' ;
done
The output looks something like this now:
输出现在看起来像这样:
Host1
xx.xx.xx.xx
Host2
xx.xx.xx.xx
The problem is that hosts and ips must be saved into an array, not a file!
问题是必须将hosts和ips保存到数组中,而不是文件中!
Output must look like this:
输出必须如下所示:
Host1(tab)xx.xx.xx.xx
Host2(tab)xx.xx.xx.xx
4 个解决方案
#1
0
You can use awk
:
你可以使用awk:
echo $output | awk 'NR%2{printf $0"\t";next;}1'
To save any command output, wrap it in backticks, or the newer (but less backward compatible) $(command) style substitution. E.g.:
要保存任何命令输出,请将其包装在反引号中,或者更新(但不太向后兼容)的$(命令)样式替换。例如。:
result=`echo $output | awk 'NR%2{printf $0"\t";next;}1'`
#2
1
You are looking for process substitution. $(command)
, or old-style in `
s.
您正在寻找流程替代。 $(命令),或者s中的旧式。
(sorry, the description of how it should work is not clear enough for me to show modified version of your code)
(对不起,它应该如何工作的描述不够明确,我可以显示你的代码的修改版本)
#3
0
using sed 'N;s/\n/\t/g'
使用sed'N; s / \ n / \ t / g'
change
更改
Host1
xx.xx.xx.xx
Host2
xx.xx.xx.xx
to
至
Host1(tab)xx.xx.xx.xx
Host2(tab)xx.xx.xx.xx
#4
0
You can use "set" to get faster output
您可以使用“set”来获得更快的输出
exg:
EXG:
I have file best2,
我有文件best2,
# cat best2
Host1 xx.xx.xx.xx Host2 xx.xx.xx.xx
make a script called: tabcheck.sh
制作一个名为:tabcheck.sh的脚本
# cat tabcheck.sh
#!/bin/bash
out=$(cat best2)
set $out
echo -e "$1\t$2\n$3\t$4"
# ./tabcheck.sh
Host1 xx.xx.xx.xx
Host2 xx.xx.xx.xx
If you use shift command(Usually only nine command line arguments can be accessed using positional parameters. The shift command gives access to command line arguments greater than nine by shifting each of the arguments.) as well.
如果使用shift命令(通常只能使用位置参数访问九个命令行参数.shift命令也可以通过移动每个参数来访问大于9的命令行参数。)。
Thanks.
谢谢。
#1
0
You can use awk
:
你可以使用awk:
echo $output | awk 'NR%2{printf $0"\t";next;}1'
To save any command output, wrap it in backticks, or the newer (but less backward compatible) $(command) style substitution. E.g.:
要保存任何命令输出,请将其包装在反引号中,或者更新(但不太向后兼容)的$(命令)样式替换。例如。:
result=`echo $output | awk 'NR%2{printf $0"\t";next;}1'`
#2
1
You are looking for process substitution. $(command)
, or old-style in `
s.
您正在寻找流程替代。 $(命令),或者s中的旧式。
(sorry, the description of how it should work is not clear enough for me to show modified version of your code)
(对不起,它应该如何工作的描述不够明确,我可以显示你的代码的修改版本)
#3
0
using sed 'N;s/\n/\t/g'
使用sed'N; s / \ n / \ t / g'
change
更改
Host1
xx.xx.xx.xx
Host2
xx.xx.xx.xx
to
至
Host1(tab)xx.xx.xx.xx
Host2(tab)xx.xx.xx.xx
#4
0
You can use "set" to get faster output
您可以使用“set”来获得更快的输出
exg:
EXG:
I have file best2,
我有文件best2,
# cat best2
Host1 xx.xx.xx.xx Host2 xx.xx.xx.xx
make a script called: tabcheck.sh
制作一个名为:tabcheck.sh的脚本
# cat tabcheck.sh
#!/bin/bash
out=$(cat best2)
set $out
echo -e "$1\t$2\n$3\t$4"
# ./tabcheck.sh
Host1 xx.xx.xx.xx
Host2 xx.xx.xx.xx
If you use shift command(Usually only nine command line arguments can be accessed using positional parameters. The shift command gives access to command line arguments greater than nine by shifting each of the arguments.) as well.
如果使用shift命令(通常只能使用位置参数访问九个命令行参数.shift命令也可以通过移动每个参数来访问大于9的命令行参数。)。
Thanks.
谢谢。