I essentially execute a command and have to iterate over it one file at a time. The output is read into an array. I want to do this only one time: 1. for efficiency and 2. the directory structure is constantly changing with new files being added hourly.
#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)
Now with the way $output is written, all data resides in a single element $output[0]. To extract and read this array line by line we can simply use read line which seems to work great.
# 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
Unfortunately the above is not working how I want it and the result within the terminal prints blank lines and that is because well, var1 and var2 don't have a match. I'm really just trying to search the line for a specific keyword, parse it, store it in a variable and then finally print it in comma delimited format.
Desired Output:
Line1: $var1,$var2
Line2: $var1,$var2
Output for a single .bin file. These are the values I'm grepping for each line.
UD : JJ533
ID : 117
Ver : 8973
Time: 15545
ls?done <<< $outputwith this you not preserve new lines when you pass the text,$outputneed to be double quoted.script.bashis do with the file name? How is the output look like?