PDA

View Full Version : [SOLVED] Read a line from one string till to another.... Unix scripting..



hakermania
August 19th, 2010, 11:57 AM
So i have a file which contains paths to JPG images separated by a space.
I have to separate them each path to another file. So, I have to search all strings that start from /home/ and ends with .jpg
Then write each one to another file...

Can you please help me on doing this???

Bachstelze
August 19th, 2010, 12:51 PM
If I understand correctly...


firas@tsukino ~ % cat test
/home/foo/bar/baz.jpg /home/bar/foo.jpg /home/bar/foo.png /var/www/foo.jpg /home/bar/baz/foo.jpg
firas@tsukino ~ % grep -o "/home/[^ ]*\.jpg" test > test2
firas@tsukino ~ % cat test2
/home/foo/bar/baz.jpg
/home/bar/foo.jpg
/home/bar/baz/foo.jpg

hakermania
August 19th, 2010, 01:04 PM
Thanks for your reply. Some Dirs have Spaces between them (e.g. Nice Pictures), then I have a script to correct them and add a \ before every space.
Your solution doesn't work in case the path includes Dirs with Spaces (e.g. /home/alex/Pictures/Nice\ Pictures/pic.jpg)

dwhitney67
August 19th, 2010, 01:55 PM
Thanks for your reply. Some Dirs have Spaces between them (e.g. Nice Pictures), then I have a script to correct them and add a \ before every space.
Your solution doesn't work in case the path includes Dirs with Spaces (e.g. /home/alex/Pictures/Nice\ Pictures/pic.jpg)

Take a step back... why did you create a file that is hard to parse?

Would it not have made more sense to place a separate file path on individual lines? More importantly, why do you even have a file? Could you not have used the 'find' command to locate the desired files and then process them?

hakermania
August 19th, 2010, 02:37 PM
dwhitney67 (http://ubuntuforums.org/member.php?u=322753) Interesting... Problem solved actually by this suggestion......
If you know QtCreator
i used QStringlist::join("\n") instead join(" ")
cool, thank you

geirha
August 19th, 2010, 04:37 PM
Even better would be to use \0 as separator, because \0 is the only character not allowed in pathnames.

To iterate a \0 separated list in bash, you'd do:

while IFS= read -r -d '' file; do
echo "Do stuff with $file"
done < file.list


See http://mywiki.wooledge.org/BashFAQ/020