Monday, July 23, 2012

Print double quotes in unix Awk


I have already described in one of the earlier posts on how to print string within single quote in Awk print statement. Here is how we can print strings without "double quotes" in Awk.
$ cat file.txt 
6289693505455 Plan_DAIL_30D_AA
6289693505475 Plan_DAIL_30D_AA
6289693505462 Plan_DAIL_30D_AB
Output required:
Plan_DAIL_30D_AA "6289693505455"
Plan_DAIL_30D_AA "6289693505475"
Plan_DAIL_30D_AB "6289693505462"
i.e. Print the first field of the file within "double quotes".
Here are some of the alternatives:
$ awk '{print $2,$1}' file.txt 
Plan_DAIL_30D_AA 6289693505455
Plan_DAIL_30D_AA 6289693505475
Plan_DAIL_30D_AB 6289693505462

$ awk '{print $2,"\""$1"\""}' file.txt 
Plan_DAIL_30D_AA "6289693505455"
Plan_DAIL_30D_AA "6289693505475"
Plan_DAIL_30D_AB "6289693505462"

#Assigning the quotes sequence to a variable x 
$ awk -v x="\"" '{print $2,x$1x}' file.txt 
Plan_DAIL_30D_AA "6289693505455"
Plan_DAIL_30D_AA "6289693505475"
Plan_DAIL_30D_AB "6289693505462"

#Using octal code of double quotes
$ awk '{print $2,"\042"$1"\042"}' file.txt 
Plan_DAIL_30D_AA "6289693505455"
Plan_DAIL_30D_AA "6289693505475"
Plan_DAIL_30D_AB "6289693505462"

#Using ASCII code of double quotes
$ awk '{print $2,"\x22"$1"\x22"}'  file.txt 
Plan_DAIL_30D_AA "6289693505455"
Plan_DAIL_30D_AA "6289693505475"
Plan_DAIL_30D_AB "6289693505462"
Related posts: - Accessing external variable in Awk and Sed

1 comment:

Dooble Banna said...

very nice post for bash codes you can also visit http://cshonours.blogspot.in/
thank you

© Jadu Saikia www.UNIXCL.com