Web Hosting Talk







View Full Version : Display line <n>


innova
11-09-2004, 03:06 PM
I know this will probably be exceedingly simply but I cannot even think of how to do this right now.

Lets say I have a variable called 'var1'. It contains several lines of stuff (bash shell):

echo "$var1"

This displays:
One
Two
Three
Four
Five

How can I specify that I want to display ONLY line 2, or line 4? I need to display an ARBITRARY line from that variable's data. If only I could do:

echo "$var1" | displayline 3

I hope that makes sense. Probably something really silly I am forgetting...

Captian_Spike
11-09-2004, 03:19 PM
Your best bet is an array. Create an array with 5 entries, each line gets a place in the array. Something like

...
var1[3]=three
var1[4]=four
...
etc.

then you can echo var1[x] to print the line you want.

gogocode
11-09-2004, 06:01 PM
How about (for line 2)

echo "$var1" | head -2 | tail -1

innova
11-10-2004, 03:15 AM
So much for exceedingly simple :)

Good call on the arrays.. I decided that rather than find a patchwork solution, I should re-approach the problem.

I went with the arrays instead.

I posted about a similar problem I was having earlier with arrays being too slow, but with my new implementation it benchmarks 8x faster :)

Thanks for the thoughts.