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.

  • 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 at 18:02
  • @j0h i did use 'sort' se my question above – rearThing Aug 26 at 18:29
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

| improve this answer | |

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.