I have two tmp files named tmp1 and tmp2 which contains some lines.
tmp1 file,

datafile no. 23  
datafile is ok

tmp2 file,

datafile no. 24  
datafile is ok

I have a file (named wrong_file) which entries I want to correct from tmp files

datafile no. 32  
datafile is ok  
datafile no. 42  
datafile is ok

My output file (modified_file) will be like,

datafile no. 23  
datafile is ok  
datafile no. 24  
datafile is ok    

I want use for loop which will run until the last tmp file and write data from tmp files to final file (Output) instead doing it manually.

I have tried,

sed -f tmp1 wrong_file > file1  
sed -f tmp2 file1 > modified_file

I'm pretty sure you can just do:

cat ./tmp[12] | sed -f - ./wrong_file >outfile

At least, that will not cause any issues if all of sed's script instructions are specific to line number. There's no need to apply the scripts separately - you can chain them all together and run the script at once.

That you have to do this at all, though, is indicative of duplicated work. Here is a copy of a sed script which would avoid writing any of those tempfiles in the first place and simply scan input for the lines which need changing before passing all of the script in a stream to the final sed in one go:

{   sed '/^#\.tid\.setnr/!d;=;g;G' |
    paste  -d'c\\\n' - - - ./addfile
}   <./mainfile | sed -f - ./mainfile

Its output is not identical to your sample data here because it is tailored to the samples you provided in the other question. But it avoids writing out the modification scripts at all and sends all editing commands to a sed process which can take action immediately.

In general you can consider that a sed process is just as ready to handle its script input in all of the ways it might also be ready to handle its edit content input.-

  • cat ./tmp[12] means tmp1 and tmp2. Is it? I want my system to count tmp files and write it to the final file. Then what will be my command? Is it cat ./tmp[*] | sed -f - ./wrong_file >outfile – pmaipmui Jun 12 '15 at 20:30
  • @Nainita - yeah, your shell should expand the [12] glob to two arguments. I would just use it for sed like sed -f ./tmp1 -f ./tmp2 infile >outfile but I just wanted to show that you can string any number together - and I thought it might be possible that you could have more than two eventually. – mikeserv Jun 12 '15 at 20:32
  • Suppose I dont know how many tmp file has been created and I want to write those tmp files data like this way. Then what will be the command? Should I have to put * instead of [12] in cat ./tmp[*] ? – pmaipmui Jun 12 '15 at 20:35
  • @Nainita - you might, but be sure your globs are restrictive enough not to include any sed commands you don't want, is all. But yes, * will work as well as [num-num], it's just that it is more inclusive. – mikeserv Jun 12 '15 at 20:36
  • @Nainita - by the way, I'm fairly sure I know where those came from, too. If you had used the answer I offered to that other question, you probably would not have ever needed to create any tempfiles at all. – mikeserv Jun 12 '15 at 20:39

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.