LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-22-2010, 09:37 AM   #1
j123
LQ Newbie
 
Registered: Mar 2010
Posts: 12

Rep: Reputation: 0
question regarding finding data from a text file


Hi there, I'm trying to write a shell script which finds bits of data from a text file. at the moment i'm using grep and basically i need a function which will look through the text file and take the data out of it. the file has days, months, years etc and i want it so i can type feb 06 and it finds all of the data for feb 06. the problem i have is i can type feb and all the information comes back for feb, but i can't get it more precise e.g. feb 2009 and it finds just feb 2009, it seems to ignore that latter half. I've tried experimenting with egrep and having two inputs but i can't seem to fuse them together, it only takes the first input. any help on this would be much appreciated, thanks guys
 
Old 03-22-2010, 10:09 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,505

Rep: Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890
Tried putting the words in inverted commas?

grep "feb 06" file
 
Old 03-22-2010, 01:12 PM   #3
j123
LQ Newbie
 
Registered: Mar 2010
Posts: 12

Original Poster
Rep: Reputation: 0
Sorry I haven't explained it properly. I'm developing a shell script which prompts the user to ask for a time period. I have grep '$INPUT' but I cannot seem to get precise information because the second part is not taken into account e.g. I type in Feb 06 it only brings up the hits in February in all different years because the information in the file is written day, month, time, year so unless i specify it in that order i only get the first part. I want to be able to make the script so the user can ask for a month and year and it brings it up.But at the moment i can't because to do it i need to write the other parts which is no good
 
Old 03-22-2010, 03:19 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
You still haven't explained it well at all ...

Can you post some sample data and expected outcomes?


Cheers,
Tink
 
Old 03-22-2010, 03:43 PM   #5
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,120

Rep: Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876
i think you need to "quote" the variable when referencing it.

please post [code] and sample output so we can test it on our side.
 
Old 03-22-2010, 08:57 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,505

Rep: Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890
schneidz is correct, by placing your variable in single quotes you are not preserving the white space.

Try grep "$INPUT"
 
Old 03-23-2010, 05:47 AM   #7
j123
LQ Newbie
 
Registered: Mar 2010
Posts: 12

Original Poster
Rep: Reputation: 0
here is the code
#!/bin/sh
echo "what do you want to find?"
read INPUT
DATA = grep "$INPUT" -i /home/directory/etc
echo $DATA

the text file i am taking information from is set up like this

student no. day registered month registered time year e.g.
123456 mon MAR 17 13:00 2006

with my code, i can find something if i type it in order e.g. mon mar 17 it will bring up those records, but if i try typing MAR 2006 it won't find anything.
 
Old 03-23-2010, 07:27 AM   #8
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,120

Rep: Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876
seems you would need to parse the number of input feilds and do your searches based on how many things the student wants to look for. this is quick and dirty:
Code:
grep -i `echo $INPUT | cut -d " " -f 1`.*`echo $INPUT | cut -d " " -f 2`.*`echo $INPUT | cut -d " " -f 3`.*`echo $INPUT | cut -d " " -f 4`.*`echo $INPUT | cut -d " " -f 5`.*`echo $INPUT | cut -d " " -f 6` /home/directory/etc

Last edited by schneidz; 03-23-2010 at 07:55 AM.
 
Old 03-23-2010, 08:30 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,505

Rep: Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890
j123 ... issue seems to be your understanding of regular expressions.
In your example:
Quote:
123456 mon MAR 17 13:00 2006
You can do grep "mon MAR" as these words are side by side in the file so it will find them accordingly.
However, MAR and 2006 are not side by side so you need to tell grep about the fact that there might be
something else in between, hence you would need to do:

Code:
grep "MAR.*2006" file
This says, look in the file until you find the word MAR and then look to see if after zero or more
of any other character(s) that the characters 2006 appear. If yes, return line

Obviously now you need to ask yourself, to what level will you allow the users to enter data?
ie, single word only, words separated by space, regular expressions, ...
 
  


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
remove no data rows from a text file lothario Linux - Newbie 1 08-30-2009 07:18 AM
C++ - Store a text file in a Data Structure nilly16 Programming 3 05-26-2009 06:42 AM
Retreiving data from a 60G text file? blackout Linux - Server 1 02-09-2008 10:07 PM
outputting data from a directory to a text file? minm Linux - Newbie 2 12-19-2004 06:46 PM
Finding Text in an html file Xaque208 Linux - Software 3 11-15-2004 10:32 PM


All times are GMT -5. The time now is 11:59 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration