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
Using a shell script:
$
$ 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 -
$
$
$ 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
agama
3
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
line=`test1.tmp grep "|${v_server_nbr}$" test1.tmp `
Use awk to match line and output the fields you need:
#!/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"
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 
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
f10 is my data file and sn is a variable.
In that case, a good place to start is the manual:
man awk
Save the output to a file, print it out and refer to it at all times.
tyler_durden
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
Really good reference doc Jim.. thank u
---------- Post updated at 09:33 AM ---------- Previous update was at 09:30 AM ----------
Jim, Can you suggest me good docs for Unix. I am just beginner of Unix programming, I want to improve my skills in Unix programming.