Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

grep exact pattern match.

Posted on 2010-09-22
13
Medium Priority
?
1,690 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
13 Comments
 
LVL 8

Accepted Solution

by:
nognew earned 288 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 288 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
 
LVL 3

Assisted Solution

by:egarciat
egarciat earned 284 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 284 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 284 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 284 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 288 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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Suggested Courses

730 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