List creation - Grep a line in a file with a script name

Hi,
I have a list file which has script names in them. Some scripts take lists as parameters which inturn have script names. This is basically for sequencing the job run.

Eg: List1:

test1.ksh
test2.ksh test2.lst
test3.ksh test3.lst
test4.ksh

test2.lst:

test21.ksh
test23.ksh
test23.ksh
 

test3.lst:

test31.ksh
test33.ksh
test33.ksh

I need to write code to create one master list from this which will look like:

test1.ksh
test21.ksh
test23.ksh
test23.ksh
test31.ksh
test33.ksh
test33.ksh
test4.ksh

I am trying to grep every line in List1, if it contains .lst get the list file name and echo the contents to create the master list.
It gives me an error as the line contains a script name and the script is not in the directory.
Is there a wat I can grep a line in a file which contains a script name?

#!/bin/ksh
while read line
do
   echo $line
   sublst_name1=`echo $line | cut -d " " -f2`
   echo $sublst_name1
   lst_cnt=$(grep -i .lst $sublst_name1 | wc -l)
   echo $lst_cnt
done <"$lst_file"

output:

test1.ksh
test1.ksh
grep: can't open test1.ksh
0
test2.ksh test2.lst 
test2.lst
2
test3.ksh test3.lst 
test3.lst
2
test4.ksh
test4.ksh
grep: can't open test4.ksh
0

Thanks in advance for the help.

If you have egrep supporting the -o option the following snippet might do it:

egrep -o '\w*.lst' List1 | xargs cat >> big.list
1 Like

Thanks Aia. egrep is giving me the lines in the List1 which have .lst
I need to get the specific .lst names and read its contents to the output file as well.
Which is why I am looping through the lines and grep in every line separately.

Any sugesstions?

I guess I am missing some understanding, because I think that's what piping to xargs cat >> big.list does.

Regardless, if you what to loop them

for f in $(egrep -o '\w*.lst' List1); do <command, ...> $f; done

I get the below result when I run the for loop:
test2.ksh
test2.lst
test3.ksh
test3.lst

I want to be able to grep a pattern on a single line with spaces like below. Is there a way?
grep .lst "test.ksh test.lst" |wc -l

I don't understand why any grep is needed here???

I also don't understand why you don't want:

test2.ksh
test3.ksh

in your output file in addition to what you have said you want?

But, to get the output you said you want from the input files you have described, try:

while read script list
do	if [ -f "$list" ]
	then	cat "$list"
	else	printf "%s\n" "$script"
	fi
done < List1
1 Like

test2.ksh and test3.ksh read the .lst file and execute the scripts in the list. They are just like wrappers. The result list here needs to be just the actual scripts that execute.
The List1 is the main script which executes all scripts under it. When the execution comes to the line test3.ksh test3.lst, it call test3.ksh with test3.lst as the parameter. All that the script does is it executes the list of scripts under the test3.lst.
So when I create the master list I dont need the entry test3.ksh test3.lst and instead I need the scripts list inside the test3.lst.

For this I am looping through the List1, when the script finds a line with .lst, it replaces that line with the contents (script list) of the .lst file. Does this make sense?

Yes, it makes sense. And the script I gave you does exactly that.