How to read multiple lines from Std Input into an array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to read multiple lines from Std Input into an array
# 1  
Old 03-04-2010
How to read multiple lines from Std Input into an array

Hi All,

Does anyone know how to read multiple lines from standard input into an array and then iterate a loop for all the lines read. Below is an example pseudocode:


I need the below filenames to be read by the script into an array or something similar:

Quote:
filename1
filename2
filename3
filename4
filename5

And then in the script, I would need to iterate for each file:

Code:
For each filename

Do processing of the current file

End For

Is there anyway to do the above in UNIX. Please help!
# 2  
Old 03-04-2010
This is a sample code
Code:
declare -a array[10]

for i in seq 1 3
do
read array[$i]
done

for i in seq 1 3
do
echo  -e "${array[$i]}\n " ;
done


Last edited by abubacker; 03-04-2010 at 12:49 AM..
# 3  
Old 03-04-2010
You try the following script.This script will read the filenames from user and print the content of filenames in stdout.

Code:
i=0
echo "Enter The number of filenames to read:"
read n
for((i=0;i<$n;i++))
do
echo -n "Enter Filename:"
read file
filenames[$i]=$file
done
for((i=0;i<$n;i++))
do
echo -e "--------------------------\n\tFILE:${filenames[$i]}\t\n--------------------------"
cat ${filenames[$i]}
done

# 4  
Old 03-04-2010
MySQL

To read from stdin just try the following way,After getting all the lines just perss ctrl+d to stop.

Code:
i=1
while read line
do
    array[ $i ]="$line"
    (( i++ ))
done
echo " --------------- "
j=1;
while [ $j -le $i ]
do
        echo ${array[ $j ]}
        let j=$j+1
done


Last edited by karthigayan; 03-04-2010 at 12:53 AM..
# 5  
Old 03-04-2010
Try this

Code:
#!/bin/sh
i=0;
while read file
do
        file_array[$i]=$file
        i=`expr $i + 1`
done
N=${#file_array[@]}

for (( j=0; j<N; j++))
do
        echo -e "\n\tFILE NAME : ${file_array[$j]}\t\n"
done

# 6  
Old 03-04-2010
Thanks all for your reply!

Ok, I think I left 1 requirement in my first post. I am going to get my filenames (inputs) thru email and there are going to be a lot of filenames (and NO constant count on number of files) which I would be passing to the script for processing.

I was thinking if I can just copy and paste all the filenames into the standard input and read it in my script thru an array/any-other way and process them each in a loop.

I have tried the above examples but in which I was not able to paste all the filenames at a single shot! When I do paste, after the first line since there is a return key in the list of filenames, the script breaks.

Please help me do this! Let me know if you have any questions!

---------- Post updated at 12:16 AM ---------- Previous update was at 12:10 AM ----------

Simply put, the read command, I believe, reads only one line from the Standard Input. So, I will end up giving my filename list one by one which I will not be able to do as the list is huge. Is there a way to read multiple lines from Std input at a single stretch?
# 7  
Old 03-04-2010
It will make more sense to paste the contents into a separate file and then read the file line by line in your script.

Code:
while read file
do
[your logic here]
done < inputfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing multiple lines from input file, if multiple lines match a pattern.

GM, I have an issue at work, which requires a simple solution. But, after multiple attempts, I have not been able to hit on the code needed. I am assuming that sed, awk or even perl could do what I need. I have an application that adds extra blank page feeds, for multiple reports, when... (7 Replies)
Discussion started by: jxfish2
7 Replies

2. Programming

Separate input buffer into multiple lines

Hi All I have a very tricky problem, not able to solve it. Hence asking this question. I have a code portion like this - int parse_msg(in_buf,line1,line2,sccs_line) char in_buf; char line1; char line2; char sccs_line; { .... (void)fprintf(trace_fp,"parse_msg1 in_buf = %s \n",... (0 Replies)
Discussion started by: nsinha
0 Replies

3. Shell Programming and Scripting

Read multiple input from CLI :ksh

Hi folks.. i got a requirement to red multiple directories from STDIN and store them to a variable. ex:- echo "Enter directory to add:" echo " Enter directory to add:" read value till there is input and when there is no input close the read loop and store variable into an array ... (1 Reply)
Discussion started by: bangaram
1 Replies

4. Shell Programming and Scripting

password file as std input to script

I'm a fairly new AIX admin (disclaimer). We have SQL scripts written by end users that use a userid and passwd to connect to our DB2 database. Is it possible to create an "input file" that contains the db2 connect parameters and yet secure the file from the SQL creator? i.e., they can "use"... (2 Replies)
Discussion started by: mpheine
2 Replies

5. Programming

How to accept multiple lines input from User in C?

Hi I want to accept multiple lines input with spaces from User and i have a working code like this. char sRes; char sReq; printf("Please enter request:"); scanf("%",sReq); /* Accept the input from user */ printf("\nPlease enter response:"); scanf("%",sRes); but the... (4 Replies)
Discussion started by: AAKhan
4 Replies

6. Shell Programming and Scripting

need shell or Perl script to read multiple input

I need shell 0r Perl script to read multiple input and do something and come out example: echo “ enter the host names separated by space “ read servers foreach @servers { do do something done} Here host names like host1 host2 host3 . . . . . . . so on Please help me... (8 Replies)
Discussion started by: sreedhargouda
8 Replies

7. Shell Programming and Scripting

Read files, lines into array, cat vs open

Hi Everyone, I have a file: a.txt a,b,c,d,6,6,6 1,2,3,d,6,6,6 4,5,6,6,6,6,6 #!/usr/bin/perl use warnings; use strict; my @array = (); ### Load file into array for my $i (split '\n', `cat /tmp/a.txt`) { push @array, ; } It works. But my a.txt have 1million lines, and... (2 Replies)
Discussion started by: jimmy_y
2 Replies

8. Shell Programming and Scripting

PDKSH dual std input threads

Hi All, I'm trying to read from two files at the same time, but the second READ is failing, giving no value. Obvious STDIN is being used by the first "while read", so how can I retrieve a value from a second file within the loop ?? IFS=" ," cat $DATAFILE | while read curdate currentcksum... (3 Replies)
Discussion started by: adrianmarsh
3 Replies

9. Shell Programming and Scripting

read from std i/p with timeout within a script

hello every one , this is my first participation in the forum , I hope it'll be a good start within a script I would like to put some code to read i\p from standard i\p using read command if it reads Y it will terminate the script if it reads N it will continue execution , if no i\p is... (2 Replies)
Discussion started by: Blue_shadow
2 Replies

10. Programming

Sun Studio C++ - Getting error in linking std::ostream &std::ostream::operator<<(std:

Hello all Im using CC: Sun C++ 5.6 2004/07/15 and using the -library=stlport4 when linkning im getting The fallowing error : Undefined first referenced symbol in file std::ostream &std::ostream::operator<<(std::ios_base&(*)(std::ios_base&))... (0 Replies)
Discussion started by: umen
0 Replies
Login or Register to Ask a Question