1

I need to sed from all *.txt files from line 6 until the end All the results should be in a new single file with the name of each file preceding the results

Example: Having different txt files the result should be one single that looks like this taking from the 3rd line to the end

file 1
3
4
5
file 2
3
4
5
file 3
3
4
5
1
  • Please edit your question and show us i) your input files and ii) your desired output. Showing ambiguous output (should the file names be in the output or not?) and no input is not very helpful since we can't help get you from here to there unless we know what here is.
    – terdon
    Mar 14, 2016 at 13:16

1 Answer 1

1

With GNU sed:

sed -s '1F;6,$!d' -- *.txt > output

Portably, you could use awk instead:

awk 'FNR == 1 {print FILENAME}; FNR>=6' ./*.txt > output

(note that in both cases, hidden files are excluded).

Empty files won't be referenced, but files that have fewer than 6 lines will. If you don't want to hear about those, you can do:

sed -s '6F;6,$!d' -- *.txt > output
awk 'FNR == 6 {print FILENAME}; FNR>=6' ./*.txt > output

Or if you want to see all files:

for f in *.txt; do
  printf '%s\n' "$f"
  tail -n +6 < "$f"
done > output

With GNU tail, you also do:

$ tail -vn +6 -- *.txt
==> a.txt <==
6
7
8
9
10

==> b.txt <==
6
7
8
9
10
2
  • Thank you, this is perfect I will be using awk is there an edit to this function to make my file look as follow original file: 'file 1 1 2 3 4 5 Done' file needed 'file 1 3 4 5 file 2 3 4 5 6' while having an empty line in between and while removing the last line which is "done"
    – Chris
    Mar 14, 2016 at 13:32
  • @Chris, I'd suggest asking a separate question for that. Mar 14, 2016 at 14:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .