简体   繁体   中英

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.

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.

Using :

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

and some shorter version:

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

and even shorter:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM