Help with searching a text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with searching a text file
# 1  
Old 01-01-2004
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  
Old 01-01-2004
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  
Old 01-01-2004
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  
Old 01-01-2004
sed -n '/tk=/s/^.*\(tk=[^:]*\):.*/\1/p' < datafile

or something like that...
# 5  
Old 01-01-2004
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  
Old 01-02-2004
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.
This User Gave Thanks to Perderabo For This Post:
# 7  
Old 01-02-2004
ahh, thanks...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help searching through a text file.

I'm trying to get one specific number out of a text file, in order to use as part of an algorithm in a shell script. It will always come after a specific string and that string won't appear anywhere else in the file. So I'm trying to search through the file for that one string, then grab the... (2 Replies)
Discussion started by: mikedigornio
2 Replies

2. Shell Programming and Scripting

Help in searching a multiple text in zip file

Hi Gurus, i have 8 zipped files and each file is having more than 100,000 records or more. issue :- i want to search the missing text from each zipped files i have stuck here, the below command works fine if i give the value 10 for the deptno. if i have more than 1 records... (6 Replies)
Discussion started by: SeenuGuddu
6 Replies

3. Shell Programming and Scripting

searching a file with a specified text without using conventional file searching commands

without using conventional file searching commands like find etc, is it possible to locate a file if i just know that the file that i'm searching for contains a particular text like "Hello world" or something? (5 Replies)
Discussion started by: arindamlive
5 Replies

4. Shell Programming and Scripting

Need help with searching and copying in a text file

Hi, I need help searching through a large text file. I need to find a certain string within the text, and copy each line until another string appears. The file looks like this: >scf15164843 ATTAAAGGNNNGGAATTTCCCCAA ATTACCGGCTTTAAANNNTTACCC >scf15154847 CCGGGNNNTTTAAACCCGNGNGCC... (2 Replies)
Discussion started by: repiv
2 Replies

5. Shell Programming and Scripting

String searching and output to a file in a formatted text

Hi, I'm very new to UNIX scripting and find quite difficult to understand simple UNIX syntax. Really appreciat if somebody could help me to give simple codes for my below problems:- 1) I need to search for a string "TTOH 8031950001" in a files which filename will be "*host*'. For example, the... (3 Replies)
Discussion started by: cuji
3 Replies

6. UNIX for Dummies Questions & Answers

Searching a text file and assigning it to a variable

Hi Gurus, I am new to unix.I have a requirement as below I have text file like a.txt which contains a.txt hi hello process update status Ok to Proceed no issues good data arrangement My requirement here is i need to read the file and check for the words "OK to Proceed" and if... (2 Replies)
Discussion started by: pssandeep
2 Replies

7. Shell Programming and Scripting

PERL: Searching for a string in a text file problem

Looking for a bit of help. I need to search for a string of words, but unfortunately these words are located on separate lines. for example the text output is: United Chanmpions Ronaldo Liverpool Losers Torres and my script code is print("DEBUG - checking file message"); while... (15 Replies)
Discussion started by: meevagh
15 Replies

8. UNIX for Dummies Questions & Answers

Searching for text in a Space delimited File

Hi I am trying to search a firewall syslog space delimeted file for all of the different tcp and udp destination ports. I know that grep will find lines that contain specific text. And I have tried using the the the cut command to cut out of the file certain colums. However the test I am... (6 Replies)
Discussion started by: andyblaylock
6 Replies

9. UNIX for Dummies Questions & Answers

Searching directory for file that contains some text.

If I go into a directory and type in .. more * | grep foo I get the lines of text that contain foo in all of the files in the directory out of all of the files that are there. How do I make it so I can find out what the names of the files are that contain that text "foo"? By doing what I... (4 Replies)
Discussion started by: LordJezo
4 Replies

10. Shell Programming and Scripting

searching each file in a directory for text

what command can i use to search the files in a directory for a text. the output would list the files containing the text. ive tried this but it is not exactly what im looking to do: find . -name "*.xml" -exec agrep searchstring {} \; (2 Replies)
Discussion started by: jim majors
2 Replies
Login or Register to Ask a Question