Problem with Grep command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with Grep command
# 1  
Old 10-15-2012
Problem with Grep command

Hi All,

Below is my issue.

File consist of the 2 server information. This information I am pulling from Netezza table and keeping it in tmp file.

File format:
database name|schemaname|server number

eg: test1.tmp
FWKD|FWKD_DEV|1
FWKD1|FWKD_QA|2

my requirement is to fetch the one server at a time based on the parameter which I am passing while executing the script.

if I ran the below script sh test.ksh 1 I need to get only one server information. But below code is returning wrongly

test.ksh

v_server_nbr=$1

line=`cat test1.tmp | grep $v_server_nbr`
echo $line

v_ora_srv_nme=`echo $line | awk -F '|' ' {print $1}'`
v_ora_sch_nme=`echo $line | awk -F '|' ' {print $2}'`
v_srv_nbr=`echo $line | awk -F '|' ' {print $3}'`

output :
If is run the script I am getting the wrong output for v_srv_nbr

> sh test1.ksh 1

FWKD|FWKD_DEV|1 FWKD1

I need the output like this FWKD|FWKD_DEV|1

If I am giving > sh test1.ksh 2 it is giving correct output. Due to '1' in the database name it is giving wrongly for first time.

Please provide me the clarification on it.

Thanks,
Chandu

Last edited by bbc17484; 10-15-2012 at 10:46 PM..
# 2  
Old 10-15-2012
Using a shell script:

Code:
$
$ cat f10
FWKD|FWKD_DEV|1
FWKD1|FWKD_QA|2
$
$ cat f10.sh
#!/usr/bin/ksh
sn=$1
IFS="|"
while read db sch srv
do
  if [ "$srv" == "$sn" ]
  then
    echo "$db|$sch|$srv"
    exit
  fi
done < f10

$
$
$ ./f10.sh 1
FWKD|FWKD_DEV|1
$
$

Using awk or Perl would be shorter and easier -

Code:
$
$
$ awk -F"|" -v sn="2" '$3 == sn' f10
FWKD1|FWKD_QA|2
$
$
$ perl -F"\|" -slane 'print if $F[2] == $sn' -- -sn=2 f10
FWKD1|FWKD_QA|2
$
$

tyler_durden
# 3  
Old 10-15-2012
First of all, grep can read the file, you don't need to use cat.

Search for the number immedately after a pipe, and at the end of the line
Code:
line=`test1.tmp  grep "|${v_server_nbr}$" test1.tmp `

# 4  
Old 10-15-2012
Use awk to match line and output the fields you need:

Code:
#!/usr/bin/ksh
set -- `awk -F '|' -v N="$1" '$3==N{print $1,$2,$3}' test1.tmp`
v_ora_svr_nme=$1
v_ora_sch_nme=$2
v_srv_nbr=$3
echo "$v_ora_svr_nme|$v_ora_sch_nme|$v_srv_nbr"

This User Gave Thanks to Chubler_XL For This Post:
# 5  
Old 10-15-2012
Thanks for replying me Tyler. This is the part of the code in one of my script.
so If I used the AWK, that is the better option.

In your code I couldn't understand one thing. I completely new to AWK programming.

awk -F"|" -v sn="2" '$3 == sn' f10

here what is the meaning of sn & f10. Here run time I will provide the server number.
like
sh test.ksh 1
sh test.ksh 2

---------- Post updated at 08:01 AM ---------- Previous update was at 07:58 AM ----------

Thanks for reply me Agma.

I tried that option. Placing | before server number. But in this case

FWKD|FWKD_DEV|1
FWKD1|FWKD_QA|2

it will work. For below data it wont work Smilie

FWKD|FWKD_DEV|1
FWKD1|FWKD_QA|2
FWKQ|1FWKD_QA|3

---------- Post updated at 08:06 AM ---------- Previous update was at 08:01 AM ----------

Chubler.. Thanks for replying me.

I am new to AWK programming. Can I pass the parameter 1/2 while executing the script ?? will it return only one server information

> sh test.ksh 1
FWKD|FWKD_DEV|1

> sh test.ksh 2
FWKD1|FWKD_QA|2


Note : This logic I need to use it in the middle of the script

Thanks,
Chandu
# 6  
Old 10-15-2012
Quote:
Originally Posted by bbc17484
...here what is the meaning of sn & f10....
f10 is my data file and sn is a variable.

Quote:
Originally Posted by bbc17484
...I completely new to AWK programming....
In that case, a good place to start is the manual:
Code:
man awk

Save the output to a file, print it out and refer to it at all times.

tyler_durden
This User Gave Thanks to durden_tyler For This Post:
# 7  
Old 10-16-2012
The AWK Manual - Table of Contents

Is a readable explanation of awk syntax and reserved words. It gives examples of everything. nawk is the new awk (now POSIX awk).

This is also a good reference especially for things like what operators there are .. things like +, -, *, / etc.....

awk
This User Gave Thanks to jim mcnamara For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

I'm facing problem with rpm command, when running the command and appears this error:

exec(): 0509-036 Cannot load program /usr/opt/freeware/bin/rpm because of the following errors: 0509-022 Cannot load module /opt/freeware/lib/libintl.a(libintl.so.1). 0509-150 Dependent module /opt/freeware/lib/libiconv.a(shr4.o) could not be loaded. 0509-152 Member... (4 Replies)
Discussion started by: Ohmkar
4 Replies

2. Shell Programming and Scripting

Grep command giving different result for different users for same command

Hello, I am running below command as root user #nodetool cfstats tests | grep "Memtable switch count" Memtable switch count: 12 Where as when I try to run same command as another user it gives different result. #su -l zabbix -s /bin/bash -c "nodetool cfstats tests | grep "Memtable switch... (10 Replies)
Discussion started by: Pushpraj
10 Replies

3. UNIX for Dummies Questions & Answers

sed Or Grep Problem OR Terminal Problem?

I don't know if you guys get this problem sometimes at Terminal but I had been having this problem since yesterday :( Maybe I overdid the Terminal. Even the codes that used to work doesn't work anymore. Here is what 's happening: * I wanted to remove lines containing digits so I used this... (25 Replies)
Discussion started by: Nexeu
25 Replies

4. Shell Programming and Scripting

Problem with "find" and "grep" command

I want to list all files/lines which except those which contain the pattern ' /proc/' OR ' /sys/' (mind the leading blank). In a first approach I coded: find / -exec ls -ld {} | grep -v ' /proc/| /sys/' \; > /tmp/list.txt But this doesn't work. I got an error (under Ubuntu): grep:... (5 Replies)
Discussion started by: pstein
5 Replies

5. UNIX for Advanced & Expert Users

Problem with grep command options in Sunsolaris

Hi Experts I need the following output from grep command of Sunsolaris on a set of input files. Output:........ 1st search string from file1 2nd search string from file1 3rd search string from file1 1st search string from file2 2nd search string from file2 3rd search string from... (3 Replies)
Discussion started by: ks_reddy
3 Replies

6. Shell Programming and Scripting

can anyone help with shell script command about searching word with grep command?

i want to search in the current directory all the files that contain one word for example "hello" i want to achieve it with the grep command but not with the grep * (2 Replies)
Discussion started by: aintour
2 Replies

7. Shell Programming and Scripting

grep problem

hi, i need to get a count of the lines that contain 2 given patterns. eg: Line1 is <PATTERN1>blahblahblah</PATTERN1>then more text <PATTERN2>blahblahblah</PATTERN2> Line 2 is <PATTERN1>blahblahblah</PATTERN1>then some more text<INVALIDPATTERN>whatever</INVALIDPATTERN> I want to get a count of... (1 Reply)
Discussion started by: King Nothing
1 Replies

8. Solaris

Grep problem

Hi all, I get this this error message grep: illegal option -- q Usage: grep -hblcnsvim pattern file when I execute this command rsh <host ip> " ps -ef | grep '26' " & ur cooperation is appreciated (0 Replies)
Discussion started by: AshAdmin
0 Replies

9. UNIX for Advanced & Expert Users

how to exclude the GREP command from GREP

I am doing "ps -f" to see my process. but I get lines that one of it represents the ps command itself. I want to grep it out using -v flag, but than I get another process that belongs to the GREP itself : I would like to exclude # ps -f UID PID PPID C STIME TTY TIME CMD... (2 Replies)
Discussion started by: yamsin789
2 Replies

10. Shell Programming and Scripting

Grep Problem

I have a large file that I am grepping to find a certain string. grep 'C:\Data\Directory\Test.txt' test.txt It can not find it even though I know it is in there . I know that there is a problem with the backslashes but I can't get it to work. I tried grep... (2 Replies)
Discussion started by: lesstjm
2 Replies
Login or Register to Ask a Question