Operating System - HP-UX
1830898 Members
3070 Online
110017 Solutions
New Discussion

loop to add line to file list

 
Declan Heerey
Frequent Advisor

loop to add line to file list

I have the following list of files in a directory;

2 instrument.sql
4 parameter(data).sql
1 programs.sql

when i try to add a blank line to the end of these file with

for i in `ls *`;do echo "" >> $i; done

i get the following files;

2
2 instrument.sql
4
4 parameter(data).sql
1
1 programs.sql

Can anyone tell me how to do this correctly??

Thanks in advance - Declan
8 REPLIES 8
Peter Godron
Honored Contributor

Re: loop to add line to file list

Decla,
works for me!
Created file a.lis:
1
2
then ran your command and got:
1
2

So the line was added!
I assume the numbers in front of the filenames come from ll?
Can you do a ls -b ?
(Hidden files/control characters ????)
Regards
BONNAFOUS Jean Marc
Trusted Contributor

Re: loop to add line to file list

Hi,

Have you blank in file names?

Try:

for i in `ls`
do
echo "" >> "$i"
done

Rgds
JMB
Si vous ne faites jamais de bétises, c'est que vous ne faites rien de difficile. Et ça c'est une grosse bétise.
Stephen Keane
Honored Contributor

Re: loop to add line to file list

ls * | while read i; do echo "" >> $i; done
Declan Heerey
Frequent Advisor

Re: loop to add line to file list

Thanks Stephen (et al) the while loop looks to work a treat............cheers
Peter Godron
Honored Contributor

Re: loop to add line to file list

Declan,
were was the problem then?
In the for loop or the filenames?
Regards
Declan Heerey
Frequent Advisor

Re: loop to add line to file list

Peter i think so, i amended Stephen's syntax slightly quoting the $i and it worked i.e.

ls * | while read i; do echo "" >> "$i"; done

not sure this wouldn't work for original loop though
Stephen Keane
Honored Contributor

Re: loop to add line to file list

In the original loop, because the file names contain spaces, the do list gets
2, instrument.sql, 4, parameter(data).sql, 1, programs.sql

i.e. 6 arguments, when in fact you wanted only 3 arguments "2 instrument.sql", "4 parameter(data).sql" and "1 programs.sql"

apart from stating the obvious (i.e. don't use spaces in file names), the while/read construct sees each argument from the ls as a separate line, rather than as separate strings separated by whitespace.



Declan Heerey
Frequent Advisor

Re: loop to add line to file list

Thanks Stephen that clears a lot up!

Cheers