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!
[SOLVED] Having trouble showing more output.
[SOLVED] Having trouble showing more output.
Last edited by mrbubl3s on Mon 05 Apr 2010, 03:01, edited 1 time in total.
Hi mrbubl3s; Several ways as usual...
>>> Method #1:
Echo the variables you want to show:
If you have lots of lines to manage put them in an array.
>>> Method #2:
Echo the variable through sed:
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:
Just the first 2:
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.
>>> Method #1:
Code: Select all
S1=Once the game is over,
S2=the king and the pawn
S3=go in the same box.
Code: Select all
echo -e $S1\\n$S2\\n$S3
>>> Method #2:
Code: Select all
S='Once the game is over,
the king and the pawn
go in the same box.'
Code: Select all
echo "$S" |sed '3!d'
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'
Code: Select all
echo "$S" |sed '1,2!d'
>>> However... Method #1 can show any combination of lines you want it to.