Get tokens from line and put them into an array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get tokens from line and put them into an array
# 1  
Old 09-20-2012
Get tokens from line and put them into an array

I have a line like:

Code:
IDNO H1 H2 H3 HT Q1 Q2 Q3 Q4 Q5 M1 M2 EXAM

I would like to be able to access these tokens in such a way:
echo $myline[1] #displays IDNO

Is this possible to do in a shell script?
# 2  
Old 09-20-2012
Code:
echo "IDNO H1 H2 H3 HT Q1 Q2 Q3 Q4 Q5 M1 M2 EXAM" | while read -a array
do
echo "${array[0]}"
done

This User Gave Thanks to msabhi For This Post:
# 3  
Old 09-20-2012
Code:
 
set -A array IDNO H1 H2 H3 HT Q1 Q2 Q3 Q4 Q5 M1 M2 EXAM
 
echo var 1     : ${array[0]}
echo array size: ${#array[@]}
echo array     : ${array[@]}

# 4  
Old 09-20-2012
Code:
line="IDNO H1 H2 H3 HT Q1 Q2 Q3 Q4 Q5 M1 M2 EXAM"
myline=( $line )
echo "${myline[0]}"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read file and get the data then put it in array

Hi, I have a file called "readfile" it contains below parameters #cat readfile word=/abc=225,/abc/cba=150 three=12 four=45 five=/xyz/yza likewise multiple line. From the above file, I have to read "word" output should be like, /abc /abc/cba these values need to be put in... (3 Replies)
Discussion started by: munna_dude
3 Replies

2. Shell Programming and Scripting

Perl : Large amount of data put into an array

This basic code works. I have a very long list, almost 10000 lines that I am building into the array. Each line has either 2 or 3 fields as shown in the code snippit. The array elements are static (for a few reasons that out of scope of this question) the list has to be "built in". It... (5 Replies)
Discussion started by: sumguy
5 Replies

3. Shell Programming and Scripting

How do I put data piped to my script into an array for repeated processing

Hi folks, Sorry for something I'm sure was answered already, I just could not find it in a search of the forums. I am trying to build a script that eats a config file as: cat file.cnf | ConProcess.shWhat I want to do inside the script is: !#/usr/bin/bash # captured piped cat into an... (6 Replies)
Discussion started by: Marc G
6 Replies

4. Shell Programming and Scripting

Extract specific text from variable and put it into array

Dear community, I have to do something too hard for me :rolleyes:. I hope you can help me. This is an output coming from Oracle query, stored in a file called query.out, there are many rows, but I read them, one by one, using while/read/done. Assuming each row is contained into $line variable... (8 Replies)
Discussion started by: Lord Spectre
8 Replies

5. Programming

put an 2D array in shared memory

Hi, I want to make 2 simple programs communicate each other with shared memory. The programs will share an 2D array. In the first program (intarr.c) i create an x array which is: 1 2 3 4 5 6 7 8 9 10 11 12 and i call the second program (intarr2.c) with execvp(); giving it the segment id as an... (2 Replies)
Discussion started by: giampoul
2 Replies

6. Programming

how to put element of an array to first position.

hi, I have a array like my $array = ( "apple","ball","cat","dog","elephant"); how to push some element in the array to the first position. for example my final array should be elephant apple ball cat dog (5 Replies)
Discussion started by: vprasads
5 Replies

7. Shell Programming and Scripting

perl, put one array into many array when field is equal to sth

Hi Everyone, #!/usr/bin/perl use strict; use warnings; my @test=("a;b;qqq;c;d","a;b;ggg;c;d","a;b;qqq;c;d"); would like to split the @test array into two array: @test1=(("a;b;qqq;c;d","a;b;qqq;c;d"); and @test2=("a;b;ggg;c;d"); means search for 3rd filed. Thanks find the... (0 Replies)
Discussion started by: jimmy_y
0 Replies

8. Shell Programming and Scripting

Put lines of a file in an array with awk

Hello, Is there any way in awk to put every line of a file in an array and so we can like this print the line we want. For example, if we have this file aaa eee bbb fff ccc ggg ddd hhh So we can print to the output the 3rd line only ccc ggg If it is possible, please put the... (7 Replies)
Discussion started by: rany1
7 Replies

9. Shell Programming and Scripting

Put output into an array

I'm trying to take the output of an environment that has multiple strings ex. # echo $SSH_CLIENT 192.168.1.1 57039 22 I need that IP... so I can set it to another environment. Thank you (3 Replies)
Discussion started by: adelsin
3 Replies

10. Shell Programming and Scripting

Removing tokens from cmd line

Hi everyone. I am trying to develop my own shell,and i am in the part of redirection. let's say the user gives as input cat test > test2 in the array of arguments i want to keep only arg=cat,arg=test. ">" token is not an input file so cat cannot worka and test2 is output.how can i remove > and... (1 Reply)
Discussion started by: bashuser2
1 Replies
Login or Register to Ask a Question