Limiting Bash array single line output

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Limiting Bash array single line output
# 1  
Old 03-11-2017
Limiting Bash array single line output

Code:
#!/bin/bash

PH=(AD QD QC 5H 6C 8C 7D JH 3H 3S)

echo ${PH[@]}

In the above array, how can I print to screen just the first 8 elements of ${PH[@]} and have the last 2 elements print just below the first line starting underneath AD?

I need to do this in order to save terminal window spacing for Bash Script.

Thanks in advance for any and all suggestions.

Cogiz
# 2  
Old 03-11-2017
Code:
echo ${PH[@]:0:7}
echo ${PH[@]:8:9}


Last edited by Aia; 03-11-2017 at 10:34 PM..
# 3  
Old 03-11-2017
Quote:
Originally Posted by Aia
Code:
echo ${PH[@]:0:7}
echo ${PH[@]:8:9}

While this happens to work with the sample array provided, asking to print 9 elements starting with index 8 seems to be a strange way to print the last two elements of an array of 10 elements. If you want to print the 1st 7 elements of an array on one line and the last 2 elements of that array on a second line (no matter how many elements are in the array, you might want to try something more like:
Code:
echo ${PH[@]:0:7}
echo ${PH[@]:$((${#PH[@]} - 2))}

or, if there is any chance that any of those elements in your array might contain multiple adjacent spaces, might contain tabs, or might contain newlines that you want preserved in the output:
Code:
echo "${PH[@]:0:7}"
echo "${PH[@]:$((${#PH[@]} - 2))}"

This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 03-12-2017
Thank you very much for all of your help and suggestions...it pointed me in the right direction.

I used the following in my script:

Code:
tput cup 4 10
echo ${PH[@]:0:24}
tput cup 6 10
echo ${PH[@]:24:48}


Last edited by Don Cragun; 03-12-2017 at 12:19 AM.. Reason: Fix CODE tags.
# 5  
Old 03-12-2017
Why :48? for two elements?

With a recent bash, try
Code:
echo ${PH[@]: -2}
3H 3S

# 6  
Old 03-14-2017
Surely printf is a better fit here:
Code:
$ PH=(AD QD QC 5H 6C 8C 7D JH 3H 3S)
$ printf "%s\n" "${PH[@]}"
AD
QD
QC
5H
6C
8C
7D
JH
3H
3S

printf keeps reusing the format until it runs out of data to print. It follows that more %s specifiers will print more fields:
Code:
$ printf "%s %s %s %s %s %s %s %s\n" "${PH[@]}"
AD QD QC 5H 6C 8C 7D JH
3H 3S

This has the advantage over the above solutions that when your array becomes bigger you don't have to change it:
Code:
$ PH=(AD QD QC 5H 6C 8C 7D JH 3H 3S AF R3 T0)
$ printf "%s %s %s %s %s %s %s %s\n" "${PH[@]}"
AD QD QC 5H 6C 8C 7D JH
3H 3S AF R3 T0

cogiz, I noticed that your final solution was to limit to 24 fields. 24 %ss are a lot to type but I think it will be the most practical solution to your problem.

Andrew
This User Gave Thanks to apmcd47 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Output to file print as single line, not separate line

example of problem: when I echo "$e" >> /home/cogiz/file.txt result prints to file as:AA BB CC I need it to save to file as this:AA BB CC I know it's probably something really simple but any help would be greatly appreciated. Thank You. Cogiz (7 Replies)
Discussion started by: cogiz
7 Replies

2. Shell Programming and Scripting

convert single line output to multiple line

Hi all, I have a single line output like below echo $ips 10.26.208.28 10.26.208.26 10.26.208.27 want to convert above single line output as below format. Pls advice how to do ? 10.26.208.28 10.26.208.26 10.26.208.27 Regards Kannan (6 Replies)
Discussion started by: kamauv234
6 Replies

3. Shell Programming and Scripting

Joining multi-line output to a single line in a group

Hi, My Oracle query is returing below o/p ---------------------------------------------------------- Ins trnas value a lkp1 x a lkp1 y b lkp1 a b lkp2 x b lkp2 y ... (7 Replies)
Discussion started by: gvk25
7 Replies

4. Solaris

Limiting Connections from a single IP

I'm looking for a way to limit connections to a Solaris 10 box from any single IP. The problem is that I've had more experience doing this with IPTables on Linux, rather than with IPFilter, which I've found to be somewhat feature-poor. I hope there is some way to do this using IPFilter, I've... (2 Replies)
Discussion started by: spynappels
2 Replies

5. Shell Programming and Scripting

Merge multi-line output into a single line

Hello I did do a search and the past threads doesn't really solve my issue. (using various awk commands) I need to combine the output from java -version into 1 line, but I am having difficulties. When you exec java -version, you get: java version "1.5.0_06" Java(TM) 2 Runtime... (5 Replies)
Discussion started by: flagman5
5 Replies

6. Shell Programming and Scripting

Bash - Loading a command's output line by line into an array

I have been trying this a lot of different ways and haven't found too much online. Here's what I've got so far: j=0 declare -a first zero=(`cat $tmpfile`) for i in "${zero}" do command $i >> "${first}" ... (4 Replies)
Discussion started by: Azrael
4 Replies

7. Shell Programming and Scripting

Perl question - How do I print contents of an array on a single line?

I have the following code: print @testarray; which returns: 8 8 8 9 How do I return the array like this: The output is: 8, 8, 8, 9 (5 Replies)
Discussion started by: streetfighter2
5 Replies

8. Shell Programming and Scripting

BASH: print matrix from single array

I am creating a report in groff and need to format data from a file into a table cell. Sample data: dador,173323,bpt,jsp,39030013338878,1 dador,173323,brew,jsp,39030013338860,1 dador,173323,brew,jsp,39030013339447,1 dador,173323,brew,jsp,39030013339538,1 I would like to build a table... (12 Replies)
Discussion started by: Bubnoff
12 Replies

9. Shell Programming and Scripting

single line input to multiple line output with sed

hey gents, I'm working on something that will use snmpwalk to query the devices on my network and retreive the device name, device IP, device model and device serial. I'm using Nmap for the enumeration and sed to clean up the results for use by snmpwalk. Once i get all the data organized I'm... (8 Replies)
Discussion started by: mitch
8 Replies
Login or Register to Ask a Question