grep exact pattern match.

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.
srikanthradAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
nognewConnect With a Mentor Commented:
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
 
Deepak KosarajuDevOps EngineerCommented:
you can use egrep -B 1 "^FILL$"
this implies pattern just started with FILL and ends with FILL.
0
 
srikanthradAuthor Commented:
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
A proven path to a career in data science

At Springboard, we know how to get you a job in data science. With Springboard’s Data Science Career Track, you’ll master data science  with a curriculum built by industry experts. You’ll work on real projects, and get 1-on-1 mentorship from a data scientist.

 
srikanthradAuthor Commented:
> 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
 
woolmilkporcConnect With a Mentor Commented:
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
 
woolmilkporcCommented:
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
 
egarciatConnect With a Mentor Commented:
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
 
UfomenConnect With a Mentor Commented:
-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
 
SuperdaveConnect With a Mentor Commented:
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
 
martin_seaConnect With a Mentor Commented:
-->
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
 
woolmilkporcCommented:
@martin_sea - try just for fun in "testgrep":
g,hi,h
; -)
0
 
TintinConnect With a Mentor Commented:
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
 
srikanthradAuthor Commented:
Thanks all for your solutions.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.