Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have ~2k files that I want to extract 12 of 26 columns and save the extracted information into a new file. I have been doing this for individual files using

cat "oldfile.csv" | grep -v "=" | cut -f 1-4,9,14,15,19,21,22,24,26 > newfile.csv

There must be a way to do all files with one Unix series of commands.

Any help is greatly appreciated

share|improve this question

The shell can do loops:

for i in *.csv; do 
    grep -v "=" "$i" | cut -f 1-4,9,14,15,19,21,22,24,26 > "new.$i"

Although, if you have embedded field separators in your fields, this will bomb. But not because of the loop.

share|improve this answer
$i needs quoting, or it will break on filenames containing characters in IFS. – Chris Down Jan 30 '12 at 17:07
Spot on. I stand corrected. – Alien Life Form Jan 30 '12 at 17:13

If you want to provide particular files as argument to a script e.g. script named process_csv.sh

for arg; do
    grep -v "=" "${arg}" | cut -f 1-4,9,14,15,19,21,22,24,26 > "${arg}.new"

Now you can run this script as following,

./process_csv.sh *.csv OR

./process_csv.sh first.csv fifth.csv seventh.csv OR

./process_csv.sh somefile1 somefile2

Before somebody points it out, in the above script, if you do not want to preserve the arguments you can use shift in the script.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.