0

context: grep for multiple strings in file on different lines (ie. whole file, not line based search)?

egrep -nH 'Dansk|Norsk|Svenska' file3

or using ripgrep

rg  'Dansk|Norsk|Svenska' file3

But there is an answer

awk '/Dansk/{a=1}/Norsk/{b=1}/Svenska/{c=1}END{ if (a && b && c) print $0 }'  file3

I don't understand this answer.

I understand awk '/Dansk/{a=1}/Norsk/{b=1}/Svenska/{c=1} .

I think now the question is using awk, while pattern matched, print line number with output lines.

update: I even understand this answer. update:
almost close:

awk '/Dansk/{a=1}/Norsk/{b=1}/Svenska/{c=1}{ if (a || b || c ) print $0; next}1'  file3

it return

Subtitle: 04, Language: da - Dansk, Content: Undefined, Stream id: 0x23,
        Subtitle: 05, Language: de - Deutsch, Content: Undefined, Stream id: 0x24,

the expected return is

   Subtitle: 04, Language: da - Dansk, Content: Undefined, Stream id: 0x23,

file3 content:

Disc Title: unknown
Title: 01, Length: 01:33:37.000 Chapters: 33, Cells: 31, Audio streams: 04, Subpictures: 20
        Subtitle: 01, Language: ar - Arabic, Content: Undefined, Stream id: 0x20, 
        Subtitle: 02, Language: bg - Bulgarian, Content: Undefined, Stream id: 0x21, 
        Subtitle: 03, Language: cs - Czech, Content: Undefined, Stream id: 0x22, 
        Subtitle: 04, Language: da - Dansk, Content: Undefined, Stream id: 0x23, 
        Subtitle: 05, Language: de - Deutsch, Content: Undefined, Stream id: 0x24, 
(...)

update. almost close.
awk '/Dansk/{a=1}/Norsk/{b=1}/Svenska/{c=1}{ if (a || b || c ) print NR, $0}' file3 But it returns

6         Subtitle: 04, Language: da - Dansk, Content: Undefined, Stream id: 0x23,
7         Subtitle: 05, Language: de - Deutsch, Content: Undefined, Stream id: 0x24,
8 (...)
6
  • other answer has explanation, which part you don't understand? or you understood as you have written understand. also please don't point to a code by just giving the link to it, but also mention exact code in your question. Jan 13, 2022 at 6:17
  • @αғsнιη I am almost close. I get the matched line and the next line. I don't want next line. see my updates.
    – jian
    Jan 13, 2022 at 6:25
  • x||y||z||... means "if x OR y OR z OR ... were True" then do something. x&&y&&z&&... means "if x AND y AND z AND ... all were True" then do something."True" means a result of an expression or condition evaluated true. I believe you strongly need to start first reading some basics of awk commnad. Jan 13, 2022 at 6:29
  • @αғsнιη to clarify my question. to emulate rg 'Dansk|Norsk|Svenska' file3 command which return ` Subtitle: 04, Language: da - Dansk, Content: Undefined, Stream id: 0x23,` but now my awk idea return the matched line and the next line. I only want matched line. now I don't know how to improve it.
    – jian
    Jan 13, 2022 at 6:35
  • 1
    about awk '/Dansk/{a=1}/Norsk/{b=1}/Svenska/{c=1}{ if (a || b || c ) print NR, $0}' you can just do awk '/Dansk|Norsk|Svenska/{print NR, $0}', but I really don't know what are you trying to do here at all. what is your question and goal now? Jan 13, 2022 at 7:01

1 Answer 1

1

awk is consist of pair of condition(s) & action(s) which condition part can be any arithmetic expression or pattern matching or result of one-or-more expression which all of these result can be true or false statement;

and here, the given sample is used a pair of pattern matching and actions which is:

awk '/if-this-regex-exist-in-current-line/ { "yes, so do something" }' infile

all of 3 doing the same pattern matching and set a temporary variable flag if those regex found in a line.

then at the END{} block, which this block only runs once after all records/line of the input infile file processed, which it simply checking if those all flags were set (means all 3 patterns were found either those were found in a single line or in while input file), then print the last line (you might wanted to print FILENAME instead or something else) of the input file.


Answer to the second added question about:

awk '/Dansk/{a=1}/Norsk/{b=1}/Svenska/{c=1}{ if (a || b || c ) print $0; next}1' file3

this prints the lines where that line was contain at least one of Dansk or Norsk or Svenska in it, otherwise print the line which all what this commnad do can be written awk 1 infile or cat infile.

In x ||y ||z || ... means "if x OR y OR z OR ... at least one were True" then do something while in x && y && z &&... means "if x AND y AND z AND ... all of them were True" then do something; "True" means a result of an expression(s) or a condition(s) evaluated true result.

You must log in to answer this question.

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