Looping through input/output


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Looping through input/output
# 8  
Old 08-20-2016
I apologize for guessing wrong at what combining.executable does. I made the unwarranted assumption that a file named infile1 would be your 1st input file and that a file named infile2 would be your second input file instead of infile1 being the name of your 2nd input file and infile2 being the name of your 1st input file.

With that list of files and no indication of what produced it, I'll make another wild assumption that the list is the first couple of lines of output from the command:
Code:
ls *header

and that the intent is that your input files are to be processed in increasing alphanumeric sorted order. Assuming that is correct (which based on the failure of my earlier assumptions is certainly not guaranteed), the following will combine your input files and produce an output file named outfile. It uses temporary files named 0 and 1 and renames the last used temporary file to be outfile and removes the other temporary file before it exits:
Code:
#!/bin/ksh
i=0
last=0
for file in *header
do	case "$i" in
	(0)	f1=$file
		i=1
		;;
	(1)	combining.executable "$file" "$f1" > $((out = last))
		i=2
		;;
	(2)	out=$((1 - last))
		combining.executable "$file" $last > $out
		last=$out
		;;
	esac
done
mv $out "outfile"
rm -f $((1 - out))

Although written and tested using a Korn shell, this will work with any shell that uses basic Bourne shell syntax and supports POSIX arithmetic substitutions (such as ash, bash, dash, and ksh; but not csh and its derivatives, and not a traditional Bourne shell).
# 9  
Old 08-21-2016
Hi and thank you for your solution.
I will test it out tomorrow on my files.

Many thanks for your kind help!

---------- Post updated at 12:53 AM ---------- Previous update was at 12:33 AM ----------

Hi,

I actually just tried out your script now :-)

The combining.executable complines that the argument 2 is not specified. It creates files 0 and 1, but they are empty.

Your assumption is correct that the files can be taken in increasing alphanumeric sorted order. Your other assumption that the list of files was generated with command ls is also correct.

Many thanks for your help in advance.

---------- Post updated at 02:19 AM ---------- Previous update was at 12:53 AM ----------

p.s. there is also this:

/bin/ksh: bad interpreter: No such file or directory
# 10  
Old 08-21-2016
Quote:
Originally Posted by zajtat
Hi and thank you for your solution.
I will test it out tomorrow on my files.

Many thanks for your kind help!

---------- Post updated at 12:53 AM ---------- Previous update was at 12:33 AM ----------

Hi,

I actually just tried out your script now :-)

The combining.executable complines that the argument 2 is not specified. It creates files 0 and 1, but they are empty.

Your assumption is correct that the files can be taken in increasing alphanumeric sorted order. Your other assumption that the list of files was generated with command ls is also correct.

Many thanks for your help in advance.
It sounds like you may have removed some of the <space> characters from the script I suggested???

Please show us the output from the command uname -a. We need to know what operating system (including the release number) that you are using?

What shell are you using (including version number)? If you don't know the version number, show us the output from the command:
Code:
shell --version

where shell is the name of the shell you are using.

Then run the command:
Code:
shell -xv script_name > log 2>&1

where shell is the name of your shell and script_name is the name of the file containing my script. Then show us the exact contents of the 1st 40 lines in the file named log (in CODE tags; not ICODE tags) and show us the output from the commands:
Code:
type combining.executable
printf '%s\n' "$PATH"
ls -l 0 1 log outfile
ls -l *header|head -n 20

(also in CODE tags).

----------------

I just saw your PS. If /bin/ksh isn't a valid path on your system, how did you run the script to produce files 0 and 1 and if trying to run my script produced that error, what else did you do that attempted to run combining.executable???

Please also show us the output from the commands:
Code:
ls -l "$SHELL"
type ksh

in CODE tags.
# 11  
Old 08-21-2016
Hi,

I did not alter your script, just copy pasted it into a shell and run it. When the shell produced an error of /bin/ksh: bad interpreter: No such file or directory, I deleted the first line of your code and tried it that way. Other than that, I didn't change anything.

Here are the answers to your questions:

Question1: Please show us the output from the command uname -a . We need to know what operating system (including the release number) that you are using?


Answer:

Code:
Linux hw-uger-1000 2.6.32-642.el6.x86_64 #1 SMP Wed Apr 13 00:51:26 EDT 2016 x86_64 x86_64 x86_64 GNU/Linux

Question 2: What shell are you using (including version number)?

Answer:

Code:
GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>[/ICODE]

Question 3: show the first 40 lines of output from this command [ICODE]shell -xv script_name > log 2>&1



Answer:

Code:
module () {  eval `/usr/bin/modulecmd bash $*`
}
#!/bin/ksh
i=0
+ i=0
last=0
+ last=0
for file in *header
do	case "$i" in
	(0)	f1=$file
		i=1
		;;
	(1)	./match.pl -f "$file" -g "$f1" -k 1 -l 1 -v "5 6 7" > $((out = last))
		i=2
		;;
	(2)	out=$((1 - last))
		./match.pl -f "$file" -g $last -k 1 -l 1 -v "5 6 7" > $out
		last=$out
		;;
	esac
done
+ for file in '*header'
+ case "$i" in
+ f1=9477885102_R01C01header
+ i=1
+ for file in '*header'
+ case "$i" in
+ ./match.pl -f 9477885102_R01C02header -g 9477885102_R01C01header -k 1 -l 1 -v '5 6 7'
+ i=2
+ for file in '*header'
+ case "$i" in
+ out=1
+ ./match.pl -f 9477885102_R02C01header -g 0 -k 1 -l 1 -v '5 6 7'
Must specify file one using -g switch
+ last=1
+ for file in '*header'
+ case "$i" in
+ out=0
+ ./match.pl -f 9477885102_R02C02header -g 1 -k 1 -l 1 -v '5 6 7'
+ last=0


I'm sorry,but I'm not able to share the answers to the following commands because they would contain sensitive information.

Code:
type combining.executable
printf '%s\n' "$PATH"
ls -l 0 1 log outfile
ls -l *header|head -n 20

Question 4: the output form the following commands:

Please also show us the output from the commands:

Code:
ls -l "$SHELL"

answer:
Code:
-rwxr-xr-x 1 root root 941880 Dec 22  2015 /bin/bash*


Code:
type ksh

answer:
Code:
-bash: type: ksh: not found

Many thanks!

Last edited by zajtat; 08-21-2016 at 04:13 AM..
# 12  
Old 08-21-2016
Quote:
Originally Posted by zajtat
Please also show us the output from the commands:

Code:
ls -l "$SHELL"

answer:
Code:
-rwxr-xr-x 1 root root 941880 Dec 22  2015 /bin/bash*


Code:
type ksh

answer:
Code:
-bash: type: ksh: not found

In this case do what Don Cragun already (implicitly) suggested in post #8:

change the line in his script where the shell to be used to execute it is named:

Code:
#! /bin/ksh

and replace it with a line pointing to a shell valid on your system, namely (from your quoted output above)

Code:
#! /bin/bash

Further, to spare you one source of common problems with this line (which is, informally, also called a "shebang"): the line needs to be the very first line in the file and it must not be indented! Even a leading space would make it a normal comment (instead of this special one which tells the OS which shell executable to use to have it interpreted) without any further meaning.

I hope this helps.

bakunin
# 13  
Old 08-21-2016
Hi Bakunin and thank you for your suggestion/explanation.

I've changed the first line to

#! /bin/bash

Unfortunately, I'm getting the same error that there is no file specified as argument2.

I apologise for misunderstanding things or not explaining them clearly. I'll try to do better.

Many thanks!
# 14  
Old 08-21-2016
Quote:
Originally Posted by zajtat
Hi Bakunin and thank you for your suggestion/explanation.

I've changed the first line to

#! /bin/bash

Unfortunately, I'm getting the same error that there is no file specified as argument2.

I apologise for misunderstanding things or not explaining them clearly. I'll try to do better.

Many thanks!
I think Don's script has found a bug in your, what looks like a, perl script; wherever that came from!?

I could be wrong but it looks as though your script cannot differentiate between 0, (zero) as a number and 0, (zero) as a file...

-g 0

Did you test your _perl_ script with a file named "0"?

Just a thought, as Don's script looks as though it generates a file name "0".
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Looping over output of 'ls'

Hi, I have some output from 'ls' command and I want to loop over the output in a bash script. What would be a good way to go about it? For example, if the output of the ls command gives me 'prefix1 prefix2 prefix3', how can I set a loop that will iterate over these? many thanks! (5 Replies)
Discussion started by: pc2001
5 Replies

2. UNIX for Dummies Questions & Answers

Looping through the contents of array for output name

Hi all, I am trying to loop through the string contents of an array, to add it during the saving of the output files. I am trying this code to print each column and save it to unique file name, but it doesn't work. Thanks for any help. fnam=(japan usa uk) alldata.dat contained sample data... (1 Reply)
Discussion started by: ida1215
1 Replies

3. Shell Programming and Scripting

looping and saving output of each line separately

I have been trying this program for a long time. I am trying to read a file named "odon" line by line; read the first line, send it to do a command saved in a file "perm", once the first line has finished going through the content of the file perm, the result is saved with the number of the line.... (17 Replies)
Discussion started by: iconig
17 Replies

4. Shell Programming and Scripting

Looping through for user input

Legends, I want to remain in the script until user passes the correct name. I had tried the below code; but it didn't work out. Please help echo "\nPlease enter the source system: \c" while read SYSTEM_NAME do if ]; then echo "\nMaking $SYSTEM_NAME as source system for particular... (5 Replies)
Discussion started by: sdosanjh
5 Replies

5. Solaris

SVM Solaris 8 Problem. Metastat output looping

Hi friends, I'm newbie to SVM. Just wanna try installed it on one of our server (to do mirroring for disk0 and disk1) but i think im lost until now. :( the steps i've taken is as below:- 1.prtvtoc /dev/rdsk/c1t0d0s2 | fmthard -s - /dev/rdsk/c1t1d0s2 2.metadb -a -c 3 -f c1t0d0s7... (3 Replies)
Discussion started by: kronenose
3 Replies

6. Shell Programming and Scripting

Dynamic output file generation using a input text file with predefined output format

Hi, I have two files , one file with data file with attributes that need to be sent to another file to generate a predefined format. Example: File.txt AP|{SSHA}VEEg42CNCghUnGhCVg== APVG3|{SSHA}XK|"password" AP3|{SSHA}XK|"This is test" .... etc --------- test.sh has... (1 Reply)
Discussion started by: hudson03051nh
1 Replies

7. Shell Programming and Scripting

perl: looping through the output of a 'system' command

Hi there could anybody point me in the right direction when it comes to looping through the output of a system command in perl (i.e. df -k) doing a test against each line to see if it matches? for example if i have a df -k output like this and I wanted to grab the lines that matched "sda" or... (3 Replies)
Discussion started by: rethink
3 Replies

8. Shell Programming and Scripting

input -output file

Hi, I am having an Input file .which is having a list of names. comapring with our database , needs to write the out put in file called output.txt , format should be name--> country--->phone number could you please help me.. thanks in advance (7 Replies)
Discussion started by: hegdeshashi
7 Replies

9. UNIX for Advanced & Expert Users

input/Output settings

How can we view all the input/output settings of unix environment for a session (6 Replies)
Discussion started by: paritoshc
6 Replies

10. Shell Programming and Scripting

Using Output from one command as input to another

This site has been very helpful thus far.. I thank you all in advance for sharing the knowledge. Let me get to it. I am trying to write a very small script to take away from the boredom of doing the same thing over and over. Everynow and again I have to get the hex value of a file using a... (2 Replies)
Discussion started by: BkontheShell718
2 Replies
Login or Register to Ask a Question