- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Double-Quotes In awk
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-27-2009 10:46 AM
тАО01-27-2009 10:46 AM
"124","124",,60.00,60.00,60.00,0.00,0.45,60.45,0.059000,"APP","00","EXC"
I need to awk or sed this stuff so its output looks like
"124","124","","60.00","60.00","60.00","0.00","0.45","60.45","0.059000","APP","00","EXC"
In other words, if a "field" is already surrounded by double-quotes, pass it thru. If the field is *NOT* surrounded by double-quotes, I need to output them doing so. And, if a field between commas is empty, I need to parse out a placeholder set of quotes.
I know my awk command would be something of the form
awk -F, -f [progfile]
and that [progfile] will of the form
{
for(i=1;i<=NF;i++)
{
if(there's a quote already)
{printf("%s,",$i)}
else
{printf("\"%s\",",$i)}
}
printf("\n")
}
What's got me stumped is how to detect that field N already begins and ends with a double-quote. Pretty much anything I've tried
if(match($i,/\"/)!=0)
if($i~/^'"'/)
if($i~/^'"'/)
has caused awk to barf on another statement, presumably because I have unbalanced quotes where I'm not escaping something correctly (perhaps in that last mondo printf statement?).
Unfortunately, perl is not an option for me, and I'm not a high-enough muckety-muck to insist that it be installed. Therefore, I'm thinking I'm going to have to stick with awk, unless someone has some sed or other text-processing capital-m Magic they can share.
Help?
HP-Server-Literate since 1979
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-27-2009 10:52 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-27-2009 11:00 AM
тАО01-27-2009 11:00 AM
Re: Double-Quotes In awk
Try:
# awk '{gsub("\,\,", ",\"\",");print}' file
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-27-2009 11:03 AM
тАО01-27-2009 11:03 AM
Re: Double-Quotes In awk
D'oh. Thank you, Dennis. As you may or may not recall, I have quite a knack for figuring out the Most Complicated Way Possible to accomplish tasks. Cutting thru the muck and just testing substr($i,1,1) and substr($i,1,length($i)) for quotes Did The Trick.
HP-Server-Literate since 1979
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-27-2009 11:05 AM
тАО01-27-2009 11:05 AM
Re: Double-Quotes In awk
If not, just strip all double-quotes, replace commas with "," and start and end with a doublequote.
Are you sure perl is not simple on the box?
Anyway...
$ cat tmp.txt
"124","124",,60.00,60.00,60.00,0.00,0.45,60.45,0.059000,"APP","00","EXC"
$ awk '{gsub(/"/,""); gsub(/,/,"\",\""); print "\"" $0 "\""}' tmp.txt
"124","124","","60.00","60.00","60.00","0.00","0.45","60.45","0.059000","APP","00","EXC"
hth,
Hein.
$ perl -pe 's/"//g;s/,/","/g;$_; s/^/"/; s/.$/"/' tmp.txt
"124","124","","60.00","60.00","60.00","0.00","0.45","60.45","0.059000","APP","00","EXC"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-27-2009 11:23 AM
тАО01-27-2009 11:23 AM
Re: Double-Quotes In awk
Thankee! That *almost* got me there, but missed putting quotes around the non-quoted numeric fields. :-/ However, Hein's suggestion worked like a charm. :-)
(And I know the "no Perl" stuff had to have driven you nutty. It's not got me all that happy, myself, but I've got to work within the handcuffs I've been handed...)
HP-Server-Literate since 1979
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-27-2009 11:26 AM
тАО01-27-2009 11:26 AM
Re: Double-Quotes In awk
Double-quotes, no. Commas, it doesn't *appear* that commas are An Issue, but all I've been handed thus far is "toy data".
Hein:> Are you sure perl is not simple on the box?
Unfortunately, yes, I'm sure. :-( :-( :-(
HP-Server-Literate since 1979
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-27-2009 11:31 AM
тАО01-27-2009 11:31 AM
Re: Double-Quotes In awk
HP-Server-Literate since 1979