How would I apply a logic to all files in a directory and pipe the final output of each input file to a separate file? Something like below
cut -d, -f2 input1.csv | awk 'END{print NR}' > input1_count.csv
(actual command is long, but I used above for simplicity to better understand the logic to all the files in a directory)
Advertisement
Answer
You can use a for-loop over the filenames:
shopt -s nullglob # enable nullglob
for f in *.csv; do
cut -d, -f2 "$f" | awk 'END{print NR}' > "${f%.csv}_count.csv"
done
shopt -u nullglob # disable nullglob
The glob pattern *.csv expands to a null string with nullglob enabled (instead of a literal and probably non-existing filename *.csv).
In the loop, apply the pipeline of commands to each filename and redirect the output to a new filename.
The parameter expansion ${f%.csv}
removes the shortest suffix pattern .csv from the filename.