Unix Shell - Grep.

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > November 2006 > Grep.





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Grep.
Gazza

2006-11-22, 1:39 am

Hi,

Suppose I have a plain-text file called 'file1' with the following lines in
it:

> 9< item1
> 99< item2
>999< item3


(note the '>', '<' and spaces). How would I grep, separately, for:

'> 9< ';
'> 99< '; and
'>999< '

? I've tried everything I can think of, including (for the first line):

grep -h "> 9< " file1
grep -h '> 9< ' file1
grep -h ">\ \ 9<" file1
grep -h '>\ \ 9<' file1
grep -h "\>\ \ 9\<" file1
grep -h '\>\ \ 9\<' file1
grep -h "\\> \ \ 9\\<" file1
grep -h '\\> \ \ 9\\<' file1

(the '-h' is just to prevent the /path/to/file being output by grep).

Thanks, in advance, for any help.

Yours,
Gary Hayward.

Gazza

2006-11-22, 1:39 am

Hi,

Sorry: I should have mentioned that it's best to view these messages in a
mono-spaced font.

Yours,
Gary Hayward.

Gazza wrote:

> Hi,
>
> Suppose I have a plain-text file called 'file1' with the following lines
> in it:
>
>
> (note the '>', '<' and spaces). How would I grep, separately, for:
>
> '> 9< ';
> '> 99< '; and
> '>999< '
>
> ? I've tried everything I can think of, including (for the first line):
>
> grep -h "> 9< " file1
> grep -h '> 9< ' file1
> grep -h ">\ \ 9<" file1
> grep -h '>\ \ 9<' file1
> grep -h "\>\ \ 9\<" file1
> grep -h '\>\ \ 9\<' file1
> grep -h "\\> \ \ 9\\<" file1
> grep -h '\\> \ \ 9\\<' file1
>
> (the '-h' is just to prevent the /path/to/file being output by grep).
>
> Thanks, in advance, for any help.
>
> Yours,
> Gary Hayward.


rajesh.tamil.india@gmail.com

2006-11-22, 1:39 am

Hi,

You should use awk to perform column select

awk 'BEGIN{FS="< *"}{print $1 "<"}' file1.txt

Gazza wrote:[vbcol=seagreen]
> Hi,
>
> Sorry: I should have mentioned that it's best to view these messages in a
> mono-spaced font.
>
> Yours,
> Gary Hayward.
>
> Gazza wrote:
>

Chris F.A. Johnson

2006-11-22, 7:27 am

On 2006-11-22, Gazza wrote:
> Hi,
>
> Suppose I have a plain-text file called 'file1' with the following lines in
> it:
>
>
> (note the '>', '<' and spaces). How would I grep, separately, for:
>
> '> 9< ';
> '> 99< '; and
> '>999< '


What do you mean by separately? Do you only want the pattern you
are searching for to be printed? If so, that is not normally grep's
function, although recent versions of GNU grep have the -o option
that does that.

> ? I've tried everything I can think of, including (for the first line):
>
> grep -h "> 9< " file1
> grep -h '> 9< ' file1
> grep -h ">\ \ 9<" file1
> grep -h '>\ \ 9<' file1


If you want the entire line, any of the above will work.

If it doesn't work, what, exactly, happens?

> grep -h "\>\ \ 9\<" file1
> grep -h '\>\ \ 9\<' file1
> grep -h "\\> \ \ 9\\<" file1
> grep -h '\\> \ \ 9\\<' file1
>
> (the '-h' is just to prevent the /path/to/file being output by grep).


The /path/to/file will not be printed if there is only one file on
the command line.

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
BobbyH

2006-11-22, 7:27 am

The answer will depend on the type of UNIX system you are working on.

When I enclose the search string in single quotes it works on my Solaris and
AIX systems.

I tried using single quotes and angle brackets on Solaris:
$ grep '\<> 9<\>' file1
> 9< item1

$ grep '\<>999<\>' file1
>999< item3

On Solaris the \<string\> is synonymous with -w or word option. Escaping
the angle brackets will look for the exact string < 9> or <999> in a
search.


The angle brackets do not work on my AIX systems, the grep works by simply
enclosing the search string in single quotes:
$ grep '> 9<' file1
> 9< item1

$ grep '>999<' file1
>999< item3

$

"Gazza" <gazza@192.168.1.1> wrote in message
news:RoO8h.22425$yz3.7698@newsfe4-gui.ntli.net...
> Hi,
>
> Suppose I have a plain-text file called 'file1' with the following lines
> in
> it:
>
>
> (note the '>', '<' and spaces). How would I grep, separately, for:
>
> '> 9< ';
> '> 99< '; and
> '>999< '
>
> ? I've tried everything I can think of, including (for the first line):
>
> grep -h "> 9< " file1
> grep -h '> 9< ' file1
> grep -h ">\ \ 9<" file1
> grep -h '>\ \ 9<' file1
> grep -h "\>\ \ 9\<" file1
> grep -h '\>\ \ 9\<' file1
> grep -h "\\> \ \ 9\\<" file1
> grep -h '\\> \ \ 9\\<' file1
>
> (the '-h' is just to prevent the /path/to/file being output by grep).
>
> Thanks, in advance, for any help.
>
> Yours,
> Gary Hayward.
>



Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com