Solved

grep exact pattern match.

Posted on 2010-09-22
13
1,683 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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
 
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Access_log 17 121
How to learn Linux? 10 61
Need BIOS update Linux for MSI X99A motherboard. 4 43
Linux Real Time Memory on Distinct 24 Cycle 7 21
Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
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.

832 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