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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting List creation - Grep a line in a file with a script name
# 1  
Old 05-22-2014
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:
Code:
test1.ksh
test2.ksh test2.lst
test3.ksh test3.lst
test4.ksh

test2.lst:
Code:
test21.ksh
test23.ksh
test23.ksh

test3.lst:
Code:
test31.ksh
test33.ksh
test33.ksh

I need to write code to create one master list from this which will look like:
Code:
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?

Code:
#!/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:
Code:
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.

Moderator's Comments:
Mod Comment Please use code tags!

Last edited by radoulov; 05-22-2014 at 03:28 PM..
# 2  
Old 05-22-2014
If you have egrep supporting the -o option the following snippet might do it:

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

This User Gave Thanks to Aia For This Post:
# 3  
Old 05-22-2014
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?
# 4  
Old 05-22-2014
Quote:
Originally Posted by member2014
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
Code:
for f in $(egrep -o '\w*.lst' List1); do <command, ...> $f; done

# 5  
Old 05-22-2014
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
# 6  
Old 05-22-2014
I don't understand why any grep is needed here???

I also don't understand why you don't want:
Code:
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:
Code:
while read script list
do	if [ -f "$list" ]
	then	cat "$list"
	else	printf "%s\n" "$script"
	fi
done < List1

This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 05-22-2014
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?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need UNIX script to check checksum and dummy file creation.

Hi Folks, I need a UNIX script which will copy files(Table wise) from source directory to destination directory (Under table directory) and also creates 2 additional files after getting copied to destination directory with extension .pdy and .ldy , . pdy file will be zero byte file should get... (4 Replies)
Discussion started by: Nicks1412
4 Replies

2. Shell Programming and Scripting

Script to print file name and its creation date

Hello , I am looking for a script to print file name and its last updated time. FILE CREATION-TIME FILE-NAME 24/10/2017 12:34 TDR-IU-8-2017.10.24.07:40:00-2017.10.24.07:45:00 when we run l command it print the directory and the files with details like permission,... (1 Reply)
Discussion started by: sadique.manzar
1 Replies

3. Shell Programming and Scripting

CSV File Creation Within Shell Script

Hi All, I am trying to create a CSV file within a shell script test.ksh and the code snippet is something like below: #!/usr/bin/ksh # Set required variables. . $HOME/.prof # Output file path Group1=/tmp/G1.csv Group2=/tmp/G2.csv Group3=/tmp/G3.csv $ORACLE_HOME/bin/sqlplus -s... (2 Replies)
Discussion started by: swasid
2 Replies

4. Shell Programming and Scripting

File system creation script on AIX 6.1 using while loop

#!/bin/sh echo "VG: " read VG echo "LP: " read LP echo "SAP: " read SAP echo "NUM: " read NUM echo "SID: " read SID while ]; read VG LP SAP NUM SID ; do mklv -y $SAP$NUM -t jfs2 -e x $VG $LP; crfs -v jfs2 -d /dev/$SAP$NUM -m /oracle/$SID/$SAP$NUM ... (14 Replies)
Discussion started by: arorap
14 Replies

5. Shell Programming and Scripting

Grep by range of date from file creation in directory

Hi Expert, Need your scripting and finding data so that it help me to find the culprit of this memory usage error. Data provided here is a sample. Process Snapshot directory: /var/spool/processes-snapshot webdev9o9% pwd /var/spool/processes-snapshot webdev9o9% ls -lrct -rw-r--r-- ... (3 Replies)
Discussion started by: regmaster
3 Replies

6. Shell Programming and Scripting

Avoid file creation in a script...achive same result

Guys following lines help me in getting numbers from PID column ,to be thrown into first column of a CSV file. COLUMNS=2047 /usr/bin/ps -eo pid,ppid,uid,user,args | grep -v "PID" > /tmp/masterPID.txt cat /tmp/masterPID.txt|while read line do PID=`echo $line|awk '{print $1}'` echo "$PID"... (4 Replies)
Discussion started by: ak835
4 Replies

7. Shell Programming and Scripting

how to grep a file in list

hi all, for an example: In a file out.txt contains: /export/home/raghu/bin/debug_serverLog_apache_20090626_0625.txt How to grep or cut the value as "debug_serverLog_apache_20090626_0625.txt" or i want only the output as "debug_serverLog_apache_20090626_0625.txt" pls advice me (3 Replies)
Discussion started by: raghur77
3 Replies

8. Shell Programming and Scripting

Creation of script,if the data file have more than one entry!!!

1.Daily there will be 14 files in the data directory 2.someday's the 14 files receive more than once r twice with different time stamps....we need to chk the count of the file and if the count of the file is two.we need to combine the both the files. 3. if any duplicate data is there just... (1 Reply)
Discussion started by: bobprabhu
1 Replies

9. Shell Programming and Scripting

Creation of output file from list of file

Hi Guru's, Eventhough I know basic shell scripting, Iam not an expert. Can any one help me to get a logic/answer for the below requirement: I've to create an output file "outputfile.txt" from many datafiles (ex: abc.dat, xyz.dat). Header record layout for "outputfile.txt" should be... (7 Replies)
Discussion started by: ganapati
7 Replies

10. Shell Programming and Scripting

Creation and Transfer of TAR file from one machine to another Using UNIX script

Hi, I want to create unix script such that it should run on machine A, it should run TAR commands on machine B and copy that TAR to machine C. Is it possible? Thanks Rahul (2 Replies)
Discussion started by: rahuljadhav
2 Replies
Login or Register to Ask a Question