Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting columns using awk [duplicate]

Tags:

awk

Hi I tried to split columns using awk command where in I have to use two separate characters for splitting the same column "comma and colon"

If My input file is like this

 0/1:121,313:4:99:123,0,104

I used : to split the column

echo "0/1:121,313:4:99:123,0,104" | awk '{split($0,a,":"); print a[2] a[3]}

I am getting this output

121,3134

However I only Need this output

121313

How to separate using both : and , ( Colon and Comma)

And I dont want to use awk -F command cause this is part of larger tab delimited text file which I am workin on.

like image 748
RonicK Avatar asked May 13 '18 07:05

RonicK


People also ask

How do I split a column in awk?

First awk , output the second field only. Second awk , choose [_/] as field separator, print the new Header and the fields. $1=$1 triggers reorganisation of fields, which is necessary as we change the output field separator to \t . You may add | column -t to make the columns in line.

How do I split a column in Linux?

You must specify either the -c option to cut by column or -f to cut by fields. (Fields are separated by tabs unless you specify a different field separator with -d. Use quotes (Section 27.12) if you want a space or other special character as the delimiter.)


1 Answers

awk -F '[,:]' '{ print $2 $3 }' file

By setting the field separator (by means of -F) to "either , or :", we may avoid doing an explicit split() on the data.

Or,

awk -F '[,:]' '{ print $2, $3 }' OFS='' file

which additionally uses an empty output field separator.

like image 130
Kusalananda Avatar answered Jan 01 '23 09:01

Kusalananda