[SOLVED] Having trouble showing more output.

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
mrbubl3s
Posts: 52
Joined: Sun 14 Mar 2010, 01:06
Location: %EARTHDIR%
Contact:

[SOLVED] Having trouble showing more output.

#1 Post by mrbubl3s »

Hello fellow forum members, is there a way in shell script to show a certain line of output with printf?

Example:

# fortune

Once the game is over,
the king and the pawn
go in the same box.

--whoever

How would I show 'go in the same box', instead of just the first line?

and

How do I show all the output?

I know how you get the last line (or first), and/or the last lines up (or the first lines down), using:

<command> | head -2
or
<command> | tail -2

Thanks! :D
Last edited by mrbubl3s on Mon 05 Apr 2010, 03:01, edited 1 time in total.

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#2 Post by sunburnt »

Hi mrbubl3s; Several ways as usual...

>>> Method #1:

Code: Select all

S1=Once the game is over,
S2=the king and the pawn
S3=go in the same box.
Echo the variables you want to show:

Code: Select all

echo -e $S1\\n$S2\\n$S3
If you have lots of lines to manage put them in an array.

>>> Method #2:

Code: Select all

S='Once the game is over,
the king and the pawn
go in the same box.'
Echo the variable through sed:

Code: Select all

echo "$S" |sed '3!d'
The variable "$S" must be double quoted to preserve the carriage returns ( new lines ).
The number 3 in the sed statement is the line you want shown.
To show more lines use the first and last line you want shown.
All 3 of them:

Code: Select all

echo "$S" |sed '1,3!d'
Just the first 2:

Code: Select all

echo "$S" |sed '1,2!d'
The lines shown must be continuous, you can`t show them at random.
>>> However... Method #1 can show any combination of lines you want it to.

mrbubl3s
Posts: 52
Joined: Sun 14 Mar 2010, 01:06
Location: %EARTHDIR%
Contact:

#3 Post by mrbubl3s »

Yes! Thank you! :)

Post Reply