Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I need help or I will do this is python instead (by reading one line at a time) which I know I should not need to.

I have large number of text files that I get using find.

FILES=$(find ... -iname "*.txt")
num=0
for doc in $FILES; do
     echo "start string" > cat $doc > echo "end string" > "outfile$num.txt"
     let num=num+1
done

I dont know to many unix commands, but what I want to do is something like this:

output "start string" then cat $doc then "end string" to stdout and store in the outfile.

Thank you for your help!!

share|improve this question

1 Answer 1

up vote 0 down vote accepted

use

{echo "start string"; cat $doc; echo "end string"} > "outfile$num.txt"

Or alternatively:

echo "start string" > "outfile$num.txt"
cat $doc >> "outfile$num.txt"
echo "end string" >> "outfile$num.txt"

Or yet another example using sed (you can do something similar with awk):

sed -e '1i start string;$a end string' $doc > "outfile$num.txt"

In the first example the three commands are simply executed sequentially and the output is then fed as a whole to the output redirect > "outfile$num.txt".

In the second example we do it in three commands and use the >> operator instead of the > operator to concatenate the output to the given file instead of replace the given file with the output.

Also, as an aside, you should be careful with your use of for doc in $FILES, this splits on spaces, so if any of your filenames contain spaces they will be interpreted as multiple file names instead of one file name.

share|improve this answer
    
Thank you, regrettably I have too much work to do (and too little time) to read up on Unix and different utility programs. Even though my question was probably a dumb question, stress really makes you not think clearly. You have made my day much better, thanks again! :) –  Erik Sep 12 '12 at 5:22
    
The first example needs a little tweaking: { echo ...; cat $doc; echo ...; }. Spaces to separate the braces from the contents, and a required semicolon after the last echo. –  chepner Sep 12 '12 at 15:54

Your Answer

 
discard

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.