Search This Blog

Tuesday, January 14, 2014

perl one liner to print quotes around each line

Consider the sample input file
rajulocal@hogwarts:~/x$ cat input.txt 
1.1,k
2.2,ka
3.3,kam
4.4,kama
5.5,kamar
6.6,kamara
7.7,kamaraj
8.8,kamaraju
To put quotes around each line, use the following Perl one liner
rajulocal@hogwarts:~/x$ perl -lane '$sq="\047"; print "$sq$_$sq";' input.txt 
'1.1,k'
'2.2,ka'
'3.3,kam'
'4.4,kama'
'5.5,kamar'
'6.6,kamara'
'7.7,kamaraj'
'8.8,kamaraju'
Here we are using octal code of single quote. To put quotes around just one column, use
rajulocal@hogwarts:~/x$ perl -F',' -lane '$sq="\047"; print "$F[0],$sq$F[1]$sq";' input.txt 
1.1,'k'
2.2,'ka'
3.3,'kam'
4.4,'kama'
5.5,'kamar'
6.6,'kamara'
7.7,'kamaraj'
8.8,'kamaraju'

Related posts: awk remove quotes in a column

No comments:

Followers