Faster than nested while read loops?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Faster than nested while read loops?
# 1  
Old 08-09-2011
Faster than nested while read loops?

Hi experts,
I just want to know if there is a better solution to my nested while read loops below:

Code:
 
while read line; do
    while read line2; do
        while read line3; do
            echo "$line $line2 $line3"
        done < file3.txt
    done < file2.txt
done < file1.txt > output.txt

file1.txt:
1a
1b

file2.txt:
2a
2b

file3.txt:
3a
3b

output.txt:
1a 2a 3a
1a 2a 3b
1a 2b 3a
1a 2b 3b
1b 2a 3a
1b 2a 3b
1b 2b 3a
1b 2b 3b

I am thinking that a perl or an awk script could do this, which are much faster than while loops.

Thanks!
# 2  
Old 08-09-2011
Why not use the join command or maybe paste?

Code:
paste file1 file2 file3

# 3  
Old 08-09-2011
paste doesn't sound like what he wants as that just combines individual lines.

I don't think perl or awk would necessarily be faster as they are interpreted languages just like shell.
# 4  
Old 08-09-2011
Quote:
Originally Posted by chstr_14
I am thinking that a perl or an awk script could do this, which are much faster than while loops.
The while loops are not your problem; it's the repeated reading of the same file over and over again (file3 = L1*L2, file2 = L1, file1 = 1). Even if the entire file's contents are stored in the operating system's cache, while reading will be much faster than hitting the disk, a switch to kernel mode is still required (this is expensive compared to accessing process memory).

The easiest way to speed this up would be to store each file's lines in an array. While it's not necessary to use perl or awk for that purpose, since modern shells support arrays, it's a typical approach.

Quote:
Originally Posted by jim mcnamara
Why not use the join command or maybe paste?
join(1) may not be appropriate either, as it requires both a common field (not present in the sample data) and pre-sorted data (which the sample data is, but the real data may not be).

Regards,
Alister
# 5  
Old 08-09-2011
The fastest way, arguably, to read a file is process substitutuion in bash, ksh, or zsh.

Code:
a=$(< filename)
b=$(< filename2)

But based on his example and output it looks like he is pasting.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Nested Loops for text file

Hi A text file containing data something likeVehicle: BMW Class checkin_note: Tyre : Four path_id : 11 vault_file_size: -1 Vehicle: Toyota Class checkin_note: Tyre : Four path_id : 11 vault_file_size: -1 Vehicle: Chevrolet Class checkin_note: Tyre : Five path_id :... (7 Replies)
Discussion started by: vipinHasija
7 Replies

2. Shell Programming and Scripting

ksh - Need Help Reducing Nested Loops

Hello, I pulled out some old code from an unfinished project the other day and wanted to stream line it better. I know anything beyond a double loop is usually bad practice, and I came up with some logic for later that would no longer require the first loop in the following code that works: ... (5 Replies)
Discussion started by: Azrael
5 Replies

3. Shell Programming and Scripting

two while nested loops

for server in $(echo `cat /tmp/ScanHosts_${USERSNAME}.TXT`) do for portnumber in $(echo `cat /tmp/ScanPorts_${USERSNAME}.TXT`) do #echo ${server} ${portnumber} ... (3 Replies)
Discussion started by: SkySmart
3 Replies

4. UNIX for Dummies Questions & Answers

Executing nested loops+foreach

It's been a while since I used csh formatting and I am having a little bit of trouble with a few things. Things seem so much easier to execute in Matlab, however I need to do this on the terminal because of the programs I am trying to interact with. So here's what I want to do: I have a file... (0 Replies)
Discussion started by: katia
0 Replies

5. Shell Programming and Scripting

Nested for loops

Greetings All, The following script attempts to enumerate all users in all groups in the group file(GROUP) and echo the following information: GROUP ---> USER The script is as follows: IFS="," for GROUP in `ypcat -k group | cut -d" " -f1` do for USER in `ypcat -k group... (13 Replies)
Discussion started by: jacksolm
13 Replies

6. Shell Programming and Scripting

KSH nested loops?

KSH isn't my strong suit but it's what my company has to offer. I've got a script with two nested loops, a FOR and UNTIL, and that works fine. When I add a CASE into the mix I end up getting "Unexpected 'done' at line xx" errors. Any suggestions on this? for divi in at ce ci cm co de di fl... (9 Replies)
Discussion started by: mrice
9 Replies

7. Shell Programming and Scripting

Need help with Regular Expressions and nested loops

Ok... am going slightly loopy trying to get this working (no pun intended) What I need is to modify this code which takes a string input then echo's each character on a seperate line, to do the same thing but to put DIGIT: in front of numbers and LETTER: in front of letters. I know a regular... (5 Replies)
Discussion started by: U_C_Dispatj
5 Replies

8. Shell Programming and Scripting

Nested while loops (ksh scripting)

You can use one while inside another? I made the following script (without really knowing if I can use two while) to get 3 numbers different from each other at random: num1=$(( $RANDOM % 10 )) num2=$num1 while do num2=$(( $RANDOM % 10 )) done num3=$num1 while do while do... (1 Reply)
Discussion started by: ale.dle
1 Replies

9. Shell Programming and Scripting

nested for loops

I need help getting over this bump on how nested for loops work in shell. Say i was comparing files in a directory in any other language my for loop would look like so for(int i=0;to then end; i++) for(int y = i+1; to the end; y++) I can't seem to understand how i can translate that... (5 Replies)
Discussion started by: taiL
5 Replies

10. Shell Programming and Scripting

Grepping within nested for loops

Good morning - I have publication lists from 34 different faculty members. I need to end up with the numbers of publications in common across all 34 faculty. I need to grep person1 (last name) in list2, person1 in list3, person1 in list 4, etc., then person2 in list3, person 2 in list4, etc.,... (2 Replies)
Discussion started by: Peggy White
2 Replies
Login or Register to Ask a Question