You are not logged in.

#1 2009-07-22 21:32:52

svanberg
Member
Registered: 2009-07-16
Posts: 88

[SOLVED] Search in a file

Hi!
I'm working on a radio script with several functions, but i need your help to continue.  I want to search for a spefic string in a file. The files content is like:

Super Hot Girls Radio Channel;http://lovepotion.org:666
Sir Camelot Danish Radio;kingandnight.org:231

So if i search for "io", this will appear:

Super Hot Girls Radio Channel
Sir Camelot Danish Radio

Last edited by svanberg (2009-07-26 22:34:13)

Offline

#2 2009-07-22 21:35:53

rebugger
Member
From: Germany
Registered: 2007-10-28
Posts: 229

Re: [SOLVED] Search in a file

if i understood you, this is what you want:

grep "io" ./file.ext

Offline

#3 2009-07-22 21:36:52

.:B:.
Forum Fellow
Registered: 2006-11-26
Posts: 5,819
Website

Re: [SOLVED] Search in a file

You should tell your search function that io is the whole string, and no subset of whatever it finds. If not it will simply print all matches - not the exact ones but, as you can see, also the strings that contain your query and do not match it exactly.


Got Leenucks? :: Arch: Power in simplicity :: Get Counted! Registered Linux User #392717 :: Blog thingy

Offline

#4 2009-07-22 21:37:29

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: [SOLVED] Search in a file

simply,

awk -F ';' '/io/ {print $1}' ./file.txt

but, if in a script, and a variable is the search term you've got to switch the quoting like so

term="$1"
awk -F ';' "/$term/"'{print $1}' $file

Last edited by brisbin33 (2009-07-22 21:39:00)

Offline

#5 2009-07-22 21:44:07

Boris Bolgradov
Member
From: Bulgaria
Registered: 2008-07-27
Posts: 185

Re: [SOLVED] Search in a file

brisbin33 wrote:

but, if in a script, and a variable is the search term you've got to switch the quoting like so

term="$1"
awk -F ';' "/$term/"'{print $1}' $file

You should also declare "file"

Last edited by Boris Bolgradov (2009-07-22 21:44:48)

Offline

#6 2009-07-22 21:52:26

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: [SOLVED] Search in a file

Boris Bolgradov wrote:
brisbin33 wrote:

but, if in a script, and a variable is the search term you've got to switch the quoting like so

term="$1"
awk -F ';' "/$term/"'{print $1}' $file

You should also declare "file"

yeah, i figured that was implied.  i just meant to highlight the fact that something like $term wouldn't get evaluated if you used the single quotes as in the first literal-search example.  thanks go to procyon for showing me that trick.

Offline

#7 2009-07-23 10:18:02

svanberg
Member
Registered: 2009-07-16
Posts: 88

Re: [SOLVED] Search in a file

When using this:

awk -F ';' "/rad/"'{print $1}' $file

I get this output:

Mix Megapol
The Voice 105,9

But i don't want to search the whole line, just all text before the ";" char and the search should not be case sensitive.

Mix Megapol;mms://streaming.sbsradio.se/03872_MixMegapol_high
The Voice 105,9;mms://streaming.sbsradio.se/03872_TheVoice_high

Offline

#8 2009-07-23 10:24:51

ngoonee
Forum Fellow
From: Between Thailand and Singapore
Registered: 2009-03-17
Posts: 7,356

Re: [SOLVED] Search in a file

You could do a grep, pipe it to sed to remove everything after the ";", and repeat the grep to make sure the match wasn't in the second half.


Allan-Volunteer on the (topic being discussed) mailn lists. You never get the people who matters attention on the forums.
jasonwryan-Installing Arch is a measure of your literacy. Maintaining Arch is a measure of your diligence. Contributing to Arch is a measure of your competence.
Griemak-Bleeding edge, not bleeding flat. Edge denotes falls will occur from time to time. Bring your own parachute.

Offline

#9 2009-07-23 11:17:55

Boris Bolgradov
Member
From: Bulgaria
Registered: 2008-07-27
Posts: 185

Re: [SOLVED] Search in a file

Try this:

cat file.txt | cut -d ';' -f1 | grep -i 'io'

This will print everything that contains 'io' before ';'

Last edited by Boris Bolgradov (2009-07-23 11:20:29)

Offline

#10 2009-07-23 12:41:59

FeatherMonkey
Member
Registered: 2007-02-26
Posts: 313

Re: [SOLVED] Search in a file

I'm no awk guru but this seems to work.

echo "Mix Megapol;mms://streaming.sbsradio.se/03872_MixMegapol_high
The Voice 105,9;mms://streaming.sbsradio.se/03872_TheVoice_high
The Rad;mms:/streaming.sbsradio.se/03872_The_rad" | awk 'BEGIN{OFS=FS=";";IGNORECASE=1}$1 ~ /rad/{print$1}'

Offline

#11 2009-07-23 13:53:43

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: [SOLVED] Search in a file

FeatherMonkey wrote:

I'm no awk guru but this seems to work.

echo "Mix Megapol;mms://streaming.sbsradio.se/03872_MixMegapol_high
The Voice 105,9;mms://streaming.sbsradio.se/03872_TheVoice_high
The Rad;mms:/streaming.sbsradio.se/03872_The_rad" | awk 'BEGIN{OFS=FS=";";IGNORECASE=1}$1 ~ /rad/{print$1}'

this line seems to work best; throwing a second | grep "rad" on the end of mine would also ensure that the match is only for the first portion, but that's a bit of a hack.

Offline

#12 2009-07-26 22:33:57

svanberg
Member
Registered: 2009-07-16
Posts: 88

Re: [SOLVED] Search in a file

Thanks for all your help. Now I am able to continue again.

Offline

Board footer

Powered by FluxBB