我使用下面的命令合并 file1 和 file2 以获取 file3:
awk 'NR==FNR {a[$2]=$1; next} {$(NF+1) = a[$NF]} 1' file2 file1 > file3
当我使用 expect 在 bash 脚本中使用此命令时,我得到的 file3 为空(该命令正在手动运行)。
#!/bin/bash
# Bash Menu Script Example
outputMME="$( expect <<END
spawn ssh [email protected]
expect "Password: " { send "password\r" }
expect "# " { send "bash\r" }
expect "$ " { send "cd /tmp/DPE_CORE/home/atndn/eniq/\r" }
expect "$ " { send "awk 'NR==FNR{a[$2]=$1; next} {\\\$(NF+1) = a[$NF]} 1' file2 file1 > file3\r" }
END
)"
echo "$outputMME"
文件1:
471808241 29164840 1 10001 156197396
471722917 21067410 1 31001 135961856
471941441 20774160 1 7001 180995072
471568655 29042630 1 15001 157502996
471524711 20716360 1 4001 180226817
471873918 29583520 1 2001 128567298
471568650 29042631 1 15002 157502910
文件2:
610146 156197396
531101 135961856
704011 180226817
502216 128567298
707012 180995072
615246 157502996
685221 157502910
文件3:
471808241 29164840 1 10001 156197396 610146
471722917 21067410 1 31001 135961856 531101
471941441 20774160 1 7001 180995072 707012
471568655 29042630 1 15001 157502996 615246
471524711 20716360 1 4001 180226817 704011
471873918 29583520 1 2001 128567298 502216
471568650 29042631 1 15002 157502910 685221
答案1
为了实施steeldriver的评论:
outputMME="$(
expect <<'END'
#....^ [1]
spawn ssh [email protected]
expect "Password: " { send "password\r" }
expect "# " { send "bash\r" }
expect "$ " { send "cd /tmp/DPE_CORE/home/atndn/eniq/\r" }
expect "$ " {
send {awk 'NR==FNR{a[$2]=$1; next} {$(NF+1) = a[$NF]} 1' file2 file1 > file3}
#....^ [2]
send "\r"
}
expect "$ " { send "exit\r" }
expect "# " { send "exit\r" }
expect eof
END
)"
- 您需要引用 heredoc 终止符 (
'END'
),以便 bash 不会在文档中执行变量(和其他)替换。 - 您需要正确引用 awk 命令,以便 expect 不会替换 awk 变量。