awk

How do strongly typed regexp constants work in GNU Awk?

心已入冬 提交于 2021-02-07 17:29:22
问题 Strongly typed regexp constants is a handy tool that GNU Awk has. It is documented in GNU Awk User's Guide -> 6.1.2.2 Strongly Typed Regexp Constants and in there you can find interesting examples. From reading it, and comments to an answer I made up some examples that show those: $ cat file he;llo ho are you; $ gawk -v patt='@/;/' '$0 ~ patt' file # it prints those lines containing ";" he;llo you; In this case, we pass the pattern ";" with @/;/ and so it prints all the lines that contain ";"

How do strongly typed regexp constants work in GNU Awk?

一曲冷凌霜 提交于 2021-02-07 17:28:18
问题 Strongly typed regexp constants is a handy tool that GNU Awk has. It is documented in GNU Awk User's Guide -> 6.1.2.2 Strongly Typed Regexp Constants and in there you can find interesting examples. From reading it, and comments to an answer I made up some examples that show those: $ cat file he;llo ho are you; $ gawk -v patt='@/;/' '$0 ~ patt' file # it prints those lines containing ";" he;llo you; In this case, we pass the pattern ";" with @/;/ and so it prints all the lines that contain ";"

How can I get “grep -zoP” to display every match separately?

拟墨画扇 提交于 2021-02-07 14:36:36
问题 I have a file on this form: X/this is the first match/blabla X-this is the second match- and here we have some fluff. And I want to extract everything that appears after "X" and between the same markers. So if I have "X+match+", I want to get "match", because it appears after "X" and between the marker "+". So for the given sample file I would like to have this output: this is the first match and then this is the second match I managed to get all the content between X followed by a marker by

How can I get “grep -zoP” to display every match separately?

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-07 14:35:20
问题 I have a file on this form: X/this is the first match/blabla X-this is the second match- and here we have some fluff. And I want to extract everything that appears after "X" and between the same markers. So if I have "X+match+", I want to get "match", because it appears after "X" and between the marker "+". So for the given sample file I would like to have this output: this is the first match and then this is the second match I managed to get all the content between X followed by a marker by

How can I get “grep -zoP” to display every match separately?

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-07 14:35:05
问题 I have a file on this form: X/this is the first match/blabla X-this is the second match- and here we have some fluff. And I want to extract everything that appears after "X" and between the same markers. So if I have "X+match+", I want to get "match", because it appears after "X" and between the marker "+". So for the given sample file I would like to have this output: this is the first match and then this is the second match I managed to get all the content between X followed by a marker by

Is it possible to handle fields containing line breaks in awk?

岁酱吖の 提交于 2021-02-07 10:39:35
问题 Suppose I have a text file with records of the following form, where the FS is generally speaking a comma, and the RS is generally speaking a newline. However, the exception to this rule is that if a field is in quotes, it should treat the line breaks and commas as part of the field. "This field contains line breaks and is quoted but it should be treated as a single field",1,2,3,"another field" How can I use awk to parse such a file correctly, where I can still access $1,$2... , as I usually

Is it possible to handle fields containing line breaks in awk?

流过昼夜 提交于 2021-02-07 10:39:21
问题 Suppose I have a text file with records of the following form, where the FS is generally speaking a comma, and the RS is generally speaking a newline. However, the exception to this rule is that if a field is in quotes, it should treat the line breaks and commas as part of the field. "This field contains line breaks and is quoted but it should be treated as a single field",1,2,3,"another field" How can I use awk to parse such a file correctly, where I can still access $1,$2... , as I usually

Print all lines between two patterns, exclusive, first instance only (in sed, AWK or Perl) [duplicate]

白昼怎懂夜的黑 提交于 2021-02-07 09:33:34
问题 This question already has answers here : How to print lines between two patterns, inclusive or exclusive (in sed, AWK or Perl)? (9 answers) Closed 1 year ago . Using sed, AWK (or Perl), how do you print all lines between (the first instance of) two patterns, exclusive of the patterns? 1 That is, given as input: aaa PATTERN1 bbb ccc ddd PATTERN2 eee Or possibly even: aaa PATTERN1 bbb ccc ddd PATTERN2 eee fff PATTERN1 ggg hhh iii PATTERN2 jjj I would expect, in both cases: bbb ccc ddd 1 A

Remove all lines between two strings

馋奶兔 提交于 2021-02-07 09:02:32
问题 In a sh shell script. Given data in a text file: string1 string2 gibberish gibberish string3 gibberish string4 How could you use awk or sed to remove all lines between string2 (inclusive) and string3 (not including string3 )? to end up with: string1 string3 string4 回答1: you can try this. Anything before "string2" will not be deleted. awk 'BEGIN{f=0} { match($0,"string2") if(RSTART){ print substr($0,1,RSTART-1) f=1 next } match($0,"string3") if(RSTART){ $0=substr($0,RSTART) f=0 } } f==0{print}

Remove all lines between two strings

我只是一个虾纸丫 提交于 2021-02-07 09:01:06
问题 In a sh shell script. Given data in a text file: string1 string2 gibberish gibberish string3 gibberish string4 How could you use awk or sed to remove all lines between string2 (inclusive) and string3 (not including string3 )? to end up with: string1 string3 string4 回答1: you can try this. Anything before "string2" will not be deleted. awk 'BEGIN{f=0} { match($0,"string2") if(RSTART){ print substr($0,1,RSTART-1) f=1 next } match($0,"string3") if(RSTART){ $0=substr($0,RSTART) f=0 } } f==0{print}