probem while grep

Hi ,

I used the floowing command in my script.

grep 'swi' file.txt | grep '^DEV' | cut -f 6 >> f.txt

Here file.txt contains fields of records.
My intension is to grep for the 3 letter 'swi' in one of filed, this field will contain swi as part of word.
And next grep is to grep for a 'DEV' field, this is a single field.

But the output Iam gettign nothing.

Plz help me.

Thanks.

This time I used the following command:

grep 'swi' file.txt | grep "DEV" | cut -f 6 >> f.txt

DEV word I kept in double quotes, as it should be single word, and this 3 letter word should not a part of any field.

By field I am guessing you mean it is separated by spaces.

grep 'swi' file.txt | grep ' DEV ' | cut -f 6 >> f.txt

Another version of jim solution, that handle DEV in first or last field :

grep 'swi' file.txt | egrep '(^|[[:space:]])DEV([[:space:]]|$)' | cut -f 6 >> f.txt

With awk :

awk '/swi/ && /(^|[[:space:]])DEV([[:space:]]|$)/ { print $6 }' file.txt >> f.txt

Jean-Pierre.

Hi,

file.txt list of records of fields.

grep 'swi' file.txt | grep "DEV" | cut -f 6 >> f.txt

In above command 'swi' word is a part of field.

SO I have to grep for that field which has 3 latters swi.

Next grep for 'DEV', here DEV is another field and its alone field.Its not part of field.

Is the above syntax correct.

Something like this is much easier to understand when there is a smaple text file. Can you post a small piece for the file, as well as an example of what you hope to obtain as output?

Your command grep records wich contains 'swi' and 'DEV' in any positions, for example all the following lines are matched :

switch the DEVICE
DEV is switched
key DEV not found in swi table

Show an example of input file with matching and not matching records, the the required output.

Jean-Pierre.

Hi,

Sorry to confuse you guys..

Ex:

123892TX ETV156SWI DEV 14 5:20 5:45 UBXS

Above record has 7 fields.
In above record, SWI is a part of field & DEV is separate field.
SO I have to grep a record which has SWI & DEV.

Hope this hepls.

What is the problem with my solution(s) ?

Jean-Pierre.

grep 'swi' file.txt | grep "DEV" | cut -f 6 >> f.txt

perhaps should be
grep 'SWI' file.txt | grep "DEV" | cut -f 6 >> f.txt

or
grep -i 'swi' file.txt | grep "DEV" | cut -f 6 >> f.txt

Hi,

So my understading so far is, if a searching pattern is a part of word, then we should keep it in single quotes. and if the searching pattern is itself a single word, we should keep it in double quotes.

Is that correct.

Thanks.

You either have to be exact in the grep by using "SWI"
or, tell grep to ignore case

>grep -i

Your example had "SWI" but your grep was for "swi"

NO
The following commands do exactly the same thing :

grep SWI file.txt
grep 'SWI' file.txt
grep "SWI" file.txt

Lines containing SWI are selected

Jean-Pierre.

oh...

Then wat is the difference b/w using single quotes & double quotes

Within single quotes, the shell doesn't make substitutions :

$ var=Hello
$ echo '$var'
$var
$ echo "$var"
Hello
$

Jean-Pierre.

grep 'swi' file.txt | grep "DEV" | cut -f 6 >> f.txt

But with above command Iam getting the correct result.

When I did not use DEV with doube quotes, The grep is picking up the fields which has DEV part of a field, like SWI.

So Iam not getting the correct result, when DEV is not in double quotes.

> cat files.txt
123892TX ETV156SWI DEV 14 5:20 5:45 UBXS
123892TX ETV156SWI DEVB 14 6:20 5:45 UBXS
123892TX ETV156SWX DEV 14 7:20 5:45 UBXS
123892TX ETV156swi DEV 14 8:20 5:45 UBXS
123892TX ETV156SWI DEV 14 9:20 5:45 UBXS

This will find "swi" or "SWI" because I tell it to ignore case

> grep -i 'swi' files.txt | grep " DEV " | cut -f6
123892TX ETV156SWI DEV 14 5:20 5:45 UBXS
123892TX ETV156swi DEV 14 8:20 5:45 UBXS
123892TX ETV156SWI DEV 14 9:20 5:45 UBXS

This will only find "swi"

> grep 'swi' files.txt | grep " DEV " | cut -f6
123892TX ETV156swi DEV 14 8:20 5:45 UBXS

Note in both examples, the 2nd line is not included since it has a "DEVB" and I am searching for " DEV " -- a space character before D and after V. Since in double-quotes, I am looking to exactly match. The double quotes are also important when you want to maintain more than one space, as sometimes commands will atuomatically drop out extra space characters.

Iam trying to run this script.

But its saying,

"ksh: pradeep.sh: cannot execute"

SOrry..

I did not give execute permissions to the script.

Now I could run it.

Thank for all the replies & making me clear