e.g。命令
./ show PH
然后bash脚本将打印每行最后一列上有PH的所有行
输出应该是:test sample 1234 234543 111 T x 6 PH
test sample 1234 234543 111 T x 6 PH
test sample 1234 234543 111 T x 6 PH
test sample 1234 234543 111 T x 6 PH
test sample 1234 234543 111 T x 6 PH
test sample 1234 234543 111 T x 6 PH
test sample 1234 234543 111 T x 6 PH
test sample 1234 234543 111 T x 6 PH
test sample 1234 234543 111 T x 6 PH
file.txt {content}
test sample 1234 234543 111 T x 6 KR
test sample 1234 234543 111 T x 6 KR
test sample 1234 234543 111 T x 6 AU
test sample 1234 234543 111 T x 6 DE
test sample 1234 234543 111 T x 6 PE
test sample 1234 234543 111 T x 6 PH
test sample 1234 234543 111 T x 6 PH
test sample 1234 234543 111 T x 6 PH
test sample 1234 234543 111 T x 6 PH
test sample 1234 234543 111 T x 6 CA
test sample 1234 234543 111 T x 6 PH
test sample 1234 234543 111 T x 6 PH
test sample 1234 234543 111 T x 6 PH
test sample 1234 234543 111 T x 6 PH
test sample 1234 234543 111 T x 6 PH
test sample 1234 234543 111 T x 6 AU
test sample 1234 234543 111 T x 6 CA
test sample 1234 234543 111 T x 6 CA
test sample 1234 234543 111 T x 6 US
test sample 1234 234543 111 T x 6 US
test sample 1234 234543 111 T x 6 US
test sample 1234 234543 111 T x 6 US
答案 0 :(得分:0)
我会使用awk
:
awk -v search='PH' '$NF==search' file
程序检查行中的最后一个字段是否包含值PH
,如果此值为true,则awk将打印该行作为默认操作。请注意,NF
是awk中的字段数。 $1
访问第一个字段,$2
访问第二个字段,依此类推。 $NF
可用于访问最后一个字段。
这样你的bash脚本应如下所示:
#!/bin/bash
awk -v search="$1" '$NF==search'
答案 1 :(得分:0)
使用sed,show.sh
:
#!/bin/bash
sed -n "/[ \t]*${1}$/p" file
./show.sh AU
输出:
test sample 1234 234543 111 T x 6 AU
test sample 1234 234543 111 T x 6 AU
答案 2 :(得分:0)
用Grep:
#!/bin/bash
#Get Code from command line
CountryCode="$1"
MyFile="File.txt"
if [[ ! "$CountryCode" ]]; then
echo "No CountryCode Supplied"
exit 1;
fi
grep -P "$CountryCode$" "$MyFile"
然后你可以这样称呼它:
./show.sh "PH"