Code Geas Coder
Code Geas Coder

Reputation: 1979

awk to print some parameters of a line

I have lines in a file in linux, and i am trying print the line without the | and without some parameters

$cat file 
2013-07-15,Provider 1.99,3|30000055|2347|0,12222,1,3,0,0,0,19,aaa,bbb
2013-07-15,Provider 1.99,3|30000055|2347|0,12222,44,12,0,0,0,33,aaa,bbb

and i need the output like:

2013-07-15,Provider,2347,12222,1,3,0,0,0,19,aaa,bbb
2013-07-15,Provider,2347,12222,44,12,0,0,0,33,aaa,bbb

and i am trying with awk, but i have some problems.

Upvotes: 1

Views: 80

Answers (3)

glenn jackman
glenn jackman

Reputation: 246774

Use awk, and let blank or comma or pipe be the field separators:

awk -F '[[:blank:],|]' -v OFS=, '{
    print $1,$2,$6,$8,$9,$10,$11,$12,$13,$14,$15,$16
}' file
2013-07-15,Provider,2347,12222,1,3,0,0,0,19,aaa,bbb
2013-07-15,Provider,2347,12222,44,12,0,0,0,33,aaa,bbb

Upvotes: 1

perreal
perreal

Reputation: 97938

Using :

sed 's/ [^|]*|[^|]*|\([^|]*\)|[^,]/,\1/' input

and some shorter version:

sed 's/ .*|\([^|]*\)|[^,]*/,\1/' input

and even shorter:

sed 's/ .*|\(.*\)|[^,]*/,\1/' input

Upvotes: 1

jaypal singh
jaypal singh

Reputation: 77095

If your lines have similar pattern you would to retain then you can do:

awk 'BEGIN{FS=OFS=","}{$2="Provider";$3=2347}1' file

If you don't know what the patterns are then here is a more generic one:

awk 'BEGIN{FS=OFS=","}{split($2,a,/ /);split($3,b,/\|/);$2=a[1];$3=b[3]}1' file

If it doesn't solve your problem, I am pretty sure it would help you guide to get one.

Upvotes: 1

Related Questions