fork download
  1. #!/bin/bash
  2. # http://stackoverflow.com/questions/29022853/using-awk-on-multiple-input-files
  3.  
  4. # Create data files for demo
  5.  
  6. d=$(mktemp -t -d demoXXXXXXXX) || exit 127
  7. trap 'rm -rf "$d"' EXIT ERR
  8. cd "$d"
  9.  
  10. cat <<':' >main.csv
  11. x11,x21
  12. x12,x22
  13. x13,x23
  14. x14,x24
  15. x15,x25
  16. x16,x26
  17. x17,x27
  18. x18,x28
  19. x19,x29
  20. :
  21.  
  22. cat <<':' >other.csv
  23. out_file_name_1,2
  24. out_file_name_2,3
  25. out_file_name_3,4
  26. :
  27.  
  28. # The Beef
  29. while IFS=, read -u 3 filename lines; do
  30. head -n "$lines" >"$filename"
  31. done 3<other.csv <main.csv
  32.  
  33. # Proof
  34. tail out*
  35.  
  36. echo ----
  37. bash --version
Success #stdin #stdout 0s 5080KB
stdin
Standard input is empty
stdout
==> out_file_name_1 <==
x11,x21
x12,x22

==> out_file_name_2 <==
x13,x23
x14,x24
x15,x25

==> out_file_name_3 <==
x16,x26
x17,x27
x18,x28
x19,x29
----
GNU bash, version 4.3.30(1)-release (i586-pc-linux-gnu)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://g...content-available-to-author-only...u.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.