简体   繁体   中英

Mawk - removing special characters

I use mawk.

I have a file:

{1: [u'Bank Pocztowy'], 2: [u'Pekao']}

I want to get the result:

{1: [uBank Pocztowy], 2: [uPekao]}

I tried this:

mawk '{gsub("\'",""); print}' file

Thank you for your help.

EDIT: Example:

$ mawk '{gsub("\'",""); print}' file
> 

Your gsub should be as below:

gsub(/\047/,"" )

chekc with

mawk '{gsub(/\047/,""); print}' file

I dont have mawk installed here. i only have nawk:

tested with nawk:

> echo "{1: [u'Bank Pocztowy'], 2: [u'Pekao']}" | nawk '{gsub(/\047/,"" ) ; print}'
{1: [uBank Pocztowy], 2: [uPekao]}

Alternatively ,If you want the solution in perl :

> echo "{1: [u'Bank Pocztowy'], 2: [u'Pekao']}" | perl -pe "s/\'//g"
{1: [uBank Pocztowy], 2: [uPekao]}

you can cut out the final print too, and make it go faster

 mawk/mawk2/gawk 'BEGIN { FS = "^$" } gsub(/[\047]+/, "") || 1'

Setting FS so not to spend time splitting fields. If single quotes exist, the gsub( ) return will handle the print automatically. The "or 1" is for the case when no single quotes exists that line.

Another method is use FS to detect the single quotes

 mawk/mawk2/gawk 'BEGIN { FS = "\047" } (NF == 1) || gsub(/[\047]+/, "")'

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