I want to separate each new line with awk and extract a record with a specific string in grep.

Asked 2 years ago, Updated 2 years ago, 415 views

How do I extract a record from address.txt that is separated by line breaks to four records and contains the string Pass?

If you enter the following command, you will see all output.

cat address.txt |awk'BEGIN {RS="";FS="\n"}$2=="Pass"'

address.txt

Yuichi Higashikawa
passing
080-1111-1111
〒111-1111
1-1-1 A-cho, XX-ku, XX-ken
ABC Building 1001


Yuji Nishimura
failure
080-2222-2222
〒222-2222
2-22 B-cho, XX prefecture, XX prefecture

Yuzo Namsan Mountain
passing
080-3333-3333
〒333-3333
3-3-3 C-cho, XX prefecture, XX prefecture
XYZ Heights Room 3


Yuzo Kitaoka
failure
080-4444-4444
〒444-4444
4-4-4 D-cho, XX-ku, XX-ken

linux shell awk grep

2022-09-30 21:58

1 Answers

RS (Record Separator) should be two new lines (empty lines) and FS (Field Separator) should be new lines. You can specify ORS as you like.

awk-F'\n'-vRS='\n\n'-vORS='\n'$2=="Pass"'address.txt

Run Results

Yuichi Higashikawa
passing
080-1111-1111
〒111-1111
1-1-1 A-cho, XX-ku, XX-ken
ABC Building 1001

Yuzo Namsan Mountain
passing
080-3333-3333
〒333-3333
3-3-3 C-cho, XX prefecture, XX prefecture
XYZ Heights Room 3


2022-09-30 21:58

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.