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 > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
PERL: Searching for a string in a text file problem meevagh Shell Programming and Scripting 15 05-01-2008 10:57 AM
Searching for text in a Space delimited File andyblaylock UNIX for Dummies Questions & Answers 6 11-27-2007 07:33 PM
Searching directory for file that contains some text. LordJezo UNIX for Dummies Questions & Answers 4 04-04-2006 08:58 PM
searching each file in a directory for text jim majors Shell Programming and Scripting 2 03-23-2006 08:29 PM
Searching for text in files GandalfWhite Linux 2 01-21-2004 01:26 PM

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 01-01-2004
thekid2 thekid2 is offline
Registered User
  
 

Join Date: Dec 2003
Location: CT, USA
Posts: 4
Question Help with searching a text file

Hello all!

I've been working for days on this and it is really bugging me!!

Here's my dilemma:

Say I have a very large text file which contains fields delimited my a ':' which logs various records. Each record is separated by a newline character, therefore I can search for lines with 'grep' to find the results i want. The problem is, there may be as many as 30 or so fields with associated values per entry...not an issue...but, if a certain field is not used for a particular record, that field is not included in the file entry (not even a '::' for an empty field...i didn't write the software).

So the problem arrises that yes i can search for lines containing whatever i want, but sometimes I don't want to view 30 or so fields, sometimes i am only interested in one or two fields...now some of you may say use something like '# cut -d: -f22,24' or '# awk -F: {print $1, $2}' but, since not every field is used for ever record, certain data does not always appear in the same field!!!! I can guess which field until i get the correct one, but this is often waaaaayyy too time consuming. Is there a way to somehow use a string or expression to delimit a field (as opposed to a character which cut or awk only seem to do)?? Or is there a way to do this with awk?? Thank you so much...

Sorry if it was really long winded...i'm flustered!!!
  #2 (permalink)  
Old 01-01-2004
google's Avatar
google google is offline Forum Advisor  
Moderator
  
 

Join Date: Jul 2002
Location: Atlanta
Posts: 740
Please post your operating system, shell, and some sample data. I bet several of the regulars here could provide a quick solution to your problem.
  #3 (permalink)  
Old 01-01-2004
thekid2 thekid2 is offline
Registered User
  
 

Join Date: Dec 2003
Location: CT, USA
Posts: 4
I'm using Solaris 9 with the bash shell...

Here's a sample of what the data may look like:

# grep 'tid=1234567' /$HOME/somedirectory/somefile

:dt=031231130001:ca=50:sq=35:tid=1234567:ul=0542:tk=05:tm=07
:dt=031231130234:ca=23:sq=36:tid=1234567:tk=09:tm=12
:dt=031231130555:ca=99:sq=37:tid=1234567:ul=0425:ty=324:tk=03:tm=03
:dt=031231130925:ca=67:sq=38:tid=1234567:ul=0465:ty=324:uw=0778:tk=02:tm=22

I'd ideally search for any specific results in a particular field (which i can already do), but would like to only print out a certain field to the screen...for example, I'd like to print to the screen only the 'tk' field and value for each line (and not all the other garbage).

For each entry above though, it appears in the 6th, 5th, 7th, and then 8th field, which can be different for any entry.
If i added a 'cut -d: -f6', i'd only get the 'tk' for the first matching line and all the rest would be other fields...

How can I extract only that 'tk' field if it appears in a different place each time? That's why i was wondering if there was a way to use a string or expression as a delimiter rather than a character (like use 'tk' instead of ':' then print the first field, that being 'tk=whatever')

Thanks!
  #4 (permalink)  
Old 01-01-2004
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
sed -n '/tk=/s/^.*\(tk=[^:]*\):.*/\1/p' < datafile

or something like that...
  #5 (permalink)  
Old 01-01-2004
thekid2 thekid2 is offline
Registered User
  
 

Join Date: Dec 2003
Location: CT, USA
Posts: 4
Ohh, I think I am grasping that...please correct me if i am wrong, that basically means:

print 'tk=', then substitute, starting at the beginning of the line, any character, any # of times up to the value of 'tk', and then the ':' after that value, and any other character any number of times...then replace it all with just the value of 'tk', after the 'tk='?

I am still somewhat confused with the notation of the backreference, \(tk=[^:]*\), but pretty much get the rest...I'll try it soon!

Thank you Perderabo!
  #6 (permalink)  
Old 01-02-2004
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
No you have it wrong. The -n says don't print anything unless explicitly told. The /tk=/ selects only lines that have a tk= in them. The s/this/that/p does a substitute on the line and prints the result. The stuff between \( and \) gets saved as a field. The \1 refers to the first saved field.
  #7 (permalink)  
Old 01-02-2004
thekid2 thekid2 is offline
Registered User
  
 

Join Date: Dec 2003
Location: CT, USA
Posts: 4
ahh, thanks...
Closed Thread

Bookmarks

Tags
solaris

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 01:38 AM.


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