Find the answer to your Linux question:
Page 3 of 3 FirstFirst 1 2 3
Results 21 to 28 of 28
Thanks for all your effort, I'm wondering which line gives the Header (Title,Timing,Availability,Cinema) showing and which line determines the column width of the input data from the .txt file. Code: ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #21
    Just Joined!
    Join Date
    Nov 2012
    Posts
    18

    Thanks for all your effort, I'm wondering which line gives the Header (Title,Timing,Availability,Cinema) showing and which line determines the column width of the input data from the .txt file.


    Code:
     while read line; do
        date=$(echo "$line"|awk -F, '{print $1}')
        title=$(echo "$line"|awk -F, '{print $2}')
        avail=$(echo "$line"|awk -F, '{print $3}')
        cine=$(echo "$line"|awk -F, '{print $4}')
        # printf "%-26s%-14s%-13s%s\n" "$title" "$date" "$avail" "$cine"
        printf "%s\t" "$title" "$date" "$avail" "$cine"
        printf "\n"
      done < <(cat movielist.txt)
    }
    
    FILE=${1-data1}
    
    pl " Input data file $FILE:"
    cat $FILE
    
    cp $FILE movielist.txt
    
    pl " Results:"
    list_movies |
    tee f1 |
    align -st
    
    exit 0

  2. #22
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,294
    Hi.

    The perl script align remembers the longest width in each field (column), and uses that to spread the column at just the correct width. One can change the default gutter if one desires.

    It also justifies right or left depending on numeric or alpha content -- but if there is header, it's likely that each field will be treated as alpha. That also can be over-ridden.

    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  3. #23
    Just Joined!
    Join Date
    Nov 2012
    Posts
    18
    My alignment has issues .. i trried using cygwin.

    anyone can help me with the header ?

    4 headers

    take in 4 values from the txt.file separated by a , (-d:","

  4. $spacer_open
    $spacer_close
  5. #24
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Quote Originally Posted by uglyboy View Post
    My alignment has issues .. i trried using cygwin.
    Cygwin should not have anything to do with alignment.

    anyone can help me with the header ?

    4 headers

    take in 4 values from the txt.file separated by a , (-d:","
    Separated by a what, exactly? Is there an emoticon in there? Better show us your code.

  6. #25
    Just Joined!
    Join Date
    Nov 2012
    Posts
    18
    Quote Originally Posted by uglyboy View Post
    Okay, I'm looking @ 1 file.

    shell file
    Code:
    #display main ticketing menu
    display_menu()
    {
      # Called by run_menu
      echo
      echo "-- Theatre Self-service Ticketing --"
      echo -n "1. "
      echo -e '\E[1;32;40mList all Movies and Show time\E[0m'
      echo -n "2. "
      echo -e '\E[1;32;40mDisplay theatre鈥檚 seat \E[0m'
      echo -n "q. "
      echo -e '\E[1;31;47mQuit\E[0m'
    	
      echo -en "Enter your selection: "
    }
    
    # List out available movies
    list_movies()
    {
    	# Called by run_menu
    	
    	old_IFS=$IFS 
    	IFS=$'\n'
    	print_movie_header
    	for LINE in `sed -e 's/Yes/Ticket Available/g' -e 's/No/SOLD OUT/g' movielist.txt`  
    	do
    		print_movie $LINE
    	done
      IFS=$old_IFS 
    }
    	
    # Output header for movie listing
    # Catered for both numbered and non-numbered list
    print_movie_header()
    {
    	echo -e '\E[1;32;40m'
    	if [ $# == "1" ];then
    		echo -n "   "
    	fi
    	echo -n -e 'Movie Title'
      set_colwidth 'Movie Title' 25
    	echo -n -e 'Show Time\t'
    	if [ $# == "1" ];then
    		echo -n -e "\t"
    	fi
    	echo -n 'Vacancy'
    	echo -e '\E[0m'
    }
    
    # Set the width of column
    # $1 is the text in the column 
    # and $2 is the intended column width
    set_colwidth()
    {
    	STRING=$1
    	LENGTH=${#STRING}
    
    	for (( i=$LENGTH; i<=$2; i++))
    	do 
    		echo -n " "
    	done 
    
    }
    
    # Output movie entry
    print_movie()
    {
    	TIME=`echo $1 | cut -f1 -d ","`
    	TITLE=`echo $1 | cut -f2 -d ","`
    	AVAILABLE=`echo $1 | cut -f3 -d ","` 
    
    	echo -n -e $TITLE
    	set_colwidth $TITLE 25
    	echo -n -e $TIME '\t'
      echo $AVAILABLE
    }
    
    
    # Hint2
    # Get current seating arrangement of 
    # selected movie from seating.txt
    movie_data()
    {
    	COUNT=1
    	old_IFS=$IFS 
    	IFS=$'\n'
    	for LINE in `cat seating.txt`  
    	do
    		if test $1 -eq $COUNT
    			then
    				display_seats $LINE $2
    
    		fi
    		THEATER[$COUNT]=$LINE
    		let COUNT=COUNT+1
    		
    	done
      IFS=$old_IFS 	
    }
    
    # Mark occupied seat with a X and
    # seats selected by user with a red X
    mark_seat()
    {
    	# Called by display_seats
    	
    	if [ $(echo "$1" | grep -c "$2$3") -gt 0 ]
    	then
    			echo -n "X" 
    		  set_colwidth "" 5
    	elif [ $(echo "$4" | grep -c "$2$3") -gt 0 ]
    	then
    			echo -en '\E[1;31mX\E[0m'
    		  set_colwidth "" 5
    	else
    			set_colwidth "" 6 
    	fi
    }
    
    # Print out theater's seating arrangement
    display_seats()
    {
    	COL=5
    	ROW=4
    	echo
    
    	echo "---------------------------------------"
    	echo -e '\E[1;34;47m----------------SCREEN-----------------\E[0m'
    	echo "---------------------------------------"
    	for (( x=0; x<=$ROW; x++))
    	do 
    	echo -n "$x" | tr "0" " "
    		for (( j=0; j<=$COL; j++))
    		do 
    			if [ $x == "0" ]
    			then
    				echo -n $j | tr "012345" " ABCDE"
    				set_colwidth "" 5
    			else
    				CURR_COL=$(echo $j | tr "12345" "ABCDE")
    				if [ $j == "0" ]
    				then
    					set_colwidth "" 6
    				else
    					mark_seat $1 $CURR_COL $x $2
    				fi
    			fi
    	done 
      echo ""
    	done
    	echo "---------------------------------------"
    }
    
    
    # Select and book seat manually
    select_seat()
    {
    	# Called by run_menu
    	
    	COUNT=0
    	echo
    	echo "-- Select A Seat Manually --"
    	echo
    	old_IFS=$IFS 
    	IFS=$'\n'
    	print_movie_header "numbered"
    	for LINE in `sed -e 's/Yes/Ticket Available/g' -e 's/No/SOLD OUT/g' movielist.txt `;do
    		let COUNT=COUNT+1
    		echo -n "$COUNT. "
    		print_movie $LINE
    	done
    	IFS=$old_IFS 
    	echo
    	
    	# Select movie to book
    	echo -en "Select Movie To Book (Enter 1 to ${COUNT}): "
    	read NUM
    	NUMERIC=0
    	while [ "$NUMERIC" != "1" ];do
    		for (( x=$COUNT; x>=1; x--))
    		do
    			if [ $NUM == "$x" ];then
    				NUMERIC=1
    			fi
    		done
    
    		if [ "$NUMERIC" != "1" ];then
    			echo -en "Invalid Movie Selected, Try Again (Enter 1 to ${COUNT}): "
    			read NUM
    		fi
    	done
    
    	movie_data $NUM
    	echo
    	old_IFS=$IFS 
    	IFS=$'\n'
    	
    	 
    	echo
    }
    
    # Main menu navigation
    run_menu()
    {
    	i=-1
      while [ "$i" != "q" ]; do
    		display_menu
    		read i
    		i=`echo $i | tr '[A-Z]' '[a-z]'`
    
    		case "$i" in 
    	  	"1")
    			list_movies
    			;;
    		"2")
    			select_seat
    			;;
    		"q")
    			echo "GoodBye, Have A Nice Day"
    			exit 0
    			;;
    		*)
    			echo "Unrecognised Input."
    			;;
    		esac
      done
    }
    
    
    #----------------------------------------------#
    # ---------PROGRAM SCRIPT START HERE---------- #
    #----------------------------------------------#
    
    if [ ! -f movielist.txt ]; then
      echo "Error: Movie list is not available"
    fi
    
    
    run_menu
    movielist.txt
    Code:
    1300  2Oct,The Social Network,No
    1300  2Oct,The Town,Yes
    1700  2Oct,Paranormal Activity 2,Yes
    1900  2Oct,You Again,Yes
    2100  2Oct,Kamui,Yes
    seating.txt
    Code:
    0;,A2,A3,A1,A7,A9
    0;,E6,E7,E8
    
    14;,A1,D4,C3,B3,A4,E4
    15;,A1,D4,E2,C3,A2
    15;,A1,B2,C3,D4,A4
    Can anyone explain to me the shell ?

    How do you create extra table in the heading

    Movie Title Time Slot Availability (how to include Book Now )

    How do you also align the data to fit the header
    I'm stuck @ one area and I can't really progress.

    I need to do an airline reservation thing.


    Header supposed to show

    [Flight] [ Departure Time] [ Airlines] [Vacancy]

  7. #26
    Just Joined!
    Join Date
    May 2012
    Posts
    43
    Have you actually tried to change anything?
    All you've done is ask us for a solution, not given us any example of effort on your behalf.
    Editing tables is fairly straight forward, all you need to do is add an additional value in the .txt file and add a bit more code so it does action on it, which you can easily copy and paste some existing code and modify a few values.

    This also looks like homework, which is against the forum rules.

  8. #27
    Just Joined!
    Join Date
    Nov 2012
    Posts
    18
    Code:
    #display main ticketing menu
    display_menu()
    {
      # Called by run_menu
      echo
      echo "-- Theatre Self-service Ticketing --"
      echo -n "1. "
      echo -e '\E[1;32;40mList all Movies and Show time\E[0m'
      echo -n "2. "
      echo -e '\E[1;32;40mDisplay theatre seat \E[0m'
      echo -n "q. "
      echo -e '\E[1;31;47mQuit\E[0m'
    	
      echo -en "Enter your selection: "
    }
    
    # List out available movies
    list_movies()
    {
    	# Called by run_menu
    	
    #	old_IFS=$IFS 
    #	IFS=$'\n'
    	print_movie_header
    #	for LINE in `sed -e 's/Yes/Ticket Available/g' -e 's/No/SOLD OUT/g' movielist.txt`  
    #	do
    #echo "printing movie line '$LINE'"
    #		print_movie $LINE
    #	done
    #  IFS=$old_IFS 
    
    for LINE in `sed -e 's/Yes/Ticket Available/g' -e 's/No/SOLD OUT/g' movielist.txt`  
      do
        date=$(echo "$line"|awk -F, '{print $1}')
        title=$(echo "$line"|awk -F, '{print $2}')
        avail=$(echo "$line"|awk -F, '{print $3}')
        cine=$(echo "$line"|awk -F, '{print $4}')
    
        printf "%-26s%-14s%-13s%s\n" "$TITLE" "$date" "$avail" "$cine"
      done < <(cat movielist.txt)
    }
    
    # Output header for movie listing
    # Catered for both numbered and non-numbered list
    print_movie_header()
    {
    	echo -e '\E[1;32;40m'
    	if [ $# == "1" ];then
    		echo -n "   "
    	fi
    	echo -n -e 'Movie Title'
      set_colwidth 'Movie Title' 25
    	echo -n -e 'Show Time\t'
    	if [ $# == "1" ];then
    		echo -n -e "\t"
    	fi
    	echo -n 'Vacancy'
    	 
    echo -n '      Cinema Showing'
    
    echo -e '\E[0m'
    
    }
    
    # Set the width of column
    # $1 is the text in the column 
    # and $2 is the intended column width
    set_colwidth()
    {
    	STRING=$1
    	LENGTH=${#STRING}
    
    	for (( i=$LENGTH; i<=$2; i++))
    	do 
    		echo -n " "
    	done 
    
    }
    
    # Output movie entry
    print_movie()
    {
    	TIME=`echo $1 | cut -f1 -d ","`
    	TITLE=`echo $1 | cut -f2 -d ","`
    	AVAILABLE=`echo $1 | cut -f3 -d ","` 
    
    	echo -n -e $TITLE
    	set_colwidth $TITLE 25
    	echo -n -e $TIME '\t'
      echo $AVAILABLE
    }
    
    
    # Hint2
    # Get current seating arrangement of 
    # selected movie from seating.txt
    movie_data()
    {
    	COUNT=1
    	old_IFS=$IFS 
    	IFS=$'\n'
    	for LINE in `cat seating.txt`  
    	do
    		if test $1 -eq $COUNT
    			then
    				display_seats $LINE $2
    
    		fi
    		THEATER[$COUNT]=$LINE
    		let COUNT=COUNT+1
    		
    	done
      IFS=$old_IFS 	
    }
    
    # Mark occupied seat with a X and
    # seats selected by user with a red X
    mark_seat()
    {
    	# Called by display_seats
    	
    	if [ $(echo "$1" | grep -c "$2$3") -gt 0 ]
    	then
    			echo -n "X" 
    		  set_colwidth "" 5
    	elif [ $(echo "$4" | grep -c "$2$3") -gt 0 ]
    	then
    			echo -en '\E[1;31mX\E[0m'
    		  set_colwidth "" 5
    	else
    			set_colwidth "" 6 
    	fi
    }
    
    # Print out theater's seating arrangement
    display_seats()
    {
    	COL=5
    	ROW=4
    	echo
    
    	echo "---------------------------------------"
    	echo -e '\E[1;34;47m----------------SCREEN-----------------\E[0m'
    	echo "---------------------------------------"
    	for (( x=0; x<=$ROW; x++))
    	do 
    	echo -n "$x" | tr "0" " "
    		for (( j=0; j<=$COL; j++))
    		do 
    			if [ $x == "0" ]
    			then
    				echo -n $j | tr "012345" " ABCDE"
    				set_colwidth "" 5
    			else
    				CURR_COL=$(echo $j | tr "12345" "ABCDE")
    				if [ $j == "0" ]
    				then
    					set_colwidth "" 6
    				else
    					mark_seat $1 $CURR_COL $x $2
    				fi
    			fi
    	done 
      echo ""
    	done
    	echo "---------------------------------------"
    }
    
    
    # Select and book seat manually
    select_seat()
    {
    	# Called by run_menu
    	
    	COUNT=0
    	echo
    	echo "-- Select A Seat Manually --"
    	echo
    	old_IFS=$IFS 
    	IFS=$'\n'
    	print_movie_header "numbered"
    	for LINE in `sed -e 's/Yes/Ticket Available/g' -e 's/No/SOLD OUT/g' movielist.txt `;do
    		let COUNT=COUNT+1
    		echo -n "$COUNT. "
    		print_movie $LINE
    	done
    	IFS=$old_IFS 
    	echo
    	
    	# Select movie to book
    	echo -en "Select Movie To Book (Enter 1 to ${COUNT}): "
    	read NUM
    	NUMERIC=0
    	while [ "$NUMERIC" != "1" ];do
    		for (( x=$COUNT; x>=1; x--))
    		do
    			if [ $NUM == "$x" ];then
    				NUMERIC=1
    			fi
    		done
    
    		if [ "$NUMERIC" != "1" ];then
    			echo -en "Invalid Movie Selected, Try Again (Enter 1 to ${COUNT}): "
    			read NUM
    		fi
    	done
    
    	movie_data $NUM
    	echo
    	old_IFS=$IFS 
    	IFS=$'\n'
    	
    	 
    	echo
    }
    
    # Main menu navigation
    run_menu()
    {
    	i=-1
      while [ "$i" != "q" ]; do
    		display_menu
    		read i
    		i=`echo $i | tr '[A-Z]' '[a-z]'`
    
    		case "$i" in 
    	  	"1")
    			list_movies
    			;;
    		"2")
    			select_seat
    			;;
    		"q")
    			echo "GoodBye, Have A Nice Day"
    			exit 0
    			;;
    		*)
    			echo "Unrecognised Input."
    			;;
    		esac
      done
    }
    
    
    #----------------------------------------------#
    # ---------PROGRAM SCRIPT START HERE---------- #
    #----------------------------------------------#
    
    if [ ! -f movielist.txt ]; then
      echo "Error: Movie list is not available"
    fi
    
    
    run_menu
    It prints out the header, but I can't get the movieList.txt to come out.

    I understand its homework, but all i need is to have the header match the txt.

    Sorry for anything

  9. #28
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Quote Originally Posted by uglyboy View Post
    It prints out the header, but I can't get the movieList.txt to come out.
    you've made some changes to the code, but you've mixed some things and have made things worse.

    here are three things I've noticed:

    1) in the list_movies() function, you've got this:
    Code:
    for LINE in `sed -e 's/Yes/Ticket Available/g' -e 's/No/SOLD OUT/g' movielist.txt`  
      do
        date=$(echo "$line"|awk -F, '{print $1}')
        title=$(echo "$line"|awk -F, '{print $2}')
        avail=$(echo "$line"|awk -F, '{print $3}')
        cine=$(echo "$line"|awk -F, '{print $4}')
    
        printf "%-26s%-14s%-13s%s\n" "$TITLE" "$date" "$avail" "$cine"
      done < <(cat movielist.txt)
    }
    i have bolded the problem line. you are processing each line twice. you do not need that entire for/sed line at all - just a simple while read line will do.

    2) in the same function, you assign the variable $title to a value, but then try to print $TITLE later. Variables, like all things in *nix, are case sensitive.

    3) does your movielist.txt file contain a fourth column, with the theatre info?

Page 3 of 3 FirstFirst 1 2 3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •