简体   繁体   中英

AWK : Field Separator as Double Pipes or more

My data:

-1||"A|AA"||aaa@ymail
-10||"B ||B|ttB||b|| bb@ymail
-7||C||c

I want to exchange the double pipe delimiters || with a comma , like this:

awk -F'||' -v OFS="," '{$1=$1} 1' 2.txt

but the output remains the same. The reverse case (comma delimiter to double pipe), however, works:

awk -F"," -v OFS="||" '{$1=$1} 1' 1.txt

What am I doing wrong?

您需要转义管道符号:

awk -F '[|][|]' -v OFS="," '{$1=$1}1'

You need to escape metacharacters like | or use them in character class so that they are considered literals. They are considered logical OR notation without escapes. Try this:

$ awk -F'[|][|]' -v OFS="," '{$1=$1} 1' data
-1,"A|AA",aaa@ymail
-10,"B ,B|ttB,b, bb@ymail
-7,C,c

Different ways to do this :

awk -F '[|][|]' -v OFS="," '{$1=$1}1' # as suggested by accepted anwer
awk -F '[|]{2}' # my favorite as it is easier to extend this solution to more than two pipes
awk -F '\\|\\|' -v OFS="," '{$1=$1}1' # another way to escape
awk -F "\\\|\\\|" -v OFS="," '{$1=$1}1' # see https://superuser.com/a/1249834

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