w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Problems for Extracting data from multiple files with awk

I think you want something like:

for i in
{114,182,251,131,183,257,140,191,31,148,192,48,151,195,51,92,177,249} ; do 
  awk 'BEGIN {ORS=" "} NR==2 || NR==4' f_$i.dat
  echo "" 
done > merged-file.dat

There were a few problems with your for-loop

  1. do was missing from the for-loop;
    for-loops are structured like for i in $LIST ; do ... ; done
  2. the $LIST part was invalid

  3. -v num=$i was not needed as the num variable wasn't even used in the awk expression
  4. you wanted to merge the output in the same file, so putting > $i.dat in the for-loop wouldn't work as they output to separate files;
    • you need to place > $OUTFILE.dat outside the for-loop or
    • append (>>) the output of each iteration of the for-loop to the same file with something like:
      for i in ... ; do ... >> OUTFILE.dat ; done

© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.