Home > bash > Awk removing single quote(‘) from text

Awk removing single quote(‘) from text

Problem

I needed to the data in file1 to look like the data in file2

shell> cat file1

"number","Last Name, Other Names,data1","date(yyyy-mm-dd) time(hh:mm:ss)"
'6237417889','M\'MUTAKI, ISAIAH MURRAY,BATS IN A CAVE','2010-05-01 00:00:24'
'62377871709','SANWANA, SAMUEL AJAIPKIS,SAN HSE','2010-05-17 00:00:26'
'6237417889','CLAUS, FABIAN KIM,RAMS PIE','2010-05-21 05:00:24

shell> cat file2

'6237417889'|ISAIAH MURRAY M\'MUTAKI|BATS IN A CAVE|'2010-05-01 00:00:24'
'62377871709'|SAMUEL AJAI PKIS SANWANA|SAN HSE|'2010-05-17 00:00:26'
'6237417889'|FABIAN KIM CLAUS|RAMS PIE|'2010-05-21 05:00:24'

So I needed to rearrange the fields. With was pretty simple with awk.

shell> awk -v FS=”,” -v OFS=”|” ‘{print $1,$3 $2, $4,$5}’ file1

'6237417889'| ISAIAH MURRAY 'M\'MUTAKI|BATS IN A CAVE'|'2010-05-01 00:00:24'
'62377871709'| SAMUEL AJAIPKIS 'SANWANA|SAN HSE'|'2010-05-17 00:00:26'
'6237417889'| FABIAN KIM 'CLAUS|RAMS PIE'|'2010-05-21 05:00:24'

BUT: The extra ‘ just before the Last name was not helping. After struggling and googling I figured it out. I knew sub() would help but how to write it so that it works on the shell. I got my share of errors. Here are a few

^ invalid char ''' in expression
^ backslash not last character on line
unexpected EOF while looking for matching `"'
syntax error near unexpected token `)'

Anyway, I finally got it to work.

shell> awk -v FS=”,” -v OFS=”|” “{sub(/’/,\” \”,\$2); print \$1,\$3 \$2,\$4,\$5}” file1

'6237417889'|ISAIAH MURRAY M\'MUTAKI|BATS IN A CAVE|'2010-05-01 00:00:24'
'62377871709'|SAMUEL AJAI PKIS SANWANA|SAN HSE|'2010-05-17 00:00:26'
'6237417889'|FABIAN KIM CLAUS|RAMS PIE|'2010-05-21 05:00:24'
Advertisements
Categories: bash Tags:
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s