Parsing dynamic data from a command output

Hi people,
I am writing a korn shell script, and one of the command gives an output something like below:

release.label.2010.03.02
objects: 
/project/path/to/some/file_name.ksh
/project/path/another/file_name01.dat

I have to retrieve the file paths one by one & use them as input for another command. any suggestions?

Would appreciate if someone can post a string parsing code for this.

So you have something like this:

$ 
$ mycommand
release.label.2010.03.02
objects:
/project/path/to/some/file_name.ksh
/project/path/another/file_name01.dat
$ 
$ 
$ 
$ mycommand | grep -E "(ksh|dat)$"
/project/path/to/some/file_name.ksh
/project/path/another/file_name01.dat
$ 
$
$ 
$ 
$ mycommand | grep -E "(ksh|dat)$" | myothercommand
I received /project/path/to/some/file_name.ksh; I am now going to open this file...
I received /project/path/another/file_name01.dat; I am now going to open this file...
$ 
$ 

tyler_durden

Hi durden,
The output files cud be anything like .mp, .ksh, .dml. What I was thinking is I will put the output of the first command to a variable. so everything is in a string format now. So i need a logic now where I can retrieve the file paths & input them one by one in the 2nd command.

is there a way i can retrieve substring which starts with a char "/" & ending char the one before a space. i hope u getting what m trying to say. in that way i can looking for another "/" & retrieve the path

Why dont you just redirect the o/p of the first command in a temp file, and then filter the ones and used as an i/p for the next command?

yes I can do that as well, but whats the logic of filtering? cause All i want from that is the file paths. but it also displays other information like the release label & objects:

We need to store the output of the first command to a temp file. filtering in the sense from the temp file we are going to extract the file names. We can extract it by using the regular expressions. And then we are going to pass it as an input for the second command. What is the issue in this?

the issue is m a beginner to shell scripting. dont know the exact commands. would appreciate if some one cud post a script for it

In a simplified format...can be optimized..

Assuming you have redirected the o/p of the first command to a file name "file".

#!/bin/sh

while read line
do
first=$( echo $line | cut -c 1 )
if [ $first == "/" ]; then
  echo $line
fi
done < file

cheers,
Devaraj Takhellambam

consider that you are redirected your output of the first command to a file called output1

So the content of the output1 file will be as follows

release.label.2010.03.02
objects:
/project/path/to/some/file_name.ksh
/project/path/another/file_name01.dat

To extract the files from that you can use the following commands

cat test | grep "^/.*\.*" test

So now it will extract the file names from that output and will give the following as the result

/project/path/to/some/file_name.ksh
/project/path/another/file_name01.dat

Now you can pass the above result to the third command using pipe

i was trying that but was told not to create any files. So now i put the output in a variable.
I tried this code using Devtakh's idea & it worked:

for v in ${var1}
do
   first=$( echo $v | cut -c 1 )
   if [ $first == "/" ]; then
        mycommand $v
   fi
done

Thanks people. :slight_smile: