1752401 Members
5898 Online
108788 Solutions
New Discussion юеВ

Re: help for script

 
Karthick K S
Frequent Advisor

help for script

i have one file ex.given below

id Output Description
abc abc123 xxxx
xyz dbname xxx
123 dbname1 xxx

for given above i want to write scritp to take output correspondign id
without space
if abc identified the abc123 output goes to some variablt, i tried but
its output is coming with spaces
9 REPLIES 9
Orhan Biyiklioglu
Respected Contributor

Re: help for script

Hi, could you explain how did you try to get the output.

awk '$1=="123"{print $2}' your_file_name

will print "dbname1" without any spaces.

if you would like to assign this to a shell variable than something like

myvar=`awk '$1=="123"{print $2}' your_file_name`

will do the job.

hth
Karthick K S
Frequent Advisor

Re: help for script

export $rid=abc
export FILE=/abc/ho/

grep $rid ${FILE}/recv.txt | cut -d "=" -f2

if i use this above one output is coming but with spacec For eg:

output
" dbname"

Bharat Katkar
Honored Contributor

Re: help for script

Hi,
It worked for me in this way, no spaces in the output:

(root@server1)[/] export rid=abc
(root@server1)[/] cat xyz
abc abc123 xxxx
xyz dbname xxx
123 dbname1 xxx
(root@server1)[/] export FILE=/
(root@server1)[/] grep $rid ${FILE}xyz | cut -d " " -f2
abc123
(root@server1)[/]

Hope that helps.
Regards,
You need to know a lot to actually know how little you know
Orhan Biyiklioglu
Respected Contributor

Re: help for script

-d option of the cut command specifies the field delimitter. Why do you use cut -d "=" since there is no "=" in your file.

export $rid=abc
export FILE=/abc/ho/

grep $rid ${FILE}/recv.txt | cut -d " " -f2

should work.

You can also use the awk command instead of the grep and cut commands (only one command invocation)

awk -v rid=$rid '$1==rid{print $2}' ${FILE}/recv.txt
Karthick K S
Frequent Advisor

Re: help for script

if i am using cut spaces is coming output
my o/p should be
myvar is output of cut command

/dir/$myvar
Orhan Biyiklioglu
Respected Contributor

Re: help for script

Have you tried

export $rid=abc
export FILE=/abc/ho/

this:

grep $rid ${FILE}/recv.txt | cut -d " " -f2

or this:

awk -v rid=$rid '$1==rid{print $2}' ${FILE}/recv.txt
Eric SAUBIGNAC
Honored Contributor

Re: help for script

Hi Karthick,

Be aware that if in your file you have any string containing "abc", grep will return every line containing "abc", independently of the place where is this chaine in the line.

awk is a good tool to do that, but i find it heavy to use. So i do prefer this kind of approach :

ID="The string to search"
FILE="File to scan"

OUTPUT=
egrep "^$ID +" $FILE | read nop OUTPUT nop
if [ -z "$OUTPUT" ]
then echo "ID $ID not found"
else echo "ID $ID found with OUTPUT $OUTPUT"
fi

When U use egrep (enhanced grep) "^" means beginning of line and " +" means one or more spaces. In this manner U are sure to find $ID in the first field, not $ID somewhere on the line.

Hope this will help

Eric

(PBFWME;-)
Karthick K S
Frequent Advisor

Re: help for script

Thanks all given a solution
Eric SAUBIGNAC
Honored Contributor

Re: help for script

Thanks for all points you have given ;-(