Re: help for script (232 Views)
Reply
Frequent Advisor
Karthick K S
Posts: 85
Registered: ‎08-16-2005
Message 1 of 10 (232 Views)

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
Respected Contributor
Orhan Biyiklioglu
Posts: 153
Registered: ‎09-12-2005
Message 2 of 10 (232 Views)

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
Frequent Advisor
Karthick K S
Posts: 85
Registered: ‎08-16-2005
Message 3 of 10 (232 Views)

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"

Honored Contributor
Bharat Katkar
Posts: 2,309
Registered: ‎05-29-2003
Message 4 of 10 (232 Views)

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
Respected Contributor
Orhan Biyiklioglu
Posts: 153
Registered: ‎09-12-2005
Message 5 of 10 (232 Views)

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
Frequent Advisor
Karthick K S
Posts: 85
Registered: ‎08-16-2005
Message 6 of 10 (232 Views)

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
Respected Contributor
Orhan Biyiklioglu
Posts: 153
Registered: ‎09-12-2005
Message 7 of 10 (232 Views)

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
Honored Contributor
Eric SAUBIGNAC
Posts: 947
Registered: ‎01-10-2001
Message 8 of 10 (232 Views)

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;-)
Frequent Advisor
Karthick K S
Posts: 85
Registered: ‎08-16-2005
Message 9 of 10 (232 Views)

Re: help for script

Thanks all given a solution
Honored Contributor
Eric SAUBIGNAC
Posts: 947
Registered: ‎01-10-2001
Message 10 of 10 (232 Views)

Re: help for script

Thanks for all points you have given ;-(
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.