Issue with field separator in AWK script

Viewed 49

Having a very large file where two lines shown below and having two fields name and revision having colon delimiter. I need to print only the second column.

sam:7.[0:6]
Ram:8.[6:6]_rev[2:4] h_ack[2:6]
vincent:58

I tried this code:

#!/bin/bash
awk -F: '{print $2}'

7.[0
8.[6
58

Output should be:
7.[0:6]
8.[6:6]_rev[2:4] h_ack[2:6]
58

What went wrong in my code.

3 Answers

If you specify field separator as :, it's normal behavior of awk to output this, ex:

7.[0, because you need the other columns after $2.

cut here, better suits the requirement:

cut -d: -f2- file

The problem in your awk expression is that you are splitting on all :. Instead, you want to split only on the first : from the start.

$ awk -F'^[^:]+:' '{print $2}' file

The regex pattern matches the start of the string ^, any character other than a :, and finally a :.

Could you please try following.

awk '
match($0,/:.*/){
  print substr($0,RSTART+1,RLENGTH-1)
}
' Input_file
Related