PDA

View Full Version : [Solved] sh for + sed or awk or...


graudeejs
January 3rd, 2009, 15:34
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
January 3rd, 2009, 16:05
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
January 3rd, 2009, 16:33
Thank you, friend....
this worked...
i preciously never used while, your great peace of code opens grand new perspective for me in sh :D

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


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



*going to edit many scripts*

DutchDaemon
January 3rd, 2009, 16:51
The double quotes are only for whitespace encapsulation, so I only use them for that purpose.

lbl
January 3rd, 2009, 16:54
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
January 3rd, 2009, 17:07
Sure, but that will print all sub-directories too, which may not be what OP wants.

graudeejs
January 3rd, 2009, 17:11
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 ~]$

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 :D

lbl
January 3rd, 2009, 20:27
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