Operating System - HP-UX
1832682 Members
2790 Online
110043 Solutions
New Discussion

scripting.. searching for a string

 
SOLVED
Go to solution
inventsekar_1
Respected Contributor

scripting.. searching for a string

admins,

each line in a file contains four strings within double quotes("").

like this:
An "Apple" "a" day "keeps" the "doctor" away.

i need to grep for this this four strings in each line.

PS:i understand the need to learn GOOD scriping.
Be Tomorrow, Today.
18 REPLIES 18
inventsekar_1
Respected Contributor

Re: scripting.. searching for a string

An "Apple" "a" day "keeps" the "doctor" away.

IN this line i need to grep the four strings and put in a new file as:

Apple a keeps doctor
Be Tomorrow, Today.
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: scripting.. searching for a string

Your are not specific enough but if the goal is to match the quoted strings in that order then:

grep -E '"Apple".*"a".*"keeps".*"doctor"' < myfile

Now if the order is not important then:

grep -E '"Apple"' < myfile | grep '"a"' | grep '"keeps"' | grep -E '"doctor"'

You can add a -i in addition to each -E to make grep's matchs case-insensitive. Note that the single-quotes are used to escape the double-quotes in your target strings.

If it ain't broke, I can fix that.
inventsekar_1
Respected Contributor

Re: scripting.. searching for a string

sorry for that.

i need to search for the four strings(each string will be inside "") in a line.

sorry for the wrong example.
the strings are not constant. so i need to search for the strings inside "".
Be Tomorrow, Today.
A. Clay Stephenson
Acclaimed Contributor

Re: scripting.. searching for a string

PS:i understand the need to learn GOOD scriping.

In that case, you should change the question because the task as you now define it (with your second posting) is not a good use of grep. There are better tools for the task when you want to print only the target strings.

typeset -i I=1
typeset -i N=$(grep -E '"Apple".*"a".*"keeps".*"doctor"' myfile | wc -l)
while [[ ${I} -le ${N} ]]
do
echo "Apple a day doctor" >> newfile
((I += 1))
done
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: scripting.. searching for a string

And now we are on to yet another question. I think I will wait until you can phrase the question exactly because unless I'm quail hunting I don't like shooting at moving targets.

If it ain't broke, I can fix that.
Yang Qin_1
Honored Contributor

Re: scripting.. searching for a string

cat file1 |cut -d"\"" -f2,4,6,8|sed 's/\"/ /g'
H.Merijn Brand (procura
Honored Contributor

Re: scripting.. searching for a string

# perl -nle'BEGIN{%h=map{$_=>1}qw(apple a keeps doctor)}$n=0;$n+=$h{lc$1}for/"(\w+)"/g;$n==4&&print' file

Just a go.
Will print all lines in the file that has all 4 words "apple", "a", "keeps", and "doctor" quoted in any casing in any order.

That what you want?
Probably can be done even shorter, but I don't wanna play golf right now

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Hein van den Heuvel
Honored Contributor

Re: scripting.. searching for a string


>> PS:i understand the need to learn GOOD scriping.

The single most critical part of good scripting it learn to get and capture a good problem statement. Exactly what needs to happen? What should not happen? which eventualities need to be protected against (defensive scripting!).

I find that clear sample data, both input and output are critical in understanding the problem, the constraints of the solution, and as 'proof' that the script works.

Now as far as the task on hand...

What does 'grep for this this four strings' mean?

Do you want to grep for a single string in any of those for 'fields' but not outside?

So if the data was

1: An "Apple" "a" day "keeps" the "doctor" away.
2: An "pear" in "the" night is "just" "right".

Now when you 'grep' for 'the', should find the second line but not the first?

This awk will do that... just barely:

>awk -F\" -v x=the "{ if (index($2 $4 $6 $8, x) ) { print }}" tmp.txt

How does that work?

If a double quote is made the 'split' character with -F, then every other variable will be a quoted word: 2,4,6,8...
"$2 $4 $6 $6" glues them together, and if we find a non-zero return then the string pushed in x must be in there.

Now a sample contraint question:

In the code about 'theju' will match "the" "just" is that acceptable?

If not then we either have to look in the individual strings:

>awk -F\" -v x=the "{if (index($2,x) + index($4,x) + index($6,x) + index($8,x)) { print }}" tmp.txt

or use same acceptable seperator (here *), passed in as a variable to keep it flexible.

>awk -F\" -v x=theju -v a=* "{ if (index($2 a $4 a $6 a $8, x) ){print} }" tmp.txt

so many solutions...
so many gotchas.

Enjoy,
Hein.

Peter Nikitka
Honored Contributor

Re: scripting.. searching for a string

Hi,

use the quote as delimiter and output the even numbered fields:

awk -F'"' 'NF==9 {for(i=2;i
will do this.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Sandman!
Honored Contributor

Re: scripting.. searching for a string

As said by Hein and Peter the trick is to print the even fields...here's my script:

# awk -F\" '{for(i=1;i<=NF;++i){if(!(i%2)) printf("%s ",$i)} print ""}' infile

~hope it helps
inventsekar_1
Respected Contributor

Re: scripting.. searching for a string

Thanks goes to all. sorry for the delayed response.

i thought to edit the first post thru Moderator intervention, but when others will read after sometime, they will not come to know what happend..so i am leaving that as it is.

now again, the REAL question is:
my file contains line like this:

firstLine: CONSTANT="no" NAME="abcde" initialValue="1" NAME="abcde"
secondLine: CONSTANT="no" NAME="fghij" initialValue="0" NAME="fghij"

1.)each line contains four strings
2.)out of which first string is always "no"
3.)second string and fourth string are same.
4.)third string can take only one of two values (0 or 1).

now, from this two lines:
CONSTANT="no" NAME="abcde" initialValue="1" NAME="abcde"
CONSTANT="no" NAME="fghij" initialValue="0" NAME="fghij"

i need to create a file like this:
abcde 1
fghij 0

note that: i cant "cut" since in the line starting there may be one space or two spaces or a tab.

hope u understand it clearly now.
it may be simple for GOOD admins.
Be Tomorrow, Today.
Jonathan Fife
Honored Contributor

Re: scripting.. searching for a string

Quick and dirty. There is probably a way to remove the quotes in awk so you don't have to fork a second sed process:

cat file | awk -F"[ =]" '{print $4, $6}' | sed -e 's/"//g'
Decay is inherent in all compounded things. Strive on with diligence
H.Merijn Brand (procura
Honored Contributor

Re: scripting.. searching for a string

You *CAN* use cut:

lt09:/home/merijn 108 > cat xx.dta
CONSTANT="no" NAME="abcde" initialValue="1" NAME="abcde"
CONSTANT="no" NAME="fghij" initialValue="0" NAME="fghij"
lt09:/home/merijn 109 > cut -d\" -f4,6 xx.dta
abcde"1
fghij"0
lt09:/home/merijn 110 >

Or, *literally* applying that criteria:

lt09:/home/merijn 110 > perl -ne's/^.*?(NAME="([^"]+)").*?"([01])"\s+\1.*/$2\t$3/&&print' xx.dta
abcde 1
fghij 0
lt09:/home/merijn 111 >

Enjoy, Have FUN! H.merijn
Enjoy, Have FUN! H.Merijn
Yang Qin_1
Honored Contributor

Re: scripting.. searching for a string

cat file1|awk 'FS="\"" {print $4 " " $6}'
James R. Ferguson
Acclaimed Contributor

Re: scripting.. searching for a string

Hi:

Using 'cat' in a pipeline to 'swk' is a waste of another process!

# cat file | awk 'FS="\"" {print $4 " " $6}'

...should be:

# awk 'FS="\"" {print $4 " " $6}' file

...or, more succinctly:

# awk 'FS="\"" {print $4,$6}' file

Regards!

...JRF...

Sandman!
Honored Contributor

Re: scripting.. searching for a string

# awk -F\" '{print $4,$6}' infile

~cheers
Peter Nikitka
Honored Contributor

Re: scripting.. searching for a string

Hi,

and two chars shorter as James' and even working in gawk:

awk -F'"' '{print $4,$6}' file

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
James R. Ferguson
Acclaimed Contributor

Re: scripting.. searching for a string

Hi Peter:

Yeah, two characters shorter --- I like that better than mine!

Regards!

...JRF...