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
aej1973Asked:
Who is Participating?
 
ozoConnect With a Mentor Commented:
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
 
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
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
ozoCommented:
awk '!--n
/^[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9]/{n=1;print}' filename.txt
0
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.