LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Display loop (https://www.linuxquestions.org/questions/programming-9/display-loop-606088/)

craigjward 12-11-2007 12:57 PM

Display loop
 
I have a series of directories, with one file in each. The directories are numbered from 1-9. Each file has text which I wish to read and echo on to the display, however I want each of the files to be displayed in a particular pattern (as shown below):


111111111111111122222222222222223333333333333333
111111111111111122222222222222223333333333333333
111111111111111122222222222222223333333333333333
111111111111111122222222222222223333333333333333
111111111111111122222222222222223333333333333333
444444444444444455555555555555556666666666666666
444444444444444455555555555555556666666666666666
444444444444444455555555555555556666666666666666
444444444444444455555555555555556666666666666666
444444444444444455555555555555556666666666666666
777777777777777788888888888888889999999999999999
777777777777777788888888888888889999999999999999
777777777777777788888888888888889999999999999999
777777777777777788888888888888889999999999999999
777777777777777788888888888888889999999999999999

unfortunatley, in my script, i am getting no output at all, or an unexpected characters error. I am pretty sure that the error is in my syntax somewhere when using the sed command. Here is the display loop I am using:

Code:

I=1
for I in 1 4 7
do
K=0
L=1
        for J in 0 1 2
        do
                K=$(($I + $J))
                sed -n '${L}p;${L}q' ./$J/*
                L=$(($L + 1))       
                       
        done
        echo
done

if someone can show me the error in my ways, it would be greatly appreciated.

Thanks a bunch
Craig J. Ward

PAix 12-11-2007 02:09 PM

Code:

~/1/1  1111111111111111
~/2/2  2222222222222222
~/3/3  3333333333333333
~/4/4  4444444444444444
~/5/5  5555555555555555
~/6/6  6666666666666666
~/7/7  7777777777777777
~/8/8  8888888888888888
~/9/9  9999999999999999

From what you have said, I am assuming that you are currently in the directory ~/. Can you confirm that the directory structure, filename and file-content is as shown above please?
There is little point in me setting out to catch a haggis if I'm unsure quite what it looks like!
Oh and must you use SED for this homework(?) assignment?

I'm looking forward to your confirmation of the general geography.

forrestt 12-11-2007 02:35 PM

OK, I was seeing that as each file is just text, and he wants a certain amount of the text printed in 9 sections around the screen as laid out in the grid. The contents of the files and their names (in fact I would guess each directory could hold more than one file) shouldn't be important (if my assumption is correct).

If I'm wrong, let me know.

Forrest

craigjward 12-11-2007 03:17 PM

Yes, that is the directory structure which I am working with. No, I am not required to use sed. The content of the files and their names is not important, however, there will be only one file per directory. And the order in which I display the directories IS important

Thanks for your help

craigjward 12-11-2007 10:48 PM

other than sed, what command do you suggest I use?

ghostdog74 12-11-2007 11:15 PM

Quote:

Originally Posted by craigjward (Post 2987860)
other than sed, what command do you suggest I use?

awk
Code:

# assuming file contents only 1 line. eg file 1 has 1111111111111111 and
# file 2 has 2222222222222222 and so on
awk '
{c++}
{
    v=sprintf("%s",$0)
    s=s v
    if ( c%3==0 ) {
        for (i=1;i<=5;i++) print s
        s=""
    } 
 }' /path/*/*

output:
Code:

# ../test.sh
111111111111111122222222222222223333333333333333
111111111111111122222222222222223333333333333333
111111111111111122222222222222223333333333333333
111111111111111122222222222222223333333333333333
111111111111111122222222222222223333333333333333
444444444444444455555555555555556666666666666666
444444444444444455555555555555556666666666666666
444444444444444455555555555555556666666666666666
444444444444444455555555555555556666666666666666
444444444444444455555555555555556666666666666666
777777777777777788888888888888889999999999999999
777777777777777788888888888888889999999999999999
777777777777777788888888888888889999999999999999
777777777777777788888888888888889999999999999999
777777777777777788888888888888889999999999999999


craigjward 12-11-2007 11:22 PM

unfortunately the file's contents are more than one line (13 lines). And as much as I would like to use c++, I must use shell commands. Will this solution still work for me?

ghostdog74 12-11-2007 11:40 PM

Quote:

Originally Posted by craigjward (Post 2987884)
unfortunately the file's contents are more than one line (13 lines). And as much as I would like to use c++, I must use shell commands. Will this solution still work for me?

so what does this => 1111111111111111 mean if its not the contents of the file?

craigjward 12-11-2007 11:55 PM

I was just giving an example of how I wanted the output to be laid out. I am making a sliding puzzle game with an ascii art text file(s)

the actual output is:
Code:

                          ...I$:                                               
                          ?+OO7.                                             
                        .~OOOOZ+,.        ....=I7::...                       
                      . IOOOOOOO7I.    .,IOOOOOOOOO?..                       
                      .:ZOOOOOOOOO=  ..:ZOOOOOOOOOOO+..                       
                        IOOOOOOOOZZ~..+=OOOOOOOOOOOOOO=..                     
                      .=ZOOOOOOO?:..+ZOOOOOOOOOOZI++:,..                       
                      .IOOOOOOO7,..=ZOOOOOOOOO$+:....,~======:~,.             
                    ,,$OOOOOOZ~.,,ZOOOOOOOO??:. :++7$$ZZZZZZZ$$I+..           
                    .==ZOOOOOO7 .=IOOOOOOOZ~...~7$ZOOOOOOOOOOOOOOZ:.           
                    .??OOOOOO$.,7ZOOOOOOO:.:ZZOOOOOOOOOOOOOOOOOOOZ:.           
              :??I7?:IIOOOOOO+.~OOOOOOZZ,.~OOOOOOOOOOOOOOOOOOOOOOZ:.           
            :7OOOOO$:IIOOOOOO,.?OOOOOO~~.:OOOOOOOOOOZZOOOOOZOOOOOZ:.           
        .?I$$Z7I???:IIOOOO77.,OOOOOZ:..IOOOOOOZ?:,,,......,,::=7I,.           
        .$ZO,,. .. .77OOOO++.=OOOOO+.=~OOOOOOI,. .                           
        ,$$O7...  ..77OOOO==.?OOOOZ:.77OOOOI7.,. .                           
          ??OO?,.....77OOOO::.IOOOOI.:ZZOOOZ..~7?~...                         
          ..~OOOZZI~.::7OOO::.$OOOO=.IOOOOI.~~OOOOOO7,                         
            .~ZOOOO$7==,:IO==.$OOZ$:.7OOOO:...:?OOOOO$,                       
            ..:$$OOOOZZ$?~,,,.IOOZ$,.7OOOZ..  .:7OOOO7..                     
              ....,7OOOOOOOOOO7=,....7OOO?.      .,,?=Z+=                     
                ...,IIZOOOOOOOOOZZI=,,,~,..........,$O?=                     
                      .:$OOOOOOOOOOOOOOO$I?~====~+IIOOO,,                     
                        ?OOOOZ$OOOOOOOOOOOOOOOOOOOOOZZ+.                       
                      ..$OOOZ.~OOO$?7OOOO$$$$$$7I?+:.                         
                        .=OOOZ+.$ZOZ,,ZZOO=:::::                               
                        .$OOO$.=7OO? 7IOO+                                   
                          .77OO$,:~OO$..+OO+..                                 
                          .,,OOO?:.IOO++.$OZ::                                 
                            .=OOZ?~.$OOO:,ZO77..                               
                            ..IOOZ$:~$OOO,~ZOO?.                               
                            ..,ZOOO=.IIZOI.=+..                               
                                ~IZOO=..IOO=..                                 
                                .~+ZOZ:~.7Z~.                                 
                                .  =ZOO$...                                   
                                    .~ZZ$.                                     
                                      ~:,.

split in to 9 files.

craigjward 12-12-2007 11:52 AM

I'm new to scripting, is it possible to use the awk command to output text from a file line by line without using c++.
I'm sorry for being so frustrating.

To clarify, here is the contents of file 1:
Code:

                         
                         
                        .
                      . I
                      .:Z
                        IO
                      .=ZO
                      .IOO
                    ,,$OO
                    .==ZOO
                    .??OOO
              :??I7?:IIOOO
          ..:7OOOOO$:IIOOO

File 2:
Code:

  .I$:                   
 ?+OO7.                 
~OOOOZ+,.        ...=I7::
OOOOOOO7I.  ..,IOOOOOOOOO
OOOOOOOOO=  ..:ZOOOOOOOOOO
OOOOOOOZZ~..+=OOOOOOOOOOOO
OOOOOO?:..+ZOOOOOOOOOOZI++
OOOOO7,..=ZOOOOOOOOO$+:. 
OOOOZ~.,,ZOOOOOOOO??:. :++
OOOO7 .=IOOOOOOOZ~...~7$ZO
OOO$.,7ZOOOOOOO:.:ZZOOOOOO
OOO+.~OOOOOOZZ,.~OOOOOOOOO
OOO,.?OOOOOO~~.:OOOOOOOOOO

File 3:
Code:

                         
                         
..                       
..                       
+..                       
O=..                     
,...                     
,~======:~,.             
$$ZZZZZZZ$$I+..           
OOOOOOOOOOOOZ:.           
OOOOOOOOOOOOZ:.           
OOOOOOOOOOOOZ:.           
ZOOOOOZOOOOOZ:.

and so on, through to 9 (The ninth block is a blank square)

colucix 12-12-2007 02:41 PM

If I have focused the problem, you have 9 tiles of a puzzle placed in 9 different directories, right? The problem is reduced to read the first 3 files simultaneously and print them line-by-line, then read the second 3 files and print them line-by-line, and so on.
To do this in shell scripting, you have to link three file descriptors with three input files and loop the process three times. An example can be
Code:

#!/bin/bash

# set input field separator equal to newline \n
# to take care of blank spaces in the input files
IFS=$(echo)

for row in 1 2 3
do

  # link file descriptors 3, 4, 5 to three files from
  # three directories in sequence
  exec 3< $((row * 3 - 2))/*
  exec 4< $((row * 3 - 1))/*
  exec 5< $((row * 3))/*

  # read line by line from each file descriptor
  # and echo lines together
  while read -u 3 line1
  do
      read -u 4 line2
      read -u 5 line3
      echo $line1$line2$line3
  done

  # close file descriptors
  exec 3<&-
  exec 4<&-
  exec 5<&-

done

The code above assumes you have the following directories and only one file in each (as you stated in your first post)
Code:

1/  2/  3/  4/  5/  6/  7/  8/  9/
Another assumption is that the three files have the same number of lines, otherwise the while loop fails.

craigjward 12-12-2007 04:45 PM

Thanks!

What exactly does the exec command do? I couldn't find a man page for it... How do I customize the redirect to my needs? I tried running the code given, and am getting ambiguous redirect errors.

Thanks again

chrism01 12-12-2007 05:05 PM

http://www.tldp.org/LDP/abs/html/x16...l#USINGEXECREF

craigjward 12-12-2007 05:15 PM

Thanks, but I still don't understand why I am getting these ambiguous redirect errors... Is it because of the wildcard at the end of the path?

colucix 12-12-2007 05:24 PM

Quote:

Originally Posted by craigjward (Post 2988726)
What exactly does the exec command do? I couldn't find a man page for it... How do I customize the redirect to my needs? I tried running the code given, and am getting ambiguous redirect errors.

exec is a bash built-in command. In this context it is used to perform input redirection:
Code:

exec 3< my_file
this links the file descriptor 3 to the file my_file, where a file descriptor is simply a number that the operating system assigns to an open file to keep track of it. To know file descriptors usage inside out, you can see chapter 19 of the Advanced Bash Scripting Guide.
To customize the redirect to your needs you have to simply build the path of each file. As I told you, I assumed the 9 directories were simply 1, 2, 3, and so on... and accordingly
Code:

when row = 1  -->  $((row * 3 - 2))  is  1
              -->  $((row * 3 - 1))  is  2
              -->  $((row * 3))      is  3
when row = 2  -->  $((row * 3 - 2))  is  4
              -->  $((row * 3 - 1))  is  5
and so on...

Finally, the "ambiguous redirect" error is due to the presence of more than one file in each directory so that the wildcard in
Code:

exec 3< $((row * 3 - 2))/*
matches more than one file and the shell is confused (it does not know which input must be actually redirected).


All times are GMT -5. The time now is 05:25 PM.