1

I am using the following sed command to find a string in between patters for each file in a folder, files are named 01.txt, 02.txt etc.:

sed -n '/PAT1/,/PAT2/{/PAT1/!{/PAT2/!p}}' * >> file

but the output is not sorted by the same order of files in the directory, rather by some other order. I did try to pipe out to sort but the order is then spread across ALL matches/All files which is not what I am looking for. I want the output to be appended in the same 01.txt, 02.txt order. I've seen grep doing the same, finding matches and jumping from one file to another. I prefer using sed but grep is an option. thanks.

2
  • try sorting the output with sort command. maybe you want to run sort, after you generate your file, IDK. man sort will give you some more options. Posting a sample of your input, or output could be useful to us, if you want something sorted in a more specific way.
    – j0h
    Aug 26, 2020 at 18:02
  • @j0h i did use 'sort' se my question above
    – rearThing
    Aug 26, 2020 at 18:29

1 Answer 1

0

Had to resort to a script, still one line:

for f in file*.txt; do sed -n 'PAT1/,/PAT2/{/PAT1!{/PAT2};}' $f >> outputfile; done

You must log in to answer this question.

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