Printing data at the end of a nested while loop [BASH] -
i'm hoping stack can save me ... again ...
here's code -- below explain how can save me!
#!/bin/bash while ifs= read -r input <&3 if [[ "$input" =~ ^(\/path\/to\/my\/file\/)(foo[0-9]{2}\.bar\.[0-9]{14}\.baz)$ ]] file="${bash_rematch[2]}" else echo "unknown input!" exit 1 fi printf "\n\n%s" "reading input ($file) ... " while ifs= read -r line if [[ "$line" =~ ^(v22200)(.*)$ ]] #version 2.2 if [[ "$version" = "2.2" ]] && [[ "$column082" =~ ^(pp)([a-z0-9]{3})([0-9]{6})$ ]] if [[ "${bash_rematch[2]}" = "foo" ]] output_path="/path/to/my/file/${file}_${bash_rematch[2]}_${version}${ext}" if [[ ! -f "/path/to/my/file//${file}_${bash_rematch[2]}_${version}${ext}" ]] headers='1' fi fi if [[ "$headers" -eq '1' ]] printf '%s\n' "337 column headers go here" > "${output_path}" fi headers='0' if [[ "$version" = "2.2" ]] && [[ "$column082" =~ ^(pp)([a-z0-9]{3})([0-9]{6})$ ]] && [[ ! -z "$output_path" ]] && [[ "$column031" -eq '0' ]] printf '%s\n' "$column001,...,$column337" >> "$output_path" fi fi column014=$(echo "$line" | cut -b7 | tr -d '[:space:]') column015=$(echo "$line" | cut -b8-17 | tr -d '[:space:]') column016=$(echo "$line" | cut -b18 | tr -d '[:space:]') column017=$(echo "$line" | cut -b19 | tr -d '[:space:]') column018=$(echo "$line" | cut -b20-21 | tr -d '[:space:]') column019=$(echo "$line" | cut -b22-23 | tr -d '[:space:]') column020=$(echo "$line" | cut -b24 | tr -d '[:space:]') column021=$(echo "$line" | cut -b25-26 | tr -d '[:space:]') column022=$(echo "$line" | cut -b27 | tr -d '[:space:]') column023=$(echo "$line" | cut -b28-37 | tr -d '[:space:]') column024=$(echo "$line" | cut -b38-43 | tr -d '[:space:]') column025=$(echo "$line" | cut -b44-58 | tr -d '[:space:]') column026=$(echo "$line" | cut -b59-62 | tr -d '[:space:]') column027=$(echo "$line" | cut -b63-74 | tr -d '[:space:]') column028=$(echo "$line" | cut -b75-80 | tr -d '[:space:]') column029=$(echo "$line" | cut -b81-84 | tr -d '[:space:]') column030=$(echo "$line" | cut -b85-88 | tr -d '[:space:]') column031=$(echo "$line" | cut -b89-90 | tr -d '[:space:]') column032=$(echo "$line" | cut -b91-96 | tr -d '[:space:]') column033=$(echo "$line" | cut -b97-115 | tr -d '[:space:]') column034=$(echo "$line" | cut -b116-127 | tr -d '[:space:]') fi done < "${input}" printf "%s\n" "done!" mv "$input" /my/path/to/the/processed_files done 3</path/to/my/results.txt
that's truncated version of i'm doing, hope enough convey issue. have 2 while
loops nested. results file generated find
searching directory files match regex pattern. results stored in results.txt
, file fed "file descriptor 3", assigned variable "$input". "$input" read line line in while read
loop.
as hope can see, when reach line (v22200) need print values of 337 columns. , fine, reach end, , loops on again. problem when comes last block of file. since print statement isn't last thing do, before exit, values have in last block assigned whatever column variable belong, since we're @ end, there no need loop again , print statement never gets write last block out file, makes effective discarded data.
i imagine simple answer move print statement down, in doing overwrite values have in column014-column034 (from example above) ...
i've read lot detecting eof or not printing last line, or other strange things happen @ end of loop -- don't think that's problem. saw 1 answer suggested write "$line" variable , echo
after done
statement, have 337 values need printed on each pass ... doesn't seem feasible me.
i appreciate support or suggestions might have on how last block written, since loop on , command print never comes.
p.s. -- yes, know indentation isn't traditional, or perhaps proper, way learned it. sorry.
Comments
Post a Comment