I have a directory which has text files (HTML pages). I wish to use SED to go through these text files, find the specific line, and write the line to a single text file.

So for example I have HTMLS as my directory with sample text files 1,2,3,4 and these are all HTML pages. I think I could use SED search for a line command (correct me if I'm wrong) -

sed -n 'line number' 1.txt > outputfile.txt

Considering the > outputfile.txt part works, how do I make this command go through all the text files, and save all outputs to outputfile.txt ? If my implementation seems off please let me know which might be a better way. Thanks!!

  • 1
    In other words, you want to fetch the 5th line (for example) of every *.txt file in your directory, right? Did you try sed -n '5p' ?.txt > outputfile.txt? – PerlDuck Oct 1 '18 at 15:53
  • 1
    grep "line number" -r . will search recursively from . – choroba Oct 1 '18 at 15:59
  • 1
    @PerlDuck to output the 5th line of each of multiple files, you will need the -s (--separate) flag as well I think – steeldriver Oct 1 '18 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.