0

I have the below block

while read -r line
do
  bad=$(cat node.txt  | grep -A1 success | grep -vi success | grep -i '|2|' | sed 's/|2|/ /g' | awk -v count_num=$count 'FNR==count_num')
  good=$(cat node.txt  | grep -A2 success | grep -vi success | grep -i '|1|'  | sed 's/|1|/ /g' | awk -v count_num=$count 'FNR==count_num')
  printf "| %-15s | %30s | %30s |\n" "$line" "$bad" "$good"
  count=$((count+1))
done <<< "$node"

Which prints what I want in the right form. The column 3 is what I want to sort. If I

./script.sh | awk 'NR<4{print $0;next}{print $0| "sort -r -k3"}'

This sorts well without moving the column headers well. I want this to be incorporated into the script itself, so there is no need to run the script with awk.

I tried adding it after the loop like:

done <<< "$node" | awk 'NR<4{print $0;next}{print $0| "sort -r -k3"}'

But it does not seem to work. Any idea? Thanks

  • One thing I could do is to output the result to a temp file. and sort the tem file with the awk sort command and remove the file upon result. – suhas savkoor Jan 7 at 5:46
  • 1
    Your code makes no sense. The values of bad and good are not influence by $line. Why do you need the loop at all? Why are you re-reading the whole node.txt file twice in each iteration? What does the input and output data look like? – Kusalananda Jan 7 at 7:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.