# sh for + sed or awk or...



## graudeejs (Jan 3, 2009)

here's my problem, that i can't solve on my own

i have some directory "/some/stupid directory"
and i have some files "/some/stupid directory/file 1" and "/some/stupid directory/file 2"

i'm using

```
for fileName in `ls "/some/stupid directory/"`; do
  echo "*** file: $1/$fileName"
done
```
it give wrong output [which is as it should be in this case]:

```
*** file: /some/stupid directory/file
*** file: /some/stupid directory/1
*** file: /some/stupid directory/file
*** file: /some/stupid directory/2
```

if i try 

```
for fileName in "`ls "/some/stupid directory/""`; do
  echo "*** file: $1/$fileName"
done
```
i get:

```
*** file: /some/stupid directory/file 1
/some/stupid directory/file 2
```
which is not what i wan, but it correct from point of sh view


I want to add doulb (or single) equote to each line ls outputs, before it's passed to for. I tried awk, but i'm noob in this, and failed many times....

i would like correct output to be:

```
*** file: /some/stupid directory/file 1
*** file: /some/stupid directory/file 2
```


I hope I gave good enough examples, and you did understand what i'm trying to do, if not let me know, i'll try to explain better.

Removing spaces from files with my Win****.sh script can't be used here, because this script will operate on files that i'm downloading/uploading with torrents

Thanks in advance


----------



## DutchDaemon (Jan 3, 2009)

Have you considered using while instead of for?


```
for Dir in /some/dir1 /some/dir2 /some/dir3
do 
ls $Dir | while read fileName
do 
echo "*** file: $Dir/$fileName"
done
done
```


----------



## graudeejs (Jan 3, 2009)

Thank you, friend....
this worked...
i preciously never used while, your great peace of code opens grand new perspective for me in sh 

one little suggestion to you:
better double quote $Dir when you ls


```
for Dir in /some/dir1 /some/dir2 /some/dir3
do 
ls [red]"[/red]$Dir[red]"[/red] | while read fileName
do 
echo "*** file: $Dir/$fileName"
done
done
```


*going to edit many scripts*


----------



## DutchDaemon (Jan 3, 2009)

The double quotes are only for whitespace encapsulation, so I only use them for that purpose.


----------



## lbl (Jan 3, 2009)

*The easy way*

An easier way ...


```
[lbl@atom0 ~]$ find /usr/home/lbl/mail/ | sed 's/^/*** file: /'
*** file: /usr/home/lbl/mail/
*** file: /usr/home/lbl/mail/sent-mail
*** file: /usr/home/lbl/mail/saved-messages
[lbl@atom0 ~]$
```


----------



## DutchDaemon (Jan 3, 2009)

Sure, but that will print all sub-directories too, which may not be what OP wants.


----------



## graudeejs (Jan 3, 2009)

lbl said:
			
		

> An easier way ...
> 
> 
> ```
> ...



I already see where i could use it....
But i still prefer DutchDaemon way


@ DutchDaemon the best part of your code is that it only requires me to modify 1 line in all my currents scripts, that used for


----------



## lbl (Jan 3, 2009)

*To remove subdirs ...*

To remove the dirs ...


```
[lbl@atom0 ~]$ find /usr/home/lbl/mail/ | grep -v '\/$' | awk '{ print "*** file: " $1 }'
*** file: /usr/home/lbl/mail/sent-mail
*** file: /usr/home/lbl/mail/saved-messages
[lbl@atom0 ~]$
```

or ...


```
[lbl@atom0 ~]$ find /usr/home/lbl/mail/ -type f | awk '{ print "*** file: " $1 }'
*** file: /usr/home/lbl/mail/sent-mail
*** file: /usr/home/lbl/mail/saved-messages
[lbl@atom0 ~]$
```

If you only want to get files from the specifik dir and not the subdirs you can use ...


```
[lbl@atom0 ~]$ find /usr/home/lbl/mail -type f -maxdepth 1 | sed "s/^/*** file: /"
*** file: /usr/home/lbl/mail/sent-mail
*** file: /usr/home/lbl/mail/saved-messages
[lbl@atom0 ~]$
```

/lbl


----------

