我有一个脚本连接两个不同变量的输出。问题是两个变量的输出都包含多行。所以,输出不是我所期望的。
第一个变量和输出:
snap_daily=`cat snaps.txt | grep test-for-dr | awk '{print $2}' | sed 's/[",]//g' | sed 's/test-for-dr-//g'`
2017-03-10-08-00
2017-03-10-11-00
2017-03-10-12-00
2017-03-10-14-00
2017-03-10-15-00
第二个变量和输出:
snap_prefix=`cat snaps.txt | grep test-for-dr | awk '{print $2}' | sed 's/[",]//g' | awk -F '2017' '{print $1}'
test-for-dr-
test-for-dr-
test-for-dr-
test-for-dr-
test-for-dr-
连接和结果的代码:
snap_name="$snap_prefix$snap_daily"
test-for-dr-bdmprest- test-for-dr-bdmprest- test-for-dr-bdmprest- test-for-dr-bdmprest- test-for-dr-bdmprest-2017-03-10-08-00 2017-03-10-11-00 2017-03-10-12-00 2017-03-10-14-00 2017-03-10-15-00
期望的结果:
test-for-dr-2017-03-10-08-00
test-for-dr-2017-03-10-11-00
test-for-dr-2017-03-10-12-00
test-for-dr-2017-03-10-14-00
test-for-dr-2017-03-10-15-00
本质上,我需要每一行与输出的每一行相匹配。
答案1
当您有 2 个多行变量时,有几种方法可以组合它们:
声明用于测试的变量
$ snap_prefix="test-for-dr- test-for-dr- test-for-dr- test-for-dr- test-for-dr-" $ snap_daily="2017-03-10-08-00 2017-03-10-11-00 2017-03-10-12-00 2017-03-10-14-00 2017-03-10-15-00"
使用 bash 循环从每个变量中读取一行:
while read -u3 prefix; read -u4 suffix; do echo "$prefix$suffix" done 3<<<"$snap_prefix" 4<<<"$snap_daily"
用于
pr
将单个流转换为 2 列(然后tr
删除分隔列的制表符){ echo "$snap_prefix"; echo "$snap_daily"; } | pr -2Ts | tr -d '\t'
2和3都输出:
test-for-dr-2017-03-10-08-00
test-for-dr-2017-03-10-11-00
test-for-dr-2017-03-10-12-00
test-for-dr-2017-03-10-14-00
test-for-dr-2017-03-10-15-00
但,我打赌这适用于您未向我们展示的输入:
grep -Eo 'test-for-dr-[[:digit:]-]+' snaps.txt
答案2
如果最终目标是重新排列和打印2
文件中的字段snaps.txt
,则不需要中间变量也不需要grep-sed-awk
管道,单次awk
调用应该能够完成这项工作
awk '$2 ~ /test-for-dr-/{
gsub(/[",]/, "", $2)
match($2, "test-for-dr-")
printf "%s%s\n", substr($2, RSTART), substr($2, 1, RSTART-1)
}' snaps.txt
答案3
做如下:
#!/bin/bash
cat snaps.txt | grep test-for-dr | awk '{print $2}' | sed 's/[",]//g' | sed 's/test-for-dr-//g' > snap_daily.txt
cat snaps.txt | grep test-for-dr | awk '{print $2}' | sed 's/[",]//g' | awk -F '2017' '{print $1}' > snap_prefix.txt
a=`cat snap_daily.txt | wc -l`
let x=1
for i in `cat snap_daily.txt`
do
snap_daily=`cat snap_daily.txt | sed -n "$x"p`
snap_prefix=`cat snap_prefix.txt | sed -n "$x"p`
if [[ $x -eq $a ]]
then exit 0
fi
echo $snap_daily$snap_prefix
let x++
done
答案4
一种简单的解决方法是使用数组,因为结果以换行符返回:
$ IFS=$'\n' readarray -t a< <(echo $'2017-03-10-08-00\n2017-03-10-11-00\n2017-03-10-12-00\n2017-03-10-14-00\n2017-03-10-15-00\n')
$ IFS=$'\n' readarray -t b < <(echo $'test-for-dr-\ntest-for-dr-\ntest-for-dr-\ntest-for-dr-\ntest-for-dr-\n')
$ declare -p a b
declare -a a=([0]="2017-03-10-08-00" [1]="2017-03-10-11-00" [2]="2017-03-10-12-00" [3]="2017-03-10-14-00" [4]="2017-03-10-15-00" [5]="")
declare -a b=([0]="test-for-dr-" [1]="test-for-dr-" [2]="test-for-dr-" [3]="test-for-dr-" [4]="test-for-dr-" [5]="")
$ for ((i=0;i<"${#a[@]}";i++));do conc="${b[$i]}${a[$i]}";echo "$conc";done
test-for-dr-2017-03-10-08-00
test-for-dr-2017-03-10-11-00
test-for-dr-2017-03-10-12-00
test-for-dr-2017-03-10-14-00
test-for-dr-2017-03-10-15-00
另一个解决方案是仅使用 awk 脚本来组合所有文件的结果,但我们需要更多详细信息 - 也许是一个新问题。