parse a file and fill an array with it


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting parse a file and fill an array with it
# 1  
Old 05-06-2008
parse a file and fill an array with it

Hi Guys,

I am trying to do a file parse which is something like

config file:
machines= sha1 sha2 sha3 sha4

The bash script should be supporting upto 64 such machines
what I want is to place the machines in an array and then use the array to ssh to each machine.

The script I worte

declare -x machine
x=$(gawk -F ' ' '/^machine/ {print NF}' a)
i=1

while [ $i -lt $x ]; do
machine=$(gawk 'BEGIN {FS=" "} ; /^machine/ && NF <= 64 {print $i}' a)
#do the SSH here
let "i+=1"
echo mach $machine
done

The script always prints the entire line instead of indivudual word.
Also, it only works if there a single entry of 'machine'

Can some please help in this.

Cheers,
nani
# 2  
Old 05-06-2008
You can try something like this:

Code:
awk '{for(i=2;i<=NF;i++){print $NF}}' "config.file" |
while read mach; do
  #do your stuff here
  echo "$mach"
done

Regards
# 3  
Old 05-06-2008
RE: parse a file and fill an array with it

Hi Franklin,
I tried to run by changing the config file to my config as mentioned erlier.
It prints onyl the last field (word) in the config file 3 times and does not fill anything else apart form it.

I did not understand why r u looping starting form '2'.
However, I changed it to 0 and 1 and did not get the expected result.

Regards,
Mukund Jampala

Quote:
Originally Posted by Franklin52
You can try something like this:

Code:
awk '{for(i=2;i<=NF;i++){print $NF}}' "config.file" |
while read mach; do
  #do your stuff here
  echo "$mach"
done

Regards
# 4  
Old 05-06-2008
Looks like he had a typo in his code; try changing print $NF to print $i.
# 5  
Old 05-07-2008
perhaps like this

awk '{print $1}' /tmp/config.txt|while read output;
do
machine=$(echo $output | awk '{ print $1}')
#your script here
echo $machine
done

config.txt is the machine list file
hope that help
# 6  
Old 05-07-2008
Like Annihilannic mentioned, I had a typo in the code, and the loop begins with the second field after the "=" so I assume this is the first machine name. Maybe it should begins with the 3th field, try it out.

Code:
awk '{for(i=2;i<=NF;i++){print $i}}' "config.file" |
while read mach; do
  #do your stuff here
  echo "$mach"
done

Regards
# 7  
Old 05-07-2008
RE: parse a file and fill an array with it

Hi Franklin,

Thanks you, I got it working.
But, I want these details in a array with extra spaces removed.

However, I tried by adding a ssh command to it and does not work
When I do this the loop gets restricted to a single iteration. ?I did not see the second iteration happening. and it happens only the case of ssh.

Instead local commands works fine.

awk '{for(i=2;i<=NF;i++){print $i}}' "a" |
while read mach; do
#do your stuff here
echo "hello $mach"
ssh root@rna4 uname -n # <----
echo $?
done

Regards,
Mukund Jampala


Quote:
Originally Posted by Franklin52
Like Annihilannic mentioned, I had a typo in the code, and the loop begins with the second field after the "=" so I assume this is the first machine name. Maybe it should begins with the 3th field, try it out.

Code:
awk '{for(i=2;i<=NF;i++){print $i}}' "config.file" |
while read mach; do
  #do your stuff here
  echo "$mach"
done

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to parse section of csv into array

In the awk below I am trying to parse the Sample Name below the section. The values that are extracted are read into array s(each value in a row seperated by a space) which will be used later in a bash script. The awk does execute but no values are printed. I am also not sure how to print in a row... (1 Reply)
Discussion started by: cmccabe
1 Replies

2. Shell Programming and Scripting

Perl script to fill the entire row of Excel file with color based on pattern match

Hi All , I have to write one Perl script in which I need to read one pre-existing xls and based on pattern match for one word in some cells of the XLS , I need to fill the entire row with one color of that matched cell and write the content to another excel Please find the below stated... (2 Replies)
Discussion started by: kshitij
2 Replies

3. UNIX for Dummies Questions & Answers

Fill csv entire column with content from another text file

I have a csv that looks like this: ,yude-to-nap2,0,0,0,0,0 ,2twis-yude-to-nap2,0,0,0,0,0 ,2tiws-yude-to-nap2,0,0,0,0,0 ,2arcos-yude-to-nap2,0,0,0,0,0 and another file named m1 that has a single line of text as content: Feb 1 15:30:20 How can I fill the whole the empty column of the... (1 Reply)
Discussion started by: RobertoRivera
1 Replies

4. Shell Programming and Scripting

Parse find input into array

I need help parsing the output of find into an array. I need to search 3 directories and find all files older than 31 days old. This is what I have so far. TIME=" -maxdepth 1 -mtime +31" DIR1="/dir1/" DIR2="/dir2/" DIR3="/dir3/" FIND_DIR1=$(find ${DIR1}${TIME}) FIND_DIR3=$(find... (8 Replies)
Discussion started by: jrymer
8 Replies

5. Emergency UNIX and Linux Support

Script to fill the file system mount with empty files to desired size

We are regularly using for our testing, where we are manually filling up the mount with desired size with following command dd if=/dev/zero of=file_2GB bs=2048000 count=2000 We are planning to automate the task where taking input for % of size as one input and the name of the file system... (8 Replies)
Discussion started by: chandu123
8 Replies

6. Shell Programming and Scripting

Script which fill data in XML file

Hello, I need help for writing a script that fills already generated xml file with data from oracle database and random sequences. For example if we have the following tags: <ns1:message> <ns1:messageId> </ns1:messageId> <ns1:languageCode> </ns1:languageCode>... (10 Replies)
Discussion started by: zb99
10 Replies

7. UNIX for Dummies Questions & Answers

Fill fields with awk from an array?

Hi experts, I have been trying for a while to accomplish the following task using awk, and I just don't seem find find a way. I am not particular about using awk, it just seemed like the logical choice at first. I have a file that contains 5 fields that are delimited by a space character.... (1 Reply)
Discussion started by: GermanicGalore
1 Replies

8. Shell Programming and Scripting

How to fill data from other file and get some output

Greetings, I have a hard time creating a large number of user profiles in a database. The data file looks like this : 01/01/80 Mitch Conley . . . . And I need to put the output into: Name: Mitch Surname: Conley Birthday: 01/01/80 Thanks in advance! (3 Replies)
Discussion started by: hemo21
3 Replies

9. Shell Programming and Scripting

Need to parse file "x" lines at a time ... awk array?

I have files that store multiple data points for the same device "vertically" and include multiple devices. It repeats a consistant pattern of lines where for each line: Column 1 is a common number for the entire file and all devices in that file Column 2 is a unique device number Column 3 is... (7 Replies)
Discussion started by: STN
7 Replies

10. UNIX for Dummies Questions & Answers

Read directory and fill a file with this information

Hello All, Got a question to make a script what reads a directory and put the file names from that directory in a file with some extra text. ls /tempdir output is: file1.gfh file2.txt file4.zzz these file names (always with an extention) must be placed in a line and written to... (2 Replies)
Discussion started by: ToXiQ
2 Replies
Login or Register to Ask a Question