How to read a multiple lines from a file n executing them?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to read a multiple lines from a file n executing them?
# 1  
Old 12-17-2010
Bug How to read a multiple lines from a file n executing them?

Hi all,
I am just trying to read the contents of a file. basically this file has a list of dat files. then i want to access these dat files n execute a script on them one by one using a loop.

i hav e written like this

Code:
ls -l | cut -c 58-88 > file1.txt
while [ $j -lt 928 ]
do
 
arr1[j]="$( sed -n '1p' file1.txt )"
con.ksh ${arr1[j]};
j=`expr $j + 1`
 
echo "working"
done

Smilie
Please help me out.

edit by scottn: Moved from AIX sub-forum (it's not an AIX-specific question

Last edited by Scott; 12-17-2010 at 08:12 AM.. Reason: Replace QUOTE tags with CODE tags
# 2  
Old 12-17-2010
If we don't want file names to be in a file
Code:
 
ls -1 | while read file
do
   con.ksh $file
done

OR

Code:
 
ls -1 > file.txt
while read file
do
  con.ksh $file
done < file.txt

# 3  
Old 12-17-2010
Thanx anurag 4 ur help.
No basically I am trying to delete the control M characters from around 100 files in a directory.I am using the con.ksh script to delete cntrlM characters in a single file. But how to do it in a loop.
I have tried the following script too. but its not working also

Quote:
j=0
ls -l | cut -c 58-88 > file1.txt
cat file1.txt | while read a
do
echo "value of a $a"
arr1[j]=$a
convert.ksh ${arr1[j]}
j=`expr $j + 1`
echo "working"
done
If u can help me out ..

Regards
# 4  
Old 12-17-2010
It looks like you are just passing all files names to convert.ksh script one by one in a loop. Here variables a and arr1[j] have file name value in it.
If so, same is happening in my script. each file ($file variable) is being passed to con.ksh (change it to convert.ksh).
Code:
ls -1 | while read file
do
   echo "processing file: $file"
   convert.ksh $file
done

# 5  
Old 12-20-2010
But the issue is not resolved.
I think you could not get it. I am trying to pass the filenames but those filenames are the files of a directory. So i am trying to extract the files names only by using

ls -l | cut -c 58-88 > file1.txt .

Now this file1.txt contains all the filenames only in column manner.
Now I am trying to execute the files by selecting the names of the files from file1.txt.

Any ways thanks for ur support..
# 6  
Old 12-20-2010
Code:
 
ls | while read file
do
   if [ -f $file ]; then
        echo "processing file: $file"
        convert.ksh $file
   fi
done

Here all files under current dir will be passed to convert.ksh one by one.
If condition can be removed if current dir contains only FILES, not subdirectories.
This User Gave Thanks to anurag.singh For This Post:
# 7  
Old 12-20-2010
anurag.singh is correct in what is said. The question we have to be clear on is why you want to
Code:
ls -l|cut -c 58-88 > file1.txt

I assume that this is so you get just the names of the files from an ls -l, but this is just the same as output from ls sent to a file or ls -1 (flag is minus one) to be used in a loop as described.

So, what are you trying to get? If it is just files from a directory, you might also confuse yourself if you write the temporary file there too. Editing the file with con.ksh mid program when that is the input file might have unpredictable results too.

You also run the risk of having a file that is longer than the 31 characters you are limiting this to. If you have a file called this_is_a_very_long_name_for_a_file, then you would try to run
Code:
con.ksh this_is_a_very_long_name_for_a_

and presumably get an error meaning file not found.

Another way to approach this is to use the find command. As such, you can exclude links, directories, pipe files etc. by specifying you only want regular files:
Code:
find . -type f -exec con.ksh {} \;

This looks in the current directory (the dot) for a regular files (-type f) and then executes con.ksh with each as an arguement (-exec con.ksh {} \Smilie The trailing \; marks the end of the -exec part of the command and the find will error if it is missing.



I hope that this is useful, but please write back if this I have missed the point.


Robin
Liverpool/Blackburn
UK
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. Shell Programming and Scripting

Read multiple lines at a time from file

Hello All, I have a file like below.... dn: cn=user1,ou=org,o=org cn=user1 uid=user1 cn=user2,ou=org,o=org cn=user2 uid=user2 cn=user3,ou=org,o=org cn=user3 cn=user33 uid=user3 cn=user4,ou=org,o=org cn=user4 uid=user4 (6 Replies)
Discussion started by: s_linux
6 Replies

3. Shell Programming and Scripting

Executing multiple commands in a file at same time

Hi Am having file.ksh as below wc -l file1.txt wc -l file2.txt wc -l file3.txt wc -l file4.txt i want all the commands in this file to execute in same time please help Thanks in advance (1 Reply)
Discussion started by: ragu.selvaraj
1 Replies

4. Shell Programming and Scripting

read the lines of multiple files

I am trying to create a script which will read 2 files and use the lines of file 1 for each line on file 2. here's my sample code cat $SBox | while read line do cat $Date | while read line do $SCRIPTEXE <line from first file> $2 <line from 2nd file> ... (12 Replies)
Discussion started by: khestoi
12 Replies

5. Shell Programming and Scripting

Awk match multiple columns in multiple lines in single file

Hi, Input 7488 7389 chr1.fa chr1.fa 3546 9887 chr5.fa chr9.fa 7387 7898 chrX.fa chr3.fa 7488 7389 chr21.fa chr3.fa 7488 7389 chr1.fa chr1.fa 3546 9887 chr9.fa chr5.fa 7898 7387 chrX.fa chr3.fa Desired Output 7488 7389 chr1.fa chr1.fa 2 3546 9887 chr5.fa chr9.fa 2... (2 Replies)
Discussion started by: jacobs.smith
2 Replies

6. Shell Programming and Scripting

read one line file and separate into multiple lines

I have one long line text with semicolon used as separator between values in that line. Now, I want to separate the line into multiple line right after every 29th field. example input line: ... (1 Reply)
Discussion started by: erlanq
1 Replies

7. Shell Programming and Scripting

read one line file and separate into multiple lines

I have one long line text with semicolon used as separator between values in that line. Now, I want to separate the line into multiple line right after every 29th field. example input line: ... (2 Replies)
Discussion started by: erlanq
2 Replies

8. UNIX for Dummies Questions & Answers

When reading a csv file, counter to read 20 lines and wait for minute then read next 20 till end

Hello All, i am a newbie and need some help when reading a csv file in a bourne shell script. I want to read 10 lines, then wait for a minute and then do a reading of another 10 lines and so on in the same way. I want to do this till the end of file. Any inputs are appreciated ... (3 Replies)
Discussion started by: victor.s
3 Replies

9. Shell Programming and Scripting

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: And then in the script, I... (9 Replies)
Discussion started by: bharath.gct
9 Replies

10. Shell Programming and Scripting

read and match multiple lines in perl

Could any one tell me how to read and match multiple lines in perl? Did this code below still work in this situation? while (<FILE>) { if (/ /) { } } Thanks a lot! (5 Replies)
Discussion started by: zx1106
5 Replies
Login or Register to Ask a Question