我的测试文件有以下行
30 * 1 = 30
30 * 2 = 60
30 * 3 = 90
30 * 4 = 120
30 * 5 = 150
30 * 6 = 180
30 * 7 = 210
30 * 8 = 240
30 * 9 = 270
30 * 10 = 300
我希望在循环中读取此文件并在=
之后打印任何内容。我也想以这种格式打印价值
Value is : 30
Value is : 60
等等。
我当前的逻辑是没有读取文件行,我无法显示格式以上的输出。
My current code:
HOME_DIR="/test/users/"
FILE_NAME="table.txt"
start_pos=10
end_pos=13
cmd=$HOME_DIR$FILE_NAME
while read LINE; do
value="$(cut -c$start_pos-$end_pos)"
echo "Value Read are : $value"
done <"${cmd}"
任何帮助将不胜感激! 谢谢, 马赫什
答案 0 :(得分:1)
在awk中,该行的最后一项称为$NF
。所以,我们打印出来:
$ awk '{print "Value is : " $NF;}' table.txt
Value is : 30
Value is : 60
Value is : 90
Value is : 120
Value is : 150
Value is : 180
Value is : 210
Value is : 240
Value is : 270
Value is : 300
在sed中,我们使用=
替换Value is :
之前的所有内容:
$ sed 's/.*=/Value is :/' table.txt
Value is : 30
Value is : 60
Value is : 90
Value is : 120
Value is : 150
Value is : 180
Value is : 210
Value is : 240
Value is : 270
Value is : 300
在shell中,我们看到每一行都有五个以空格分隔的项目。我们阅读了这些项目并再次打印出第五项:
$ while read a b c d e; do echo "Value is : $e"; done <table.txt
Value is : 30
Value is : 60
Value is : 90
Value is : 120
Value is : 150
Value is : 180
Value is : 210
Value is : 240
Value is : 270
Value is : 300