[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 289
  • Last Modified:

question regarding awk and regular exp?

I have a text file as follows;

1256-5656    josh; barry
dd-<55> test
4566-7777 james;john
ss-kkk test_2
4566
7777-9999 sean;penn
dd<66> test_3

I would like to write a awk script that matches the pattern four numbers a dash and four numbers (eg; 1256-5656) and pring the fields in that record. I tried doing the following but it does not work;

awk '$1~/^\d{4}-\d{4}/{print $1, $2, $3, $4}' filename.txt.

Also, how can I print the fields in the record below the matched pattern. For eg I would like to print;

dd-<55>  test
ss-kkk     test_2


Thanks for the help.

A
0
aej1973
Asked:
aej1973
  • 5
  • 4
1 Solution
 
ozoCommented:
awk '/^[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9]/'  filename.txt.
0
 
aej1973Author Commented:
Thanks. What about being able to print the next line?

A
0
 
ozoCommented:
awk '!--n
/^[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9]/{n=1;print}' filename.txt
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
aej1973Author Commented:
it is not working. The error I am getting is as follows;

awk '!--n'/^[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9]/'{n=1; print $1,"|", $2,"|",$11,"|", $13}' FULL.TXT
awk: cmd. line:1: !--n/^[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9]/{n=1; print $1,"|", $2,"|",$11,"|", $13}
awk: cmd. line:1:      ^ syntax error
awk: cmd. line:1: !--n/^[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9]/{n=1; print $1,"|", $2,"|",$11,"|", $13}
awk: cmd. line:1:           ^ syntax error
awk: cmd. line:1: !--n/^[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9]/{n=1; print $1,"|", $2,"|",$11,"|", $13}
awk: cmd. line:1:                ^ syntax error
awk: cmd. line:1: !--n/^[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9]/{n=1; print $1,"|", $2,"|",$11,"|", $13}
awk: cmd. line:1:                     ^ syntax error
awk: cmd. line:1: !--n/^[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9]/{n=1; print $1,"|", $2,"|",$11,"|", $13}
awk: cmd. line:1:                          ^ syntax error
awk: cmd. line:1: !--n/^[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9]/{n=1; print $1,"|", $2,"|",$11,"|", $13}
awk: cmd. line:1:                                ^ syntax error
awk: cmd. line:1: !--n/^[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9]/{n=1; print $1,"|", $2,"|",$11,"|", $13}
awk: cmd. line:1:                                     ^ syntax error
awk: cmd. line:1: !--n/^[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9]/{n=1; print $1,"|", $2,"|",$11,"|", $13}
awk: cmd. line:1:                                          ^ syntax error
awk: cmd. line:1: !--n/^[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9]/{n=1; print $1,"|", $2,"|",$11,"|", $13}
awk: cmd. line:1:                                            ^ unterminated regexp
0
 
ozoCommented:
awk '!--n
/^[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9]/{n=1; print $1,"|", $2,"|",$11,"|", $13}' FULL.TXT
0
 
aej1973Author Commented:
This is prining line 1 and 2, but I need only line 2 to be printed. Thanks for the help.

A
0
 
ozoCommented:
awk '!--n{ print $1,"|", $2,"|",$11,"|", $13}
/^[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9]/{n=1}'  FULL.TXT
0
 
aej1973Author Commented:
Thank you very much.
0
 
aej1973Author Commented:
If you do not mind can I ask you one more doubt? How can I have the output in such a way that the output has the first field in the first record and the 3rd and 4th fields of the 2 second record on one line in the output? for eg I would like my output to be

1256-5656  dd-<55> test
4566-7777 ss-kkk test_2

Thanks for the help.

A

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now