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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How do I put data piped to my script into an array for repeated processing
# 1  
Old 10-25-2012
[Solved] 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:
Code:
cat file.cnf  | ConProcess.sh

What I want to do inside the script is:
Code:
!#/usr/bin/bash

# captured piped cat into an array

# Offer menu of options

# For each line
    do processing per line
# end loop

# print report

But I am not sure how to receive the piped data and put it in an array

Thanks for any help you can provide

Marc

Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by radoulov; 10-25-2012 at 03:34 PM.. Reason: Marked as solved.
# 2  
Old 10-25-2012
Suggestion:-

Instead of piping the file.cnf to Conprocess.sh, you can open this config file in Conprocess.sh using a while loop and read line by line:-

Code:
!#/usr/bin/bash

cat file.cnf | while read line 
do 

# Perform operation based on $line variable value

done 

# print report

# 3  
Old 10-25-2012
That is a useless use of cat and particularly harmful here since it will put the while loop in a subshell that's unable to alter variables in the original shell.

Just read the file directly to avoid these problems.

Code:
while read LINE
do
...
done < inputfile

This User Gave Thanks to Corona688 For This Post:
# 4  
Old 10-25-2012
Ah, sorry. I was not clear.
I also want this to be able to handle items on the fly.

For example: ls -ltr | ConProcess.sh
or: history | grep -v history | awk '{print $3}' | ConProcess.sh

So as I go, there will be an increasing menu of time saver procs in the script

Sorry for not being more clear the first go

Last edited by radoulov; 10-25-2012 at 03:32 PM..
# 5  
Old 10-25-2012
Code:
while read -a array
do
process whatever you want
done < input.config

# 6  
Old 10-25-2012
example:

file1:
Code:
123123
aaaaaa

conProcess.sh
Code:
cat - | while read line
do
  echo Processing line: $line
done

so:
Code:
cat file1 | ConProcess.sh
Processing line: 123123
Processing line: aaaaaa

# 7  
Old 10-25-2012
ok..

While none of these worked as I had hoped, I did use the input you guys gave to find the answer..

the code
Code:
while read line
do
 data=("${data[@]}" $line)
done

allowed me to independently use the data in the array for commands like

Code:
echo ${#data[@]}

(which I used for input verification)

and other processes.

Thanks for helping lead this horse to water Smilie

Marc
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Put repeated values into a table

Hi all, I have blocks of records like the following, each block ends in = in a new line, I want tabularize the entire output. The pattern is the same in every block although not all types are there in every block. For example gine3 is absent in the second block but present first and third. ... (7 Replies)
Discussion started by: ritakadm
7 Replies

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

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

4. Shell Programming and Scripting

Perl script required for processing the data

I have following result.log file (always has 2 lines) which I need to process, cat result.log name.cmd.method,"result","abc","xyz"; name="hello,mine.12345,"&"tree"&" xyz "&" tree "&" xyz", data="way,"&" 1"&"rate-me"&"1"&"rate-me",str="",ret=""; now I need to extract the strings/data as... (4 Replies)
Discussion started by: perlDiva
4 Replies

5. UNIX for Advanced & Expert Users

put data in excel file using shell script

Hi. I wish to add data in a specific excel file on daily basis.However the currect dat's data should always come on top i.e for example should always occupy cell A7,B7,C7 .. and the data of day before which was earlier on 7th row of each coloumn should move to 8th row..data on 8th row should... (1 Reply)
Discussion started by: kanus
1 Replies

6. Shell Programming and Scripting

how to put data using shell script to a excel file

Hi, Can any one tell me how to put data using shell script to a excel file from text file to other columns of excel file,leaving first column unaffected i.e it should not overwrite data in first column. Say my text file data is: 15-dec-2008 15-dec-2009 16-dec-2008 16-dec-2009 say my first... (1 Reply)
Discussion started by: siri_886
1 Replies

7. Shell Programming and Scripting

How to put db2 query result into an array in shell script?

Hello, Can someone please advise me how to put the db2 query reult into an array? For example, the query reults are: string A string B string C Then how do I put them into array=string A array=string B ... (2 Replies)
Discussion started by: hanul
2 Replies

8. Shell Programming and Scripting

awk script processing data from 2 files

Hi! I have 2 files containing data that I need to process at the same time, I have problems in reading a different number of lines from the different files. Here is an explanation of what I need to do (possibly with an awk script). File "samples.txt" contains data in the format: time_instant... (6 Replies)
Discussion started by: Alice236
6 Replies

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

10. Shell Programming and Scripting

Script - Filter data - repeated loop - Help needed...

Dear Friend, I need a help for the below problem.. Can anyone suggest me to do... Input file data: rule { name=mainrule filt=pos loc { x=right + 660 y=top - 3100 } object_kind= DRAW ... (15 Replies)
Discussion started by: vasanth_vadalur
15 Replies
Login or Register to Ask a Question