LinuxQuestions.org
Register a domain and help support LQ
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
 
LinkBack Search this Thread
Old 06-20-2006, 02:11 PM   #1
NiallC
Member
 
Registered: Feb 2003
Distribution: Suse 8.1
Posts: 102

Rep: Reputation: 15
Help with Text Parsing Script


Hello,

I have a text file that's in the following format:
------------------------------------------------
Company Name Date Type Num Due Date Amount Open Balance Balance
"Abernathy Drive, 4473"
James and Barbara -240.00 -240.00 0.00

"Total for Abernathy Drive, 4473"

"Abernathy Drive, 4481"
Jane 01/01/2003 Invoice 3297 01/01/2003\

------------------------------------------------

I only want the text between the " marks.

So my result could be:

"Abernathy Drive, 4473"

"Total for Abernathy Drive, 4473"

"Abernathy Drive, 4481"

or that without the " marks.

Any help with a script would be great. You guys have always been great in the past, and I thank you in advance for any assistance.

Thanks again,

NiallC
 
Old 06-20-2006, 02:40 PM   #2
kaz2100
Senior Member
 
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy jessie
Posts: 1,399

Rep: Reputation: 81
pipe your input into
Code:
grep \"*\"
 
Old 06-21-2006, 01:07 PM   #3
NiallC
Member
 
Registered: Feb 2003
Distribution: Suse 8.1
Posts: 102

Original Poster
Rep: Reputation: 15
I tried something similar to that, but it doesn't work, because the data is not split into separate lines, so I still get all the data returnded. Sorry, I should have been clearer

Thanks

NiallC
 
Old 06-21-2006, 01:36 PM   #4
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
I don't understand what isn't working with that command.
It gives me this output....
Code:
grep \"*\" file.txt

"Abernathy Drive, 4473"
"Total for Abernathy Drive, 4473"
"Abernathy Drive, 4481"
Here are a couple of similar commands:
Code:
grep -o '"[^"]*"' file.txt

grep \"*\" < file.txt | sed 's/^"\(.*\)"$/\1/'
 
Old 06-24-2006, 12:38 PM   #5
NiallC
Member
 
Registered: Feb 2003
Distribution: Suse 8.1
Posts: 102

Original Poster
Rep: Reputation: 15
Ok,like I mentioned the text is not split on separate lines like I originally posted, it looks more like this:

Company Name Date Type Num Due Date Amount Open Balance Balance
"Abernathy Drive, 4473" James and Barbara -240.00 -240.00 0.00 "Total for Abernathy Drive, 4473" "Abernathy Drive, 4481"

Grep searches and returns the line. which includes all the other data too, unfortunately.
 
Old 06-24-2006, 01:02 PM   #6
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
Code:
while(<>){
    while(m/\"(.+?)\"/ig) {print "$1\n";}
}
specify the datafile at the command line

edit> i guess i should mention that this is perl..

Last edited by xhi; 06-24-2006 at 01:03 PM.
 
Old 06-24-2006, 03:28 PM   #7
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,058

Rep: Reputation: 955Reputation: 955Reputation: 955Reputation: 955Reputation: 955Reputation: 955Reputation: 955Reputation: 955
Check out the awk command.

Basically, an awk program consists of one or more of these:
<pattern> { <what to do when this pattern is seen> }.

That sounds quite simple, and it is, but it is also remarkably powerful and there are many good examples provided.

The two main "interesting" features of this file are that the first line is treated separately and that the data includes quoted strings. Both of these can be handled quite directly in awk.

Awk is a very useful tool for jobs that are more complex than what grep can do. The "regular expression" capabilities of both are the same. Awk includes a fully-featured programming language based loosely upon "C."
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
I need help parsing text from a text file rsmccain Linux - General 2 01-05-2006 02:43 PM
Need help parsing text file scilec Programming 5 12-02-2004 01:00 PM
need help parsing text file airman99 Linux - General 2 10-08-2004 09:09 PM
Text parsing question bruoersolitario Linux - General 4 04-15-2004 02:12 PM
Parsing Text from a html file. Rezon Programming 6 10-18-2003 12:09 AM


All times are GMT -5. The time now is 03:18 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration