Solved

grep exact pattern match.

Posted on 2010-09-22
13
1,681 Views
Last Modified: 2012-05-10
1) I want to do an exact pattern matching

soemthing fill
type : FILL

something manual fill
type : MANUAL_FILL

if I do grep -B 1 "FILL" should return only the first pair of lines. how can I do that?

grep -B 1 -x "FILL" is not working. Please suggest.

2)  How to change the shell from csh to bash?

> echo $SHELL
/bin/csh
chsh -s /bin/bash
chsh: can only change local entries; use ypchsh instead.
> grep bash /etc/shells
/bin/bash
> bash
bash-3.2$ echo $SHELL
/bin/csh
bash-3.2$ exit
> which bash
/bin/bash

3)     #if [ $# -ne 1 ] then
    #    echo "Usage: cat logfile | sh `basename $0` ORDER_TYPE"
    #    exit 1
    #fi
The above if condition is giving me an error. Why is that? looks valid to me.
0
Comment
Question by:srikanthrad
13 Comments
 
LVL 8

Accepted Solution

by:
nognew earned 72 total points
ID: 33736036
Hi there!
1.
a) grep -B 1 "FILL$"
b) grep -B1 "type : FILL"

2. perhaps your /bin/csh and /bin/bash are the same binaries. Do ls -la /bin/csh /bin/bash and see if one is the link to anohter or they have the same size, or one of them is not a binary but a shell script whcih starts another. you can define their type by executing: file /bin/bash, file /bin/csh
Your real bash might be found in /usr/local/bin/bash, but your PATH variable might not be set to search in this location.

3. I think you missing semicolon after ]

Cheers,
t.
0
 
LVL 14

Expert Comment

by:Deepak Kosaraju
ID: 33736928
you can use egrep -B 1 "^FILL$"
this implies pattern just started with FILL and ends with FILL.
0
 

Author Comment

by:srikanthrad
ID: 33737416
1) Thanks for that.

2)
> ls -la /bin/csh
lrwxrwxrwx 1 root root 4 Feb 18  2009 /bin/csh -> tcsh*
> ls -la /bin/bash
-rwxr-xr-x 1 root root 801504 Feb  1  2008 /bin/bash*

3)
if [ $# -ne 1 ];then
    echo "Usage: cat logfile | sh `basename $0` ORDER_TYPE"
    exit 1

Even If I put a semicolon, I am getting error. But, when I am using $# != 1then I am getting it right.

0
 

Author Comment

by:srikanthrad
ID: 33737438
> ls -la /usr/local/bin/b*
ls: No match.
> ls -la /usr/local/bin/c*
ls: No match.
> ls -la /usr/local/bin/
total 16
drwxr-xr-x  2 root root 4096 Oct 10  2006 ./
drwxr-xr-x 13 root root 4096 Feb 18  2009 ../
0
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 72 total points
ID: 33737600
2) You're using NIS! Does ypchsh not work for you? If it doesn't,  you'll have to ask the administrator of your NIS server to change your  login shell for you.
 
 3) csh is a bit different!

if ( $#argv != 1 ) then
   echo "Usage: cat logfile | sh `basename $0` ORDER_TYPE"
   exit 1
endif

wmp
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 33737630
BTW,

there is no need to change your login shell to run your script under bash!

Just put into the very first line of the script

#!/bin/bash

This determines which shell to use for interpreting the script.
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 3

Assisted Solution

by:egarciat
egarciat earned 71 total points
ID: 33737861
Regarding your first question:

you may use grep -m

extracto from the grep man page:

       -m NUM, --max-count=NUM
              Stop  reading  a file after NUM matching lines.  If the input is
              standard input from a regular file, and NUM matching  lines  are
              output......

see if your grep supports this option..


Regarding third question:

if [ $# -lt 1 ]; then
do_something
fi

the spaces are important for the "test" command ([]) to work,

0
 

Assisted Solution

by:Ufomen
Ufomen earned 71 total points
ID: 33739388
-w       Searches for the expression as a word  as  if  surrounded by \< and \>.

#  grep -w world infile
1 Hello world fantasy.
3 Hello world   wonderful.

0
 
LVL 13

Assisted Solution

by:Superdave
Superdave earned 71 total points
ID: 33740214
If you want your $SHELL to be something else you have to set it to what you want yourself.  You could put something like this at the end of your .profile:
setenv SHELL /bin/bash
exec /bin/bash

0
 
LVL 3

Assisted Solution

by:martin_sea
martin_sea earned 71 total points
ID: 33741145
-->
put below in your user .profile to set shell for your user
setenv SHELL /bin/bash

even for the !/bin/bash can be used in your script for bash scripting.

-->
grep -w hi *
"To search for the expression as a word [ A exact match for the word ""hi"" ] (EXACT WORD MATCH)
(The whole word should be matched be matched)"

"bash-3.00$ grep -w hi *
bash-3.00$
bash-3.00$ grep -n ghi *
testgrep:3:ghi,h"


--> check with below.
if [ $# -ne "1" ]; then
 echo "Number equals 1"
else
    echo "Number does not equal 1"
fi
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 33741730
@martin_sea - try just for fun in "testgrep":
g,hi,h
; -)
0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 72 total points
ID: 33749869
1.)   grep -w -B1 FILL filename

2.)  As it reports, use ypcsh instead

3.)  Use:

if [ $# -ne 1 ]
then
     echo "Usage: cat logfile | sh `basename $0` ORDER_TYPE"
     exit 1
fi
0
 

Author Closing Comment

by:srikanthrad
ID: 33762730
Thanks all for your solutions.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now