Shell script to read words into an array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script to read words into an array
# 1  
Old 06-05-2014
Shell script to read words into an array

Hello,

I have output like below:

Code:
-----------------------------------------------------------------------------
Group 'group1' on system 'system01' is running.
-----------------------------------------------------------------------------
Group 'group2' on system 'system01' is running.
-----------------------------------------------------------------------------
Group 'group3' on system 'system01' is running.
-----------------------------------------------------------------------------
Group 'group4' on system 'system01' is running.
-----------------------------------------------------------------------------

I want to be able to read all the group names (between the single quotes) and put them into an array for future use.

Can someone please give me some pointers?!!

Many thanks

Last edited by Scrutinizer; 06-05-2014 at 07:38 AM.. Reason: code tags
# 2  
Old 06-05-2014
Code:
 
declare -a arr=($(awk -v q="'" 'BEGIN{FS = q} NF > 1 {print $2}' file))

# 3  
Old 06-05-2014
A few questions to start:-
  • What have you tried so far?
  • What output/errors are you getting?
  • What OS name and version are you using?
  • What is your shell or other tools that you want to do this in?
Most importantly, What have you tried so far?

This will help us understand where you are stuck and advise a way forward so you develop what you actually want in a way that makes sense to you and you can support/adjust in the future. There are usually many ways to do most things, so knowing what is best for you helps everyone focus.


Thanks, in advance,
Robin
# 4  
Old 06-05-2014
Facebook

You did not mention what kind of array you would like to create, with some assumption you may try like this

Code:
$ cat file
Group 'group1' on system 'system01' is running.
-----------------------------------------------------------------------------
Group 'group2' on system 'system01' is running.
-----------------------------------------------------------------------------
Group 'group3' on system 'system01' is running.
-----------------------------------------------------------------------------
Group 'group4' on system 'system01' is running.
-----------------------------------------------------------------------------

Code:
$ cat tester
#!/bin/bash

# Declare array
declare -A array

# row counter
row=0;

# Read file skip other fields so x or anything..
while read  x c1 x x c2 x; 

	do [ -z "$x" ] && continue; 

	# see what you are storing 
	echo $c1 $c2 $row; 

	# store whatever you want...removing single quotes 
	array["$row","group"]="${c1:1:${#c1}-2}"; 
	array["$row","system"]="${c2:1:${#c2}-2}"; 

        # Or you can remove like this ${c1//\'/}
       
 
	# increment counter..
	((++row))

done < "file"; 

# Access array for future use..
for ((i = 0; i < row; ++i)); do 
	echo ${array["$i","group"]} ${array["$i","system"]}; 
done

Code:
$ bash tester
'group1' 'system01' 0
'group2' 'system01' 1
'group3' 'system01' 2
'group4' 'system01' 3
group1 system01
group2 system01
group3 system01
group4 system01

# 5  
Old 06-05-2014
For your sample file, and using bash, this might do:
Code:
ARR=( $(while IFS="'" read A B REST; do printf "%s " $B; done < file4 ))
echo ${#ARR[@]}
4
echo ${ARR[@]}
group1 group2 group3 group4
echo ${ARR[2]}
group3

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

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to pass and read an array in ksh shell script function.?

I'm able to read & print an array in varaible called "filelist" I need to pass this array variable to a function called verify() and then read and loop through the passed array inside the function. Unfortunately it does not print the entire array from inside the funstion's loop. #/bin/ksh... (5 Replies)
Discussion started by: mohtashims
5 Replies

2. UNIX for Beginners Questions & Answers

Shell - Read a text file with two words and extract data

hi I made this simple script to extract data and pretty much is a list and would like to extract data of two words separated by commas and I would like to make a new text file that would list these extracted data into a list and each in a new line. Example that worked for me with text file... (5 Replies)
Discussion started by: dandaryll
5 Replies

3. Shell Programming and Scripting

Pass C shell array to another C shell script(csh) and shell(sh)

Dear Friends, Please help me on this my script name is send.csh In this i have written the statement like this set args = ( city state country price ) I want to pass this array to another c shell called receiver.csh. and i want to use it in this c shell or how to pass to... (2 Replies)
Discussion started by: SA_Palani
2 Replies

4. UNIX and Linux Applications

Perl Script to read an excel file into an array and search in the UNIX directories

Hi, I want the Perl script with versions 5.8.2 and 5.8.5 starting with #!/usr/bin/perl The Perl program should read the excel file or text file line by line and taking into an array and search in the UNIX directories for reference file of .jsp or .js or .xsl with path .The Object names... (2 Replies)
Discussion started by: pasam
2 Replies

5. Shell Programming and Scripting

PERL : Read an array and write to another array with intial string pattern checks

I have an array and two variables as below, I need to check if $datevar is present in $filename. If so, i need to replace $filename with the values in the array. I need the output inside an ARRAY How can this be done. Any help will be appreciated. Thanks in advance. (2 Replies)
Discussion started by: irudayaraj
2 Replies

6. Shell Programming and Scripting

shell script to print words having first and last character same.

Hi I want to write a shell script to print only those words from a file whose beginning and last character are same. Please help. Thanks, vini (5 Replies)
Discussion started by: vini kumar
5 Replies

7. Shell Programming and Scripting

Shell script to find out words, replace them and count words

hello, i 'd like your help about a bash script which: 1. finds inside the html file (it is attached with my post) the code number of the Latest Stable Kernel, 2.finds the link which leads to the download location of the Latest Stable Kernel version, (the right link should lead to the file... (3 Replies)
Discussion started by: alex83
3 Replies

8. Shell Programming and Scripting

To read and separate number and words in file and store to two new file using shell

hi, I am a begginer in unix and i want to know how to open a file and read it and separate the numbers & words and storing it in separate files, Using shell scripting. Please help me out for this. Regards S.Kamakshi (2 Replies)
Discussion started by: kamakshi s
2 Replies

9. Shell Programming and Scripting

script to Read Error,jbase,voilation,segmentation words from a file

Hi, i need a script to find words "error,jbase,voilation,segmentation" from a file(verify.txt) and if got any of the above words then send an email and if wont find any then execute the cron job. every time it read file verify.txt it must first clear the previous data in the file . Kindly... (3 Replies)
Discussion started by: Mujtaba khan
3 Replies

10. Shell Programming and Scripting

Read words from file and create new file using K-shell.

Hi All, Please help me in creating files through K-shell scripts. I am having one file in this format. OWNER.TABLE_NAME OWNER.TABLE_NAME1 OWNER1.TABLE_NAME OWNER1.TABLE_NAME1 I want to read the above file and create new file through k shell script. The new file should looks like this.... (4 Replies)
Discussion started by: bsrajirs
4 Replies
Login or Register to Ask a Question