1

a) i have one huge file, from which i need to extract all the lines that match specific patterns, letus say paterna and patternb. So i need to extract all lines that start with patterna something like ^patterna, and all lines, that start with patternb something like ^patternb. Then write the output to a file named patternapatternb.txt

b) how could i make this work with a loop. For example, do the same for patterna2 and patternb2, then do the same with patterna3 and patternb3, so the file will be parsed many times, with new pairs of patterns to match lines each time, and finally the output then would be three files:

patternapatternb.txt
patterna2patternb2.txt
patterna3patternb3.txt

sample input
001 876786
989 74563
734 87387


600: sometext
601: someothertext

001 712345
345 87238


600: sometext
702: differenttext

001 5342
989 745632
734 873872


600: sometext
601: someothertext

001 987 345 87238


600: sometext
702: differenttext

patterna: ^001 patternb: 600: sometext

the output would be a file named 600: sometext.txt

001 876786
600: sometext
001 712345
600: sometext
001 5342
600: sometext
001 987
600: sometext

Actually, the first element in the pair of patterns, will be the ^001, and the second, each occurrence of a line that starts with three digits, followed by a :, then a space

closed as unclear what you're asking by Scott, HalosGhost, sam, Anthon, GAD3R Oct 4 '16 at 10:11

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • one shot would be great, how would you do this? – nupu bebi Sep 21 '16 at 13:27
  • $ sed -n -e '/Storage/w output.txt' -e '/Sysadmin/w output.txt' thegeekstuff.txt i know how to do it with every instance, what i do not know, is how to make it in a more automatic way if i need to make this for 100 pairs of patterns, do i need to run the above command 100 times? – nupu bebi Sep 21 '16 at 13:33
  • i could write them to a file, better if we could identify these pairs of patterns, from the sample input file you may see in the edited post – nupu bebi Sep 21 '16 at 14:00
1
awk '/patterna/ || /patternb/ { print > "patternapatternb.txt" }
     /patternc/ || /patternd/ { print > "patterncpatternd.txt" }
     # ... more rules following same scheme; replace with real patterns ...
     /patterny/ || /patternz/ { print > "patternypatternz.txt" }' inputfile

Put the code in a script.awk file and call with awk -f script.awk inputfile. Or else make a multi-line command line as above. In the GNU Bash shell, a newline can be inserted using Ctrl-V Ctrl-J*

There is only one loop (the implicit record-processing loop carried out by awk). Only one pass through the file is required.


* You can use Enter to add a newline between quotes only when adding new characters at the end of the line and an open quote is outstanding.

  • Good answer. But I'm not sure what you're saying about Ctrl+J. I have no trouble just typing <Enter> inside quotes in bash. – Scott Oct 4 '16 at 3:18
  • 1
    @Scott Not if the quote is already closed; only while you're adding characters to the line and an open quote is outstanding. Try typing 'foo bar' and then go back to add the newline afterward. – Kaz Oct 4 '16 at 4:17
  • OK; that was interesting and educational. – Scott Oct 4 '16 at 4:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.