How can I print array elements as different columns in bash?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How can I print array elements as different columns in bash?
# 1  
Old 02-09-2010
How can I print array elements as different columns in bash?

Hi Guys,

I have an array which has numbers including blanks as follows:

Code:
1
26
66
4.77
-0.58
88
99
 
11
12
333

I want to print a group of three elements as a different column in a file as followsSmilieincluding blanks where there is missing elements) for.e.g. array element #7 is a blank.

Code:
1 4.77 99 12
26 -0.58 333  
66 88 11

Thanks.
# 2  
Old 02-10-2010
Code:
awk '{_[NR%3]=_[NR%3]?_[NR%3] FS$0:$0}END{for(i in _)print _[i]}' infile

For anything else please post on special homework subforum.
# 3  
Old 02-10-2010
Thanks, it's not homework. It's part of a bigger program.
# 4  
Old 02-10-2010
danmero solution will not guarantee the o/p order use below:-

Code:
awk '{a[NR%3]=a[NR%3]" "$0}
END{for (i=1;i<=3;i++) {print o= (i!=3)? a[i] : a[0] } }' infile.txt

or

Code:
awk '((NR%3) == 1){s=s"\t"$0 }((NR%3) == 2){b=b"\t"$0}!(NR%3)
{c=c"\t"$0}END{print s"\n"b"\n"c}' infile.txt

SmilieSmilieSmilie
# 5  
Old 02-10-2010
Quote:
Originally Posted by ahmad.diab
danmero solution will not guarantee the o/p order use below:-
Right Smilie, let's see now:
Code:
awk 'END{for(i=0;++i<=X;){print (i==X)?a[0]:a[i]}}{a[NR%X]=a[NR%X]?a[NR%X]FS$0:$0}' X=3 infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Inaccurate scanning of Bash array elements

username=cogiz #!/bin/bash shuffle() #@ USAGE: shuffle { #@ TODO: add options for multiple or partial decks Deck=$( printf "%s\n" {2,3,4,5,6,7,8,9,T,J,Q,K,A}{H,S,D,C} | awk '## Seed the random number generator BEGIN { srand() } ## Put a random number in front... (4 Replies)
Discussion started by: cogiz
4 Replies

2. UNIX for Beginners Questions & Answers

Random choice of elements in bash array

example of problem: #!/bin/bash P=(2 4 7) How would you randomly choose one of these 3 numbers in this array? either 2 or 4 or 7 is needed...but only one of them. Thanks in advance Cogiz Please use CODE tags as required by forum rules! (3 Replies)
Discussion started by: cogiz
3 Replies

3. UNIX for Beginners Questions & Answers

Scanning array for partial elements in Bash Script

Example of problem: computerhand=(6H 2C JC QS 9D 3H 8H 4D) topcard=6D How do you search ${computerhand} for all elements containing either a "6" or a "D" then save the output to a file? This is a part of a Terminal game of Crazy 8's that I'm attempting to write in Bash. Any... (2 Replies)
Discussion started by: cogiz
2 Replies

4. Shell Programming and Scripting

Select / Print odd-even elements of an array

Hello experts, I wish to print the contents of odd-even numbered indices of an array. The problem statement is as follows : 1. The first line contains an integer, (the number of test cases). 2. Each line of the subsequent lines containing a string. Example: 2 Haider Bash ... (4 Replies)
Discussion started by: H squared
4 Replies

5. Shell Programming and Scripting

Help reading the array and sum of the array elements

Hi All, need help with reading the array and sum of the array elements. given an array of integers of size N . You need to print the sum of the elements in the array, keeping in mind that some of those integers may be quite large. Input Format The first line of the input consists of an... (1 Reply)
Discussion started by: nishantrefound
1 Replies

6. UNIX for Dummies Questions & Answers

How to declare an array in UNIX and print the elements with tab delimits?

Hello, In a shell script, I want to declare an array and subsequently print the elements with tab delimits. My array has the following structure and arbitrary elements: myArray=('fgh' 'ijk' 'xyz' 'abc'); Next, I would like to print it with a '\n' at the end. Thanks for your input! ... (2 Replies)
Discussion started by: Gussifinknottle
2 Replies

7. Shell Programming and Scripting

How to print elements between letters bash script?

Hello to all, May somebody help me to fix my bash code below, I'd like to do it only using loops and if statements is possible. I the content of the arrays are represented by the following 2 strings, where each character is an element inside the array. String for case 1:... (10 Replies)
Discussion started by: Ophiuchus
10 Replies

8. Shell Programming and Scripting

Print arguments using array elements not working?

Hey guys, I'm new to shell scripting and I'm trying to write a script that takes user input and copies the specified columns from a data file to a new one. In order to account for the possibility of a variable number of columns to copy I wrote a loop that encodes the user's choices in an array... (16 Replies)
Discussion started by: shaner
16 Replies

9. 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

10. UNIX for Dummies Questions & Answers

How can i read array elements dynamically in bash?

Hi friends how can we read array elements dynamically in bash shell? (1 Reply)
Discussion started by: haisubbu
1 Replies
Login or Register to Ask a Question