2

the following lines append the content of essay_i.txt to the end of essay1.txt;

touch essay1.txt
for (( i = 1 ; i <= 10 ; i++ )); do
sed '2p' essay_"${i}".txt >> essay1.txt
done

how should i change it so that the first line of each of essay_i.txt are not copied (i.e. only copy line 2->end) ?

  • Is it the appending you want, or the sed version? For straight shell, you could use ` tail -n +2 essay_i.txt >> essay1.txt ` – belacqua Feb 7 '11 at 16:56
  • 1
    Your script copies the seconds line twice, is that intentional? (actually, it doesn't work at all for me, so I'm guessing) – Stefano Palazzo Feb 7 '11 at 16:59
  • @ Stefano Palazzo: sorry my bad (typping error). Thanks. – user2413 Feb 7 '11 at 17:18
  • You can do it without the loop too: awk 'FNR>1' essay_[0-9].txt essay_[1-9][0-9].txt > essay1.txt. Would be even shorter if the numbers in the filenames were zero-padded. – geirha Feb 7 '11 at 20:55
8

To suppress the first line, use '1d' (i.e. line 1, delete):

touch essay1.txt
for file in essay_*; do
  sed '1d' $file >> essay1.txt
done

the output will be

~$ cat essay1.txt 
line 2 from essay 1
line 3 from essay 1
line 2 from essay 2
line 3 from essay 2
...

for all of the files named essay_* in the current working directory.


Instead of using sed, you can also use tail to do this job:

tail -n +2 $file >> essay1.txt

Where +2 means from the second line onwards (-n 2 would be the last two lines).

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.