Operating System - HP-UX
1752633 Members
5556 Online
108788 Solutions
New Discussion

Re: ksh help: to extract 2 words from every line in a file

 
SOLVED
Go to solution
Hanry Zhou
Super Advisor

ksh help: to extract 2 words from every line in a file

I have a lot of lines in a file, and all with following format:

 

host_name:/vol/v1/v2

 

I only wanted to extract "host_name" and "v1" from every line in the file, then put these two words into a output file "out_file".

 

then create a for loop to process these two words in "out_file".

 

can anybody please help me out, it'd be appreciated very much

 

Thank you!

none
10 REPLIES 10
Steven Schweda
Honored Contributor

Re: ksh help: to extract 2 words from every line in a file

 
Dennis Handly
Acclaimed Contributor

Re: ksh help: to extract 2 words from every line in a file

You could do:

IFS_save=$IFS # save
IFS=":/"
while read name dummy vol v1 v2; do
   echo $name $v1
done < in_file > out_file
IFS=$IFS_IFS   # restore

 

>then create a for loop to process these two words in "out_file".

 

You could process these in the above while.  Or you can create another while read with just the two fields.

No need to fiddle with IFS for this second while.

Hanry Zhou
Super Advisor

Re: ksh help: to extract 2 words from every line in a file

Thank you all for the response. I have one follow-up.

 

while read filer path; do
/bin/scripx.sh -f $filer -v $path -a
done < ./output1

 

with this script, why in the end only one instance of /bin/scriptx.sh got excuted? I wanted to run same number of times as the number of lines in "output1" file.

 

Thank you again!

none
Steven Schweda
Honored Contributor

Re: ksh help: to extract 2 words from every line in a file

 
Hanry Zhou
Super Advisor

Re: ksh help: to extract 2 words from every line in a file

First off, no, I did not put scriptx.sh under /bin, it is just an example. it is actually located somewhere else. the following script results in the same.

$ cat hv1.sh
#!/bin/sh

exec <./output1
while read filer path; do
./hv2.sh -f $filer -v $path -a
done

The cause of it is because that in-house script scriptx.sh (or ./hv2.sh in your case) takes quite some time to finish. I even put sleep 60 in the loop. It is still the same. I used exec < ./output1, it doesn't help.

 

output1 file is consists of multiple lines which contains filer-n and path-n, as you guessed.

 

 

none
Steven Schweda
Honored Contributor

Re: ksh help: to extract 2 words from every line in a file

 
Hanry Zhou
Super Advisor

Re: ksh help: to extract 2 words from every line in a file

#cat run_find

exec <./output1
while read filer path; do
/example/find_details.pl -f $filer -v $path -a
sleep 60
done

 

#cat output1

filer1 path1

filer2 path2

filer3 path3

...

filer180 path180

 

sorry for the confusion. Let me try it again.

 

The script "find_details.pl" is the perl script and it is to grab some volume information from about 180 NAS storages. It takes about > 1 mins to complete.

 

The issue is the script "run_find" only execute the "find_details.pl" on the first line of "output1", then exit out.

 

Hope I made myself clear this time. thank you very much for your patience.

none
Dennis Handly
Acclaimed Contributor
Solution

Re: ksh help: to extract 2 words from every line in a file

.../scripx.sh -f $filer -v $path -a
>why in the end only one instance of .../scriptx.sh got executed?

 

If your evil scriptx is also reading stdin, you'll lose input.  You can fix this by:

.../scripx.sh -f $filer -v $path -a < /dev/null

 

Typically examples of this failing is use of telnet, ssh, remsh (without -n), etc.  Or scummy C shell scripts.

http://h30499.www3.hp.com/t5/Languages-and-Scripting/telnet-script-while-read/m-p/5824797

Hanry Zhou
Super Advisor

Re: ksh help: to extract 2 words from every line in a file

This is the answer I am looking for. Thank you !

none