0

I have the following command that takes a certain part of a file in the current directory, writes it to a temporary file, gets a part of another file one directory up, writes it to a temporary file and then finally concatenates these two temporary files into one and outputs it to result:

sed -e '3003,$d' file1 > temp1;sed -n '3,$p' ../otherfile1 > temp2; cat temp1 temp2 > ../result1

This works for one file, but now I want to do this to a whole directory.

I thought that using a wildcard in the sed command would work:

sed -e '3003,$d' file* > temp1;sed -n '3,$p' ../otherfile* > temp2; cat temp1 temp2 > ../result*

but then of course it doesn't automagically know how to number the temporary files and the output file.

How can I use this command on a whole directory, outputting a unique result file for every concatenation?

2

Do it as a loop in a bash shell rather than pass the entire list to sed.

let "n = 1"
for file in *
do
  sed -e '3003,$d' $file > temp1;sed -n '3,$p' ../$file > temp2; cat temp1 temp2 > ../result${n} ;
  let "n = n + 1"
done
  • How do I change the result filename for every iteration? My goal is to get result1, result2, result3, etc. With the current loop it overwrites the result1` file. – Saaru Lindestøkke Jan 16 '14 at 16:39
1

Using a subshell, one can even avoid the "temp1" and "temp2" files. Also, if the filenames contain spaces, I tend to use "while":

n=1
ls | while read f; do
  ( sed -e '3003,$d' $f && sed -n '3,$p' ../$f ) > ../result${n}
  n=$((n+1))
done
  • Neat. Didn't think of that but a good solution to his problem. I also noticed the shell expansion math - I always like to be explicit though :) – Tony Williams Jan 17 '14 at 9:06
  • You mean the "((...))" thinghy? I did that to avoid the bashism of "let", as it's not portable. – ckujau Jan 20 '14 at 10:59
  • Yeah, that's what I meant. I'm such an idiot with math I prefer to spell it out :) – Tony Williams Jan 20 '14 at 11:37

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.