Solved

grep exact pattern match.

Posted on 2010-09-22
13
1,679 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
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

747 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

11 Experts available now in Live!

Get 1:1 Help Now