Need help with some iterative file processing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help with some iterative file processing
# 1  
Old 09-23-2008
Need help with some iterative file processing

Gurus - Please help with this urgent situation.

I have the following problem to solve using a shell script:

1. There are about 13 files named CONTAINER_1.lst, CONTAINER_2.lst, CONTAINER_3.lst .....CONTAINER_13.lst
2. Each of these files contain about 8 lines (in most cases) containing container instance names like CONTAINER_X_InstanceY where X will correspond to the container in question (ranging from 1 thru 13) and Y is some random variable value
3. For each of the 13 containers, there is a file named, CONTAINER_X_commands.sh where X corresponds to the container number. Each of the CONTAINER_X_commands.sh file contains about 3 to 5 commands with a fixed embedded string called "HELLOWORLD"
4. For each container name, I would like to first read all the CONTAINER_X_InstanceY variables (one at a time), then repeat each of command in the CONTAINER_X_commands.sh file, N number of times where N is the number of lines in the CONTAINER_X.lst (equal to the number of container instances) Once the yanking/creating multiple copies of commands is done, I would like to replace each occurrence of the "HELLOWORD" string with CONTAINER_X_InstanceY container instance name variable

Here is the code I have written so far :
for container_list in `ls -1 CONTAINER*.lst | sort -u`
do
container_name=`echo $container_list | sed 's;\(.*\)\..*;\1;'`
cmd_suffix=_commands.sh
command_file=$container_name$cmd_suffix
for container_instance_name in `cat $container_list`
do
for command in `cat $command_file`
do
echo $command | sed 's/HELLOWORLD/$container_instance_name/g;' >> /tmp/all_commands_in_singlefile.lst
done
done
done

There are 3 problems in my output :
1. It's printing each word in the commands.sh file on separate line. I would like to keep each command intact on it's own line without breaking each command word on separate line (I think this is an issue with my innermost for loop)
2. the sed is actually replacing HELLOWORLD with the literal string "$container_instnace_name" instead of using the actual variable value
3. I am also suspecting that the output file is containing lesser number of rows of commands, than I would actually anticipate. But this is yet to be substantiated because of newlines introduced in my output.

Any help will be greatly appreciated!
indi
# 2  
Old 09-23-2008
Gurus,

First of all apologies for my verbose post.

I think I have solved issue# 1 and issue# 3 by changing the code in following manner :

#/bin/sh

for container_list in `ls -1 CONTAINER*.lst | sort -u`
do
container_name=`echo $container_list | sed 's;\(.*\)\..*;\1;'`
cmd_suffix=_commands.sh
command_file=$container_name$cmd_suffix
for container_instance_name in `cat $container_list`
do
cat $command_file | while read line
do
cmd_line=`echo $line | sed 's/HELLOWORLD/$container_instance_name/g;'`
`echo $cmd_line >> /tmp/all_commands_in_singlefile.sh
done
done
done
##

However my issue# 2 is still open. sed is still replacing HELLOWORLD with the literal string "$container_instnace_name" rather than the contents of the variable. Do I have to use some escape sequence to let sed know that it's supposed to use the value of the variable as against using the liternal string? It's kinda urgent, so any help will be really appreciated!

thanks,
indi

Last edited by inditopgun; 09-23-2008 at 02:58 AM.. Reason: typos
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

awk - Rename output file, after processing, same as input file

I have one input file ABC.txt and one output DEF.txt. After the ABC is processed and created output, I want to rename ABC.txt to ABC.orig and DEF to ABC.txt. Currently when I am doing this, it does not process the input file as it cannot read and write to the same file. How can I achieve this? ... (12 Replies)
Discussion started by: High-T
12 Replies

2. Programming

awk processing / Shell Script Processing to remove columns text file

Hello, I extracted a list of files in a directory with the command ls . However this is not my computer, so the ls functionality has been revamped so that it gives the filesizes in front like this : This is the output of ls command : I stored the output in a file filelist 1.1M... (5 Replies)
Discussion started by: ajayram
5 Replies

3. Shell Programming and Scripting

Recursive file processing from a path and printing output in a file

Hi All, The script below read the path and searches for the directories/subdirectories and for the files. If files are found in the sub directories then read the content of the all files and put the content in csv(comma delimted) format and the call the write to xml function to write the std... (1 Reply)
Discussion started by: Optimus81
1 Replies

4. Shell Programming and Scripting

iterative parsing

I have always struggled when parsing a file vertically vs. by line horizontally. Can't seem to get my head around the concept. Here again I need to convert vertical output to horizontal output. original output root@acuransx:bpplsched 2000-STAND3 -v -M acuransx -l <2>bpplsched: INITIATING:... (4 Replies)
Discussion started by: jouuu
4 Replies

5. Shell Programming and Scripting

How to make parallel processing rather than serial processing ??

Hello everybody, I have a little problem with one of my program. I made a plugin for collectd (a stats collector for my servers) but I have a problem to make it run in parallel. My program gathers stats from logs, so it needs to run in background waiting for any new lines added in the log... (0 Replies)
Discussion started by: Samb95
0 Replies

6. Shell Programming and Scripting

how to change the current file processing to some other random file in awk ?

Hello, say suppose i am processing an file emp.dat the field of which are deptno empno empname etc now say suppose i want to change the file to emp.lst then how can i do it? Here i what i attempted but in vain BEGIN{ system("sort emp.dat > emp.lst") FILENAME="emp.lst" } { print... (2 Replies)
Discussion started by: salman4u
2 Replies

7. Shell Programming and Scripting

Iterative statement to cut values from a line

Hi I am new to shell scripting and trying to get values from a text file, I have a text file with values seperated with "|". like aga|120220090525|120220090525|120220090525|120220090530 bab|120220090530|120220090530|120220090535|120220090535|120220090535... (4 Replies)
Discussion started by: mannepalli
4 Replies

8. Shell Programming and Scripting

Iterative operation

grep -o '\{1,3\}\.\{1,3\}\.\{1,3\}\.\{1,3\}' then how do i iterate the file names?? (19 Replies)
Discussion started by: ravis83
19 Replies

9. Shell Programming and Scripting

Have a shell script check for a file to exist before processing another file

I have a shell script that runs all the time looking for a certain type of file and then it processes the file through a series of other scripts. The script is watching a directory that has files uploaded to it via SFTP. It already checks the size of the file to make sure that it is not still... (3 Replies)
Discussion started by: heprox
3 Replies
Login or Register to Ask a Question