The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Extracting records with unique fields from a fixed width txt file sitney Shell Programming and Scripting 8 02-10-2008 03:18 AM
combining fields in two text fields shocker Shell Programming and Scripting 3 01-16-2008 11:27 AM
extracting fields prvnrk Shell Programming and Scripting 2 10-08-2007 03:39 AM
Extracting information from a template Ernst Shell Programming and Scripting 4 03-07-2007 01:18 AM
Extracting fields from an output 8-) csaha Shell Programming and Scripting 6 01-20-2006 08:37 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-03-2007
spindoctor spindoctor is offline
Registered User
  
 

Join Date: May 2007
Posts: 31
Extracting information from text fields.

Dear friends,
I'm a novice Unix user and I'm trying to learn the ropes. I have a big task I have to accomplish and I'm convinced Unix can get the job done, I just haven't figured out how. I recently posted on the topic of cutting text between unique text patterns and somebody helped me a great deal. It worked great.
There are other tasks, however, that I want to accomplish.
I'm doing a content analysis of newspaper articles that I've exported in .txt format from a ProQuest database. The .rtf files look like this when I cat them in Unix.

Quote:
\
Adoptive parents often face tactless questions: Curiosity lies behind rude\
inquiries:[Final Edition]\
Karen Miles. Edmonton Journal. Edmonton, Alta.:Mar 15, 2000. p. F7 \
\
Author(s): Karen Miles\
\
Document types: News\
\
Section: Living\
\
Publication title: Edmonton Journal. Edmonton, Alta.: Mar 15, 2000. pg. F.7\
\
Source type: Newspaper\
\
ProQuest document 220675241\
ID:\
\
Text Word Count 314\
\
Document URL: {\field{\*\fldinst{HYPERLINK "http://proquest.umi.com"}}{\fldrslt http://proquest.umi.com}}/\
pqdweb?did=220675241&Fmt=3&clientId=14119&RQT=309&VName=PQD\
\
Abstract (Document Summary)\
\
"Is she adopted?" the school registrar whispered to Kathryn Creedy, of\
Alexandria, Va., when Creedy signed Alexis, her then five- year-old Romanian\
daughter, up for school. Since Alexis could clearly hear the question, Creedy\
curtly replied: "Yes, my daughter is adopted -- and she knows it!"\
\
"The media loves to sensationalize stories." Then point out that those stories\
are rare, and that there are many successful people who were adopted -\
- including playwright Edward Albee, Olympic skater Scott Hamilton, actor\
Melissa Gilbert and Apple Computer co- founder Steven Jobs.\
\
\
\
Full Text (314 words)\
\
Copyright Southam Publications Inc. Mar 15, 2000\
\
"Is she adopted?" the school registrar whispered to Kathryn Creedy, of\
Alexandria, Va., when Creedy signed Alexis, her then five- year-old Romanian\
daughter, up for school. Since Alexis could clearly hear the question, Creedy\
curtly replied: "Yes, my daughter is adopted -- and she knows it!"\
\
But Creedy resented the probe, especially when she later learned that the\
registrar hadn't needed the information. "The woman violated our privacy just\
to satisfy her own curiosity," she says.\
The information contained in the 'fields' such as author, date, document type, and text word count would be immensely valuable to me. I would like to be able to extract the information (preferably only the text after the field titles (author, date, text word count) although this is not necessary. I imagine find and replace functions in Excel could be used to delete that easily. I would like to be able to get this information to some kind of excel database, probably via a .csv file.
Ultimately, I will have hundreds of these news stories to extract the information from.

Does anybody have any suggestions?

Simon
  #2 (permalink)  
Old 06-03-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,509
just a snippet of it:
Code:
awk 'BEGIN{FS=":"}
    { gsub(/\\/,"")}
    /Author/{print "Author: "$2 }
    /Document types/{print "Document: "$2 }
    /Text Word Count/{sub(/Text Word Count/,"");print "Word count: " $0}
' "file"
output:
Code:
# ./test1.sh
Author:  Karen Miles
Document:  News
Word count:  314
  #3 (permalink)  
Old 06-05-2007
spindoctor spindoctor is offline
Registered User
  
 

Join Date: May 2007
Posts: 31
That's really helpful. For some reason, however, the gsub command doesn't delete the backslashes in the files. When I play with it, the command doesn't return an error; it seems to complete, but the files still contain the slashes.

Also, the field variables don't write exactly right. The /Author/{print "Author: "$2 }string only prints the first, not both names of the author. I had to modify it to read "$2, $3" to get first and last names. Any clue why that might be?

Thanks again though, this is what I was looking for!
Simon
  #4 (permalink)  
Old 06-05-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,509
Quote:
Originally Posted by spindoctor
That's really helpful. For some reason, however, the gsub command doesn't delete the backslashes in the files.
gsub() modifies what you passed to it "in place". eg gsub("","",$1);print $1


Quote:
Also, the field variables don't write exactly right. The /Author/{print "Author: "$2 }string only prints the first, not both names of the author. I had to modify it to read "$2, $3" to get first and last names. Any clue why that might be?
it should be no problem if the field separator is specified as ":" . show your code if you want.
  #5 (permalink)  
Old 06-06-2007
spindoctor spindoctor is offline
Registered User
  
 

Join Date: May 2007
Posts: 31
Hi, On the first point, this is the code I was playing with from the command line, based on your suggestion.
awk 'BEGIN { gsub(/\\/,"")}' 03152000.rtf

Did you put the gsub command in there just to get rid of the backslashes? Or is it related to the field extraction process?

Regarding the second point:
Here is the text, copied straight from the .txt file I'm trying to extract information from.

Author(s): Ashley Geddes, Provincial Affairs Writer

Here is the code (entered in the command line) I got from you and it's output.

awk 'Begin{FS=":"} ; /Author/{print $2 }' 03152000.rtf > Author.txt

Ashley

Here is the modified way I wrote the code and its result

awk 'Begin{FS=":"} ; /Author/{print $2,$3 }' 03152000.rtf > Author.txt

Ashley Geddes,

the same problem occurred with the field "Document Types" I had to change around the fields as you wrote them to get the result.

Obviously I'm not that concerned, because it seems to be working. However I am very curious because I'd like to know how the thing works. Thanks for your help!
  #6 (permalink)  
Old 06-06-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,509
use BEGIN not Begin. and yes ,the gsub is there to remove slash
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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




All times are GMT -4. The time now is 04:47 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0