LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-23-2016, 12:46 AM   #16
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,790

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201

@Daniel, A literal ' in a 'string' is '\''
Code:
echo 'won'\''t'
The shell splits the string in two, and the \' in between will become a literal '

Last edited by MadeInGermany; 11-23-2016 at 12:53 AM.
 
1 members found this post helpful.
Old 11-23-2016, 06:56 AM   #17
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by MadeInGermany View Post
@Daniel, A literal ' in a 'string' is '\''
Code:
echo 'won'\''t'
The shell splits the string in two, and the \' in between will become a literal '
Thank you for solving the syntax mystery. To recap:

With this InFile ...
Code:
John likes chicken, prefers turkey, and won't eat ham.
Luke likes chicken McNuggets.
... this awk ...
Code:
awk 'BEGIN {RS="[[:space:]]"}
       {gsub(/^[[:punct:]]|[[:punct:]]$/,""); a[$0]++}
     END{print "chicken",   a["chicken"],
              "\nturkey",   a["turkey"],
              "\nham",      a["ham"],
              "\nwon'\''t", a["won'\''t"]}'  \
$InFile >$OutFile
... produced this OutFile ...
Code:
chicken 2 
turkey 1 
ham 1 
won't 1
Daniel B. Martin
 
1 members found this post helpful.
Old 11-23-2016, 03:02 PM   #18
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Just for fun I coded a non-awk solution. As always, corrections and improvements are invited.

With this InFile ...
Code:
John likes chicken, prefers turkey, and won't eat ham.
Luke likes chicken McNuggets.
Mary raises chickens and sheep.
... this code ...
Code:
egrep -wo 'chicken|turkey|ham|won'\''t' $InFile  \
|sort |uniq -c >$OutFile
... produced this OutFile ...
Code:
      2 chicken
      1 ham
      1 turkey
      1 won't
Daniel B. Martin

Last edited by danielbmartin; 11-23-2016 at 07:55 PM. Reason: Tightened the code
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
awk script for having number of beacon sent for gpsr ranjani Linux - Networking 1 04-03-2014 09:07 AM
how to print an apostrophe (') in a shell script using awk? skuz_ball Programming 11 03-10-2012 08:26 AM
[SOLVED] How to read an external file and print it with a definite format within AWK script tia_chofi Linux - Newbie 2 12-13-2011 04:26 AM
[SOLVED] Awk script to look for string, show value in field 2, if not present print zero Perseus Programming 12 10-06-2011 03:40 AM
sed / awk command to print line number as column? johnpaulodonnell Linux - Newbie 2 01-22-2007 07:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration