Question about awk format


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Question about awk format
# 1  
Old 06-17-2013
Question Question about awk format

Hi, Guys,
There is one question about AWK format. Here is the code:
Code:
gawk -F: '/^Dan/ {print "Dan's phone number is ",$2}' lab3.data

An syntax error will come out because the quote mark between Dan and s and first quote mark are recognized as a quote pair.
I want to get the input like this:
Quote:
Dan's phone number is (904) 888-7777
I know if I put the awk program instruction into a file, it will work well. However, is there anything else to solve it in the command line?
Thanks.
# 2  
Old 06-17-2013
It's not an awk problem, it's a shell problem. Single quotes inside single quotes are taken to end a single quote block and there's no way to escape them inside single quotes (since you can't escape anything in single quotes, at all, ever.)

You could put the string in a variable:

Code:
gawk -F: '/^Dan/ {print STR, $2}' STR="Dan's phone number is" lab3.data

Or just put everything in double quotes and escape everything that needs escaping, but that's a bit more elaborate than needed here.
# 3  
Old 06-17-2013
Or you could use the octal code for single quote:
Code:
gawk -F: '/^Dan/ {print "Dan\047s phone number is ",$2}' lab3.data

# 4  
Old 06-17-2013
Quote:
Originally Posted by Yoda
Or you could use the octal code for single quote:
Code:
gawk -F: '/^Dan/ {print "Dan\047s phone number is ",$2}' lab3.data

Cool! Thanks!Smilie

---------- Post updated at 03:03 PM ---------- Previous update was at 03:01 PM ----------

Quote:
Originally Posted by Corona688
It's not an awk problem, it's a shell problem. Single quotes inside single quotes are taken to end a single quote block and there's no way to escape them inside single quotes (since you can't escape anything in single quotes, at all, ever.)

You could put the string in a variable:

Code:
gawk -F: '/^Dan/ {print STR, $2}' STR="Dan's phone number is" lab3.data

Or just put everything in double quotes and escape everything that needs escaping, but that's a bit more elaborate than needed here.
Smart!Smilie
# 5  
Old 06-17-2013
The following is more general (not assuming ASCII codes):
Code:
gawk -F: '/^Dan/ {print "Dan'\''s phone number is ",$2}' lab3.data

The shell sees 'string1'\''string2', so the shell does evaluation of \' outside a 'string'.
The awk sees the evaluated '
This User Gave Thanks to MadeInGermany For This Post:
# 6  
Old 06-17-2013
And the inevitable:
Code:
gawk -F: "/^Dan/ {print \"Dan's phone number is\",\$2}"  file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Format Question

Hello friends i have a file that outputs like this: lineA lineB lineC I would like it to format it like this as output: 'lineA','lineB','lineC' Can you help? Thank you (2 Replies)
Discussion started by: DallasT
2 Replies

2. Shell Programming and Scripting

awk Format

Guys ,, pls .. need help with this ... File 1 0A6C Not Visible ???:? NA:NA TDEV N/Grp'd NR 5121 0A6D Not Visible ***:* NA:NA TDEV N/Grp'd RW 33784 0A6E Not Visible ***:* NA:NA TDEV N/Grp'd RW 33784... (4 Replies)
Discussion started by: greycells
4 Replies

3. Shell Programming and Scripting

scripting/awk help : awk sum output is not comming in regular format. Pls advise.

Hi Experts, I am adding a column of numbers with awk , however not getting correct output: # awk '{sum+=$1} END {print sum}' datafile 2.15291e+06 How can I getthe output like : 2152910 Thank you.. # awk '{sum+=$1} END {print sum}' datafile 2.15079e+06 (3 Replies)
Discussion started by: rveri
3 Replies

4. Solaris

SFTP file format question

Hi, We here at State of Iowa are trying to SFTP an ascii text file off of an IBM server over to our DOL SUN Server using Solaris 5.8. After doing the secure handshake login all I am doing at present is "sftp> get testfile". Somehow in pulling the file in from the IBM server to the SUN server it... (2 Replies)
Discussion started by: wsiefkas
2 Replies

5. Solaris

Disk Format Question

Wondering if anyone could tell about how long it will take to perform 3 passes on a 72 GB disk? I have a Sun V240 and will have to format all 4. Thanks. (1 Reply)
Discussion started by: buckhtr77
1 Replies

6. Shell Programming and Scripting

awk/nawk question to format a file

Hi, I am new to awk/nawk, needs help. I want to merge the rows having emplid attribute same into a single row in the following file. In actual this kind of file will have around 50k rows. Here is my input file id|emplid|firstname|dep|lastname 1|001234|test|1001|1 2|002345|test|1032|2... (7 Replies)
Discussion started by: kumar04
7 Replies

7. Shell Programming and Scripting

AWK CSV to TXT format, TXT file not in a correct column format

HI guys, I have created a script to read 1 column in a csv file and then place it in text file. However, when i checked out the text file, it is not in a column format... Example: CSV file contains name,age aa,11 bb,22 cc,33 After using awk to get first column TXT file... (1 Reply)
Discussion started by: mdap
1 Replies

8. Shell Programming and Scripting

Date format question

I have a string that looks like this: 2008 04 09 18 45 30 0 I would like to convert it to a date format like this: Wed Apr 09 18:45:30.000 GMT 2008 I have been searching all over and can't find anything to help me. I am using ksh on a sun solaris unix machine. Thank you. Allyson (2 Replies)
Discussion started by: ajgwin
2 Replies

9. UNIX for Dummies Questions & Answers

Format question

I'm trying to format a hard drive. It asks me for the disk type, which is not there for my drive. So I choose other and it says "Enter number of data cylinders". Where do I get this information from? (5 Replies)
Discussion started by: shorty
5 Replies

10. UNIX for Dummies Questions & Answers

format and pkginfo -l question

I have a Solaris 2.6 box flagging an "Error block: 308918" in the messages file when I execute a pkginfo -l. pkginfo with no flags returns no errors. I think I need to use format to mark this block and then restore the database used by pkginfo for backup. Any advice/input welcome. Thanks (4 Replies)
Discussion started by: 98_1LE
4 Replies
Login or Register to Ask a Question