-1

Print the first 123 below XXX

AAA
BCB
DDD
123
123
XXX
CCC
123
123
MSM
123

closed as unclear what you're asking by Jeff Schaller, G-Man, Archemar, GAD3R, RalfFriedl Dec 8 '18 at 17:05

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.

  • AAA is the first line BCB is second line... – youngchaw Dec 7 '18 at 14:37
  • Hello and welcome to the U&L stack exchange site! Please review the Help Center to get information on how to best post to this site. To get to your question, please edit your post to include additional context Include what you have tried so far and what is not working. Thank you! – kemotep Dec 7 '18 at 14:47
  • 2
    What is different about the first 123 after XXX? Would a simple echo 123 do what you need? – JigglyNaga Dec 7 '18 at 14:52
  • @JigglyNaga Probably, if you were sure that such a line occurred in the input, in the way specified. – Kusalananda Dec 7 '18 at 15:01
  • The difference is the first one "under XXX" :) – youngchaw Dec 7 '18 at 15:08
3
awk 'NR == 1, /XXX/ {next}; /123/ {print; exit}' < input.txt

would skip all the lines up to the first one that contains XXX and then print the first line that contains 123 in the remaining lines.

For arbitrary strings stored in variables:

START='XXX' NEEDLE='fancy string with " and *' awk '
  NR == 1, index($0, ENVIRON["START"]) {next}
  index($0, ENVIRON["NEEDLE"]) {print; exit}' < input.txt
  • Thanks a lot, it is working!! How can I change this if the 123 is "prefer A B" included a space and quotation mark? – youngchaw Dec 7 '18 at 15:04
  • @youngchaw, just change /123/ to /"prefer A B"/. See also edit in case your string may contain slashes or regexp operators – Stéphane Chazelas Dec 7 '18 at 15:15
2
$ sed -n '/XXX/,$ { /123/ { p; q; }; }' <file
123

This applies the p command to print the current line, followed by the q command to quit, to a line that matches 123. The test for 123 is done on all lines between the first that matches XXX and the end of the input (inclusively).

To exclude the XXX lines (the 123 must not occur on the same line as a XXX line):

$ sed -n '/XXX/,$ { /XXX/d; /123/ { p; q; }; }' <file
123
  • Tighter: <infile sed '1,/XXX/d ; /123/!d ; q' – Isaac Dec 7 '18 at 15:03
  • That starts checking for 123 with the first matching XXX, not the first line after that. – Stéphane Chazelas Dec 7 '18 at 15:09
  • @Isaac, that wouldn't work properly if the first occurrence of XXX was on the first line. Better with /XXX/,$!d. But that could also print the XXX line if it contained both XXX and 123 – Stéphane Chazelas Dec 7 '18 at 15:11
  • @StéphaneChazelas (1) That starts checking for 123 with the first matching XXX, not the first line after that. Yes exactly as this answer also did. (2) First line? Use sed '0,/XXX/d… with GNU sed (3) It doesn't seem like the input values will have XXX and 123 on the same line. But, if that is a requirement, and the use of old sed is also required, use: sed '/XXX/,$!d;/XXX/d;/123/!d;q'. – Isaac Dec 7 '18 at 15:43
  • This answer also works. Thanks a lot for your answers! – youngchaw Dec 10 '18 at 14:17

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