Apply sed command to multiple files with similar names with increasing numbering

Viewed 25

I have 20 text files named as follows: samp_100.out, samp_200.out, samp_300.out, ... ,samp_2000.out. The naming is consistent and the numbering increases by 100 until 2000. I want to make a short script to (1) delete the first line of each script, and (2) apply the following command to each one of the files: sed 's/ \+/,/g' ifile.txt > ofile.csv while keeping the naming the same when changed to a .csv extension

I am assuming I need to use a for loop, but I am not sure how to iterate through the file names.

1 Answers

This might work for you (GNU sed and parallel):

parallel "sed '1d;s/ \+/,/g' samp_{}.out > samp_{}.csv" ::: {100..2000..100}

Use GNU sed, GNU parallel and braces expansion, to delete first line and replace one or more spaces globally by commas for desired files and make copies.

Alternative:

for i in {100..2000..100}
do sed '1d;s/ \+/,/g' samp_${i}.out > samp_${i}.csv
done
Related