I am using bash on my macbook pro. I have a list of files in a directory that I'd like to use the cut command on. When I use this command:

cut -d " " -f 6- 2.ostat > 2.ostat1

it works nicely - a new file is created and it contains the desired information

When I try to use it in a loop like this:

for num in *.ostat; do cut -d " " -f 6- ${num}.ostat > ${num}.1; done

it creates new files for all but the files are blank.

Is is possible to use a single (and for a beginner understandable) command to cut out the information I need?


$num (or ${num}, the braces aren't necessary, references the files. $num.ostat will produce 1.ostat.ostat, 2.ostat.ostat ...

for num in *.ostat; do cut -d " " -f 6- $num > $num.1; done

should work.

  • Can we see a sample file please? – Mark Williams Mar 9 '15 at 11:12
  • The content in the file just didn't show immidiately :) – Ditte Mar 9 '15 at 11:12
  • do I understand correctly, that if I'm in the "desired" directory which contains only files that I want to run my loop on, I do not need to write the files' extension? the scrip will just take everyhting that is there and run it on all? – Ditte Mar 9 '15 at 11:14
  • Yes; you can use for i in , and get all files; or specify a filter as in *.ostat, or [2-4].ostat & get all files starting in 2,3, or 4 with that extension, or whatever combination you like. Or you could do for i in {1..5} & use the index as you were originally trying to do. – Mark Williams Mar 9 '15 at 11:17
  • Thanks so much! It's invaluable to get this advice as a beginner :) – Ditte Mar 9 '15 at 11:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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