6

For example: I have 01.txt and 02.txt files and need to append data of these files into new file new.txt.

It should append data based on 01.txt followed by 02.txt files.

Before this I have to delete only the first AND last line of the files( 01.txt 02.txt ) and then append them into the new file.

How Do we accomplish this using UNIX?

6

Another approach:

for file in 01.txt 02.txt; do sed '1d;$d;' "$f"; done > output

This is particularly useful if there are many files you need to concatenate:

for file in *.txt; do ...

You can also use shell expansion:

for file in 0{1,2}.txt; do ...
4

You could delete the first and last line with sed:

sed -e '1d' -e '$d' file1 > output
sed -e '1d' -e '$d' file2 >> output
sed -e '1d' -e '$d' file3 >> output
4

An easy way of printing all except the first line of a file is tail:

tail -n +2 01.txt

To print all but the last line of a file, use head:

head -n -1 01.txt

So, to put all this together and print all but the first and last lines of 01.txt and 02.txt and save them as 03.txt, you can use a subshell to combine the output of the above commands on the two files:

(tail -n +2 01.txt | head -n -1; tail -n +2 02.txt | head -n -1) > new.txt
  • That requires GNU head. head -n -1 is not standard. – Stéphane Chazelas Aug 10 '13 at 8:17
  • @StephaneChazelas which part? The -n seems to be standard, is it the - format that is not? – terdon Aug 10 '13 at 11:19
  • See the POSIX spec, only positive integer numbers are supported. – Stéphane Chazelas Aug 10 '13 at 11:23
1

With a for loop, head and tail:

for f in 01.txt 02.txt; do head -n-1 $f | tail -n+2; done > newfile.txt

With python:

python -c 'print("\n".join(["\n".join(open(f).read()[:-1].split("\n")[1:-1]) for f in ("01.txt", "02.txt")]))' > newfile.txt

Or with awk:

awk 'FNR > 1 { lines[FILENAME][FNR] = $0; lines[FILENAME][0] = FNR } END { for (i=1; i < ARGC; i++) { for (i2=2; i2 < lines[ARGV[i]][0]; i2++) { print lines[ARGV[i]][i2]; } } }' 01.txt 02.txt > newfile.txt
0

With GNU sed:

sed -s '1d;$d' ./*.txt > output

Note: you want to make sure the name of the output file doesn't match the globbing pattern (*.txt above). Otherwise, you'd have to write it like:

sed -sn '1d;$d;w output.txt' ./*.txt

(assuming output.txt didn't exist beforehand).

-2
sed \$r01.txt new.txt
sed \$r02.txt new.txt
  • 1
    Can you explain how this would function, with some input examples and output? – Anthon Dec 18 '14 at 12:17

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.