Glam Prestige Journal

Bright entertainment trends with youth appeal.

i have log file like :

1::
10::
127.0.0.1
172.17.1.1

i want awk to split string to columns on :: delimiter. some thing like :

awk {print$1}

and the result :

1

and

awk {print$2}

and the result :

10

and

awk {print$3}

and the result :

127.0.0.1
172.17.1.1

i don't know how split with awk with delimiter ::.

5

1 Answer

You can set field-separator using -F option. Use the following commands in terminal,

$ awk -F "::" 'NR==1 {print $1}' logfile.txt
1
$ awk -F "::" 'NR==2 {print $1}' logfile.txt
10
$ awk -F "::" 'NR!=1 && NR!=2 {print $1}' logfile.txt
127.0.0.1
172.17.1.1

NR variable stores the line number. For example consider your file, with :: as field-separator

 $1 $2 $3
NR=1 1
NR=2 10
NR=3 127.0.0.1
NR=4 172.17.1.1
and so on.

If you use . as field-separator it will be like,

 $1 $2 $3 $4
NR=1 1::
NR=2 10::
NR=3 127 0 0 1
NR=4 172 17 1 1
0

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy