LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 02-04-2010, 09:28 PM   #1
yech
LQ Newbie
 
Registered: Dec 2007
Posts: 22

Rep: Reputation: 0
awk parse string with space?


How to let awk consider a string by double quota as one field?

for example:

echo "first \"second is a string\"" | awk '{ print $2 }'

I want to print out "second is a string".
 
Old 02-05-2010, 12:09 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 17,873

Rep: Reputation: 2600Reputation: 2600Reputation: 2600Reputation: 2600Reputation: 2600Reputation: 2600Reputation: 2600Reputation: 2600Reputation: 2600Reputation: 2600Reputation: 2600
echo "first \"second is a string\""| awk '{ print $2,$3,$4,$5 }'
 
Old 02-05-2010, 12:28 AM   #3
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Rep: Reputation: 32
Quote:
Originally Posted by chrism01 View Post
echo "first \"second is a string\""| awk '{ print $2,$3,$4,$5 }'
Here you are assuming that the next " present at $5. Hence you are printing from $2 till $5.
What if I don't know where the second " present???
I used perl to accomplish your task and here is the script for that.
Code:
#usr/bin/perl
#temp.pl
use strict;
use warnings;
my @arr=qw/first "second is a string" last/;
my $a=0;
foreach(@arr)
{
	if(m/^(\")/)
	{
		$a=1;
		print $_;
		next;
	}
	if(($a == 1) && (m/!(\")$/))
	{
		print " $_";
	}
	if(m/(\")$/)
	{
		print " $_\n";
		exit;
	}
}
print"\n";
Code:
[Ashok@station130 ~]$ perl temp.pl
"second string"
Hope that helps....
 
Old 02-05-2010, 01:28 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by yech View Post
How to let awk consider a string by double quota as one field?

for example:

echo "first \"second is a string\"" | awk '{ print $2 }'

I want to print out "second is a string".
just make your delimiter double quote

Code:
# echo 'first "second is a string"' | awk -F'"' '{print $2}'
second is a string
 
Old 02-10-2010, 10:03 AM   #5
yech
LQ Newbie
 
Registered: Dec 2007
Posts: 22

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by ghostdog74 View Post
just make your delimiter double quote

Code:
# echo 'first "second is a string"' | awk -F'"' '{print $2}'
second is a string
Thanks for reply. But this happens to give the correct answer, what if:

echo 'first second "third is a string"' | awk -F '"' '{print $3}'
 
Old 02-10-2010, 05:24 PM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982
Code:
$ cat testfile
first "second is a string"
first second "third is a string"
first "second is a string" third "fourth is a string, too" fifth
$
$ cat extract_quoted_strings.awk
{
  while (match($0,/"[^"]*"/)){
    print substr($0, RSTART, RLENGTH)
    sub(/"[^"]*"/,"")
  }
}
$
$ awk -f extract_quoted_strings.awk testfile
"second is a string"
"third is a string"
"second is a string"
"fourth is a string, too"
 
Old 02-10-2010, 07:48 PM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by yech View Post
Thanks for reply. But this happens to give the correct answer, what if:

echo 'first second "third is a string"' | awk -F '"' '{print $3}'
why do you want to change to $3? use $2. It also works for multiple text between quotes. Just use a step index of 2.

Code:
# echo 'first second "third is a string" fourth "fifth is a string"' | awk -F '"' '{for(i=2;i<=NF;i+=2){print $i}}'
third is a string
fifth is a string
 
Old 02-11-2010, 05:03 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982
Nice code, ghostdog! The only downside is when the input line contains single (not-paired) quotes, but it should not be a requirement.
 
Old 02-12-2010, 01:33 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,821

Rep: Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084
Not sure if your considering alternatives, but here is m2c:

echo "first \"second is a string\"" | sed -n 's/.*\(".*"\).*/\1/p'
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
how to parse the xml string Dyuri Linux - Software 2 09-03-2008 08:49 AM
How to use awk command to parse fields in a line johnsanty Programming 9 05-25-2006 09:56 PM
Parse a perl string djgerbavore Programming 3 10-31-2004 07:23 AM
I need to parse a word: awk or sed? mehesque Programming 5 07-27-2004 04:23 PM
Parse a filename with awk and sed chrisk5527 Programming 2 06-08-2004 06:13 PM

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

All times are GMT -5. The time now is 04:13 AM.

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