LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-26-2012, 01:51 PM   #1
Mes9
Member
 
Registered: Jan 2012
Distribution: Fedora 17
Posts: 38

Rep: Reputation: 0
BASH: Modify script to take $1 that specifies the number of chapters to be printed


How can I modify this script to accept a command-line argument that specifies how many chapters are to be printed. The file to be read is a book in a single .txt file wich is formatted book:chapter:verse:text
Code:
while IFS=: read book chapter verse text
do
   firstword=${text%% *}
   printf "%s %s:%s %s\n" "$book" "$chapter" "$verse" "$firstword"
done < "$file_to_be_read
"
 
Old 08-26-2012, 05:30 PM   #2
rigor
Member
 
Registered: Sep 2003
Location: 19th moon ................. ................Planet Covid ................Another Galaxy;............. ................Not Yours
Posts: 705

Rep: Reputation: Disabled
Are you saying that you want to read more than one combination of the following values?

Code:
book chapter verse text
Or do you wish to read only one set of those values, and print starting at the point specified by those values, and a specified number of chapters following that point?
 
1 members found this post helpful.
Old 08-27-2012, 03:43 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Create a counter and increment and test until equal to passed in value.
 
1 members found this post helpful.
Old 08-27-2012, 02:21 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Please give us an actual example of the input and explain in more detail what you want to do with it. As it stands it's not entirely clear what you want to do.

If I'm understanding it at all, you have multiple chapters on a line? If so I imagine that you'll probably want to change read to store the results in an array instead, and process the arguments with numerical indexes in some way. I'm guessing that index 0 should be book, and the final two entries should be verse and text ( bash from v4.2 can accept negative index numbers for this), with everything between them being chapters, correct?

If so, is it really necessary to manually enter a number?

Or is it still one chapter per line, and you want to print only the lines up to that point? In which case just set up a test of the $chapter variable, and have it break the loop when it exceeds the $1 value.

Last edited by David the H.; 08-27-2012 at 02:26 PM. Reason: Expanded the post.
 
1 members found this post helpful.
Old 09-02-2012, 10:10 AM   #5
Mes9
Member
 
Registered: Jan 2012
Distribution: Fedora 17
Posts: 38

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Mes9 View Post
How can I modify this script to accept a command-line argument that specifies how many chapters are to be printed. The file to be read is a book in a single .txt file wich is formatted book:chapter:verse:text
Code:
while IFS=: read book chapter verse text
do
   firstword=${text%% *}
   printf "%s %s:%s %s\n" "$book" "$chapter" "$verse" "$firstword"
done < "$file_to_be_read"
"

Here is some of the output from this script. What it does is it prints out the intire book, that is all the "books", chapters, verses and then only the first word of the text. I need to tell the script how many chapters are to be printed.

Revelation 021:005 And
Revelation 021:006 And
Revelation 021:007 He
Revelation 021:008 But
Revelation 021:009 And
Revelation 021:010 And
Revelation 021:011 Having
Revelation 021:012 And
Revelation 021:013 On
Revelation 021:014 And
Revelation 021:015 And
Revelation 021:016 And
Revelation 021:017 And
Revelation 021:018 And
Revelation 021:019 And
Revelation 021:020 The
Revelation 021:021 And
Revelation 021:022 And
Revelation 021:023 And
Revelation 021:024 And
Revelation 021:025 And
Revelation 021:026 And
Revelation 021:027 And
Revelation 022:001 And
Revelation 022:002 In
Revelation 022:003 And
Revelation 022:004 And
Revelation 022:005 And
Revelation 022:006 And
Revelation 022:007 Behold,
Revelation 022:008 And
Revelation 022:009 Then
Revelation 022:010 And
Revelation 022:011 He
Revelation 022:012 And,
Revelation 022:013 I
Revelation 022:014 Blessed
Revelation 022:015 For
Revelation 022:016 I
Revelation 022:017 And
Revelation 022:018 For
Revelation 022:019 And
Revelation 022:020 He
Revelation 022:021 The

Here is an glimpse from the book:
Revelation:022:007:Behold, I come quickly: blessed is he that keepeth the sayings of the prophecy of this book.
Revelation:022:008:And I John saw these things, and heard them. And when I had heard and seen, I fell down to worship before the feet of the angel which shewed me these things.
Revelation:022:009:Then saith he unto me, See thou do it not: for I am thy fellowservant, and of thy brethren the prophets, and of them which keep the sayings of this book: worship God.
Revelation:022:010:And he saith unto me, Seal not the sayings of the prophecy of this book: for the time is at hand.
Revelation:022:011:He that is unjust, let him be unjust still: and he which is filthy, let him be filthy still: and he that is righteous, let him be righteous still: and he that is holy, let him be holy still.
Revelation:022:012:And, behold, I come quickly; and my reward is with me, to give every man according as his work shall be.
Revelation:022:013:I am Alpha and Omega, the beginning and the end, the first and the last.
Revelation:022:014:Blessed are they that do his commandments, that they may have right to the tree of life, and may enter in through the gates into the city.
Revelation:022:015:For without are dogs, and sorcerers, and whoremongers, and murderers, and idolaters, and whosoever loveth and maketh a lie.
Revelation:022:016:I Jesus have sent mine angel to testify unto you these things in the churches. I am the root and the offspring of David, and the bright and morning star.
Revelation:022:017:And the Spirit and the bride say, Come. And let him that heareth say, Come. And let him that is athirst come. And whosoever will, let him take the water of life freely.
Revelation:022:018:For I testify unto every man that heareth the words of the prophecy of this book, If any man shall add unto these thing
 
Old 09-02-2012, 10:40 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
So something like:
Code:
awk -vnum=4 -F"[: ]" 'NR <= num{print $1,$2 ":" $3,$4}' file
Set num value to how many lines to return.
 
Old 09-02-2012, 02:05 PM   #7
rigor
Member
 
Registered: Sep 2003
Location: 19th moon ................. ................Planet Covid ................Another Galaxy;............. ................Not Yours
Posts: 705

Rep: Reputation: Disabled
Hi Mes9!

I put the text of the so called King James Version of Revelation 21 and 22 in a file named book.txt, in this format:

Code:
Revelation:021:001:And I saw a new heaven and a new earth: for the first heaven and the first earth were passed away; and there was no more sea.
Revelation:021:002:And I John saw the holy city, new Jerusalem, coming down from God out of heaven, prepared as a bride adorned for her husband.
Revelation:021:003:And I heard a great voice out of heaven saying, Behold, the tabernacle of God is with men, and he will dwell with them, and they shall be his people, and God himself shall be with them, and be their God.
Revelation:021:004:And God shall wipe away all tears from their eyes; and there shall be no more death, neither sorrow, nor crying, neither shall there be any more pain: for the former things are passed away.
Revelation:021:005:And he that sat upon the throne said, Behold, I make all things new. And he said unto me, Write: for these words are true and faithful.
Revelation:021:006:And he said unto me, It is done. I am Alpha and Omega, the beginning and the end. I will give unto him that is athirst of the fountain of the water of life freely.
Revelation:021:007:He that overcometh shall inherit all things; and I will be his God, and he shall be my son.
Revelation:021:008:But the fearful, and unbelieving, and the abominable, and murderers, and whoremongers, and sorcerers, and idolaters, and all liars, shall have their part in the lake which burneth with fire and brimstone: which is the second death.
Revelation:021:009:And there came unto me one of the seven angels which had the seven vials full of the seven last plagues, and talked with me, saying, Come hither, I will shew thee the bride, the Lamb's wife.
Revelation:021:010:And he carried me away in the spirit to a great and high mountain, and shewed me that great city, the holy Jerusalem, descending out of heaven from God,
Revelation:021:011:Having the glory of God: and her light was like unto a stone most precious, even like a jasper stone, clear as crystal;
Revelation:021:012:And had a wall great and high, and had twelve gates, and at the gates twelve angels, and names written thereon, which are the names of the twelve tribes of the children of Israel:
Revelation:021:013:On the east three gates; on the north three gates; on the south three gates; and on the west three gates.
Revelation:021:014:And the wall of the city had twelve foundations, and in them the names of the twelve apostles of the Lamb.
Revelation:021:015:And he that talked with me had a golden reed to measure the city, and the gates thereof, and the wall thereof.
Revelation:021:016:And the city lieth foursquare, and the length is as large as the breadth: and he measured the city with the reed, twelve thousand furlongs. The length and the breadth and the height of it are equal.
Revelation:021:017:And he measured the wall thereof, an hundred and forty and four cubits, according to the measure of a man, that is, of the angel.
Revelation:021:018:And the building of the wall of it was of jasper: and the city was pure gold, like unto clear glass.
Revelation:021:019:And the foundations of the wall of the city were garnished with all manner of precious stones. The first foundation was jasper; the second, sapphire; the third, a chalcedony; the fourth, an emerald;
Revelation:021:020:The fifth, sardonyx; the sixth, sardius; the seventh, chrysolite; the eighth, beryl; the ninth, a topaz; the tenth, a chrysoprasus; the eleventh, a jacinth; the twelfth, an amethyst.
Revelation:021:021:And the twelve gates were twelve pearls; every several gate was of one pearl: and the street of the city was pure gold, as it were transparent glass.
Revelation:021:022:And I saw no temple therein: for the Lord God Almighty and the Lamb are the temple of it.
Revelation:021:023:And the city had no need of the sun, neither of the moon, to shine in it: for the glory of God did lighten it, and the Lamb is the light thereof.
Revelation:021:024:And the nations of them which are saved shall walk in the light of it: and the kings of the earth do bring their glory and honour into it.
Revelation:021:025:And the gates of it shall not be shut at all by day: for there shall be no night there.
Revelation:021:026:And they shall bring the glory and honour of the nations into it.
Revelation:021:027:And there shall in no wise enter into it any thing that defileth, neither whatsoever worketh abomination, or maketh a lie: but they which are written in the Lamb's book of life.
Revelation:022:001:And he shewed me a pure river of water of life, clear as crystal, proceeding out of the throne of God and of the Lamb.
Revelation:022:002:In the midst of the street of it, and on either side of the river, was there the tree of life, which bare twelve manner of fruits, and yielded her fruit every month: and the leaves of the tree were for the healing of the nations.
Revelation:022:003:And there shall be no more curse: but the throne of God and of the Lamb shall be in it; and his servants shall serve him:
Revelation:022:004:And they shall see his face; and his name shall be in their foreheads.
Revelation:022:005:And there shall be no night there; and they need no candle, neither light of the sun; for the Lord God giveth them light: and they shall reign for ever and ever.
Revelation:022:006:And he said unto me, These sayings are faithful and true: and the Lord God of the holy prophets sent his angel to shew unto his servants the things which must shortly be done.
Revelation:022:007:Behold, I come quickly: blessed is he that keepeth the sayings of the prophecy of this book.
Revelation:022:008:And I John saw these things, and heard them. And when I had heard and seen, I fell down to worship before the feet of the angel which shewed me these things.
Revelation:022:009:Then saith he unto me, See thou do it not: for I am thy fellowservant, and of thy brethren the prophets, and of them which keep the sayings of this book: worship God.
Revelation:022:010:And he saith unto me, Seal not the sayings of the prophecy of this book: for the time is at hand.
Revelation:022:011:He that is unjust, let him be unjust still: and he which is filthy, let him be filthy still: and he that is righteous, let him be righteous still: and he that is holy, let him be holy still.
Revelation:022:012:And, behold, I come quickly; and my reward is with me, to give every man according as his work shall be.
Revelation:022:013:I am Alpha and Omega, the beginning and the end, the first and the last.
Revelation:022:014:Blessed are they that do his commandments, that they may have right to the tree of life, and may enter in through the gates into the city.
Revelation:022:015:For without are dogs, and sorcerers, and whoremongers, and murderers, and idolaters, and whosoever loveth and maketh a lie.
Revelation:022:016:I Jesus have sent mine angel to testify unto you these things in the churches. I am the root and the offspring of David, and the bright and morning star.
Revelation:022:017:And the Spirit and the bride say, Come. And let him that heareth say, Come. And let him that is athirst come. And whosoever will, let him take the water of life freely.
Revelation:022:018:For I testify unto every man that heareth the words of the prophecy of this book, If any man shall add unto these things, God shall add unto him the plagues that are written in this book:
Revelation:022:019:And if any man shall take away from the words of the book of this prophecy, God shall take away his part out of the book of life, and out of the holy city, and from the things which are written in this book.
Revelation:022:020:He which testifieth these things saith, Surely I come quickly. Amen. Even so, come, Lord Jesus.
You haven't told us how the value for file_to_be_read gets into the bash script you showed us. Is it in the bash environment? Is it supplied as an argument to the script?

So I've just treated it as being given a specific value in a portion of the script that you haven't showed us. I started by placing the text of the bash script you showed us in a file, and edited the file to look like this:

Code:
if [  $#  -eq  1  ]
    then

        number_of_chapters="$1"
        chapter_count=0
        previous_chapter=-1
        file_to_be_read='book.txt'

        while IFS=: read book chapter verse text
            do

                if [  $chapter  -ne  $previous_chapter  ]
                    then
                        previous_chapter=$chapter
                        (( chapter_count += 1  ))
                fi

                if [  $chapter_count  -gt  $number_of_chapters  ]
                    then
                        break
                fi

                firstword=${text%% *}
                printf "%s %s:%s %s\n" "$book" "$chapter" "$verse" "$firstword"

            done < "$file_to_be_read"

    else

        echo "Usage:  ${0} number_of_chapters"
fi
When I run that with a single argument 1 passed to the script, I get this output:

Code:
Revelation 021:001 And
Revelation 021:002 And
Revelation 021:003 And
Revelation 021:004 And
Revelation 021:005 And
Revelation 021:006 And
Revelation 021:007 He
Revelation 021:008 But
Revelation 021:009 And
Revelation 021:010 And
Revelation 021:011 Having
Revelation 021:012 And
Revelation 021:013 On
Revelation 021:014 And
Revelation 021:015 And
Revelation 021:016 And
Revelation 021:017 And
Revelation 021:018 And
Revelation 021:019 And
Revelation 021:020 The
Revelation 021:021 And
Revelation 021:022 And
Revelation 021:023 And
Revelation 021:024 And
Revelation 021:025 And
Revelation 021:026 And
Revelation 021:027 And
Hope this helps.
 
Old 09-03-2012, 09:41 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Following kakaka's example:
Code:
awk -vchpt=1 -F"[: ]" '$2!=curr{curr=$2;x++}x<=chpt{print $1,$2 ":" $3,$4}' file
 
Old 09-05-2012, 03:03 PM   #9
Mes9
Member
 
Registered: Jan 2012
Distribution: Fedora 17
Posts: 38

Original Poster
Rep: Reputation: 0
Thanks

Thank you all for the reply's. Think it is going to take some time to work through this.
 
Old 09-26-2012, 02:36 PM   #10
Mes9
Member
 
Registered: Jan 2012
Distribution: Fedora 17
Posts: 38

Original Poster
Rep: Reputation: 0
mark as solved

Quote:
Originally Posted by Mes9 View Post
Thank you all for the reply's. Think it is going to take some time to work through this.
I am going through some other books, so it will be awhile until I go back to this question, thank you again for the feedback.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script to apply a command to a file to be printed linuxandtsm Linux - Newbie 7 09-28-2011 01:02 PM
How to modify what already printed on the screen ? bfyviolin Programming 1 05-06-2011 04:36 AM
Bash:Printing the line number in bash script suryaemlinux Programming 2 02-05-2011 09:59 AM
Playful Bash Script - I want to modify it. rrrssssss Programming 3 12-20-2009 05:25 PM
modify bash script - recursive action xscousr Programming 6 09-17-2003 01:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration