我本质上执行一条命令,并且必须一次遍历一个文件。输出被读入数组。我只想执行一次:1.为了提高效率,并且2.目录结构不断变化,每小时添加新文件。
#Get file list
file_list=(`ls -lrt *.bin | awk '{print $9}'`)
#Get file output
output=$(for i in "${file_list[@]}"; do script.bash $i; done)
现在,使用$ output的写入方式,所有数据都驻留在单个元素$ output [0]中。要逐行提取和读取此数组,我们可以简单地使用似乎行得通的read line。
# Read $output line by line to search for specific keywords and here
# is where my problem lies
while read -r line
do
var1=$(echo "${line}" | grep keyword1)
var2=$(echo "${line}" | grep keyword2)
echo "$var1,$var2"
done <<< $output
不幸的是,上面的方法不起作用,并且终端中的结果显示空白行,这是因为var1和var2没有匹配项。我真的只是想在行中搜索特定的关键字,将其解析,将其存储在变量中,然后最后以逗号分隔的格式打印出来。
Desired Output:
Line1: $var1,$var2
Line2: $var1,$var2
单个.bin文件的输出。这些是我为每行抓取的值。
UD : JJ533
ID : 117
Ver : 8973
Time: 15545
答案 0 :(得分:1)
首先,我知道这不是您要查找的确切解决方案,并且缺少关键字查找。我仍然不确定您想获得什么,这可能是因为我的英语。
我只希望这段代码可以帮助您实现目标。
# Awk not required here if you not use long listing -l
file_list=$(ls -rt *.bin)
let "x=1"
for filename in ${file_list[@]}; do
echo '--- file #'"${x}"' ---'
let "y=1"
# as awk give output: "var1 var2", read can put them into different variable.
while read col1 col2;do
# col1 contain first column (ID UD Ver Time)
# col2 contain the value
echo "Line${y}: ${col1},${col2}"
let y++
# cat is used instead of script.bash as only its output is provided.
# awk cut down any space and remove : separator from each line.
done <<< "$(cat "${filename}" | awk -F ":" '{gsub(/ /,"");print $1 " " $2}')"
let x++
done
文件:
f1.bin <-较新
UD : JJ533
ID : 117
Ver : 8973
Time: 15545
f2.bin <-较旧
UD : ZZ533
ID : 118
Ver : 9324
Time: 15548
输出:
--- file #1 ---
Line1: UD,ZZ533
Line2: ID,118
Line3: Ver,9324
Line4: Time,15548
--- file #2 ---
Line1: UD,JJ533
Line2: ID,117
Line3: Ver,8973
Line4: Time,15545
答案 1 :(得分:0)
如果坚持使用Awk,这会更清洁。将/.*/
替换为您要匹配的任何/regex/
,否则它将打印每个文件中的所有行。
awk -F ' *: *' '
FNR==1{print "\n---", "file", ++f ":", FILENAME, "---"}
/.*/{print "Line" FNR ":", $1 "," $2}
' *.bin
输出:
--- file 1: f1.bin ---
Line1: UD,JJ533
Line2: ID,117
Line3: Ver,8973
Line4: Time,15545
--- file 2: f2.bin ---
Line1: UD,ZZ533
Line2: ID,118
Line3: Ver,9324
Line4: Time,15548
等