LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-21-2014, 08:01 PM   #1
BeachHead
LQ Newbie
 
Registered: May 2012
Location: Germany
Distribution: Arch, AOSP
Posts: 24

Rep: Reputation: 2
Find files with $ and spaces in name


Hallo,
i want to parse all files in a folder recursively. The 'sed' command works properly if i pass a literal name but it fails if i run it in this 'for' loop.

I tried many variations with quotes and double quotes as well as -print0/'xargs -0' examples but it looks like i'm just poking in the dark...

Anyone know what's wrong here?

GNU bash, version 4.1.10(4)-release (i686-pc-cygwin)

Code:
#!/bin/bash

FILES="myfolder" 
for i in "$(find "$FILES" -name '*.*')"; do
	#echo "$i"
	sed -n '/invoke/ {
		s/.*->\(.*\)/\1/
		s/(.*//
		p
	}' < "$i" >> output.txt
done
Code:
$ ls -R
file$1.txt  new folder

./new folder:
file$2.txt
Error is:
Code:
./sed-invokes.sh: line 6: myfolder/file$1.txt
myfolder/new folder/file$2.txt: No such file or directory
If i run 'echo "$i"' in the loop it shows the files properly.
Code:
myfolder/file$1.txt
myfolder/new folder/file$2.txt
 
Old 01-21-2014, 10:47 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Try using the following echo instead and see what happens:
Code:
echo "|$i|"
Also, whilst '*.*' may work in this limited case, should any directory or other file type have a period in it, this code will quickly unravel.
 
1 members found this post helpful.
Old 01-22-2014, 10:13 AM   #3
BeachHead
LQ Newbie
 
Registered: May 2012
Location: Germany
Distribution: Arch, AOSP
Posts: 24

Original Poster
Rep: Reputation: 2
Thanks. I changed 'find' to '-type f'.
Code:
#!/bin/bash

FILES="myfolder"
for i in "$(find "$FILES" -type f)"; do
	echo "|$i|"
done
The output seems one solid string only.
Code:
|myfolder/file$1.txt
myfolder/new folder/file$2.txt|
If i remove the double quotes surrounding $(find...) i get separate entries now but suffering whitespace problem (myfolder/new folder/file$2.txt).
Code:
for i in $(find "$FILES" -type f); do
Code:
|myfolder/file$1.txt|
|myfolder/new|
|folder/file$2.txt|

Last edited by BeachHead; 01-22-2014 at 10:16 AM.
 
Old 01-22-2014, 10:53 AM   #4
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 529

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
for + find = problems with whitespace and other special characters like * and names like --help

Try "while read" instead of "for":
Code:
#!/bin/bash

FILES="myfolder"
find "$FILES" -type f | while read -r i
do
	echo "|$i|"
done
That should handle just about anything except filenames containing a newline character, since the read command expects items to be separated by newline characters.

Edit: I also tested it with your sed command. It seems to work.

Last edited by Beryllos; 01-22-2014 at 11:06 AM.
 
1 members found this post helpful.
Old 01-22-2014, 11:23 AM   #5
BeachHead
LQ Newbie
 
Registered: May 2012
Location: Germany
Distribution: Arch, AOSP
Posts: 24

Original Poster
Rep: Reputation: 2
Yep, looks good. Thanks.
I slightly changed it to use \0 as separator.

This is the current script:
Code:
#!/bin/bash

function do_sed {
	sed -n '/invoke/ {
		s/.*->\(.*\)/\1/
		s/(.*//
		p
	}' < "$1" >> output.txt
}

FILES="myfolder"
if [ -f output.txt ]; then rm output.txt; fi
time find "$FILES" -type f -print0 | while read -d $'\0' i
do
	echo "$i"
	do_sed "$i"
done

Last edited by BeachHead; 01-22-2014 at 11:42 AM.
 
Old 01-22-2014, 11:54 AM   #6
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 529

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
Quote:
Originally Posted by BeachHead View Post
Yep, looks good. Thanks.
I slightly changed it to use \0 as separator.
Nice. Does that mean it can handle any character in the filename (except \0 which is never allowed anyway)? Thanks for pointing that out.
 
Old 01-22-2014, 12:05 PM   #7
BeachHead
LQ Newbie
 
Registered: May 2012
Location: Germany
Distribution: Arch, AOSP
Posts: 24

Original Poster
Rep: Reputation: 2
That's at least the intention. I cannot test it atm though. Newlines on NTFS/Cygwin are probably not allowed i think (Edit: It does work in cygwin. Probably abstracted).

Edit: Tested, seems to work. I created files with 'echo aaa > "$(echo -e "aaa\012bbb")"' and they were processed properly. Newlines on 'echo' feedback looks funny though.

Last edited by BeachHead; 01-22-2014 at 12:38 PM.
 
Old 01-23-2014, 07:02 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
As a final note, remember that the current method used for feeding the while loop through a pipe will mean any data changed within the loop will be lost once you have left
the loop due to it being in a sub-shell. I will let you investigate the alternatives
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] accessing files with spaces j360 Linux - General 2 03-18-2011 12:26 AM
bash: 'find ./ -type d' how to deal with directories with spaces? xaos5 Programming 4 09-06-2009 06:30 PM
find -name and variables with spaces Adam1571 Programming 1 11-05-2008 07:43 AM
Question about find command + recognizing filenames with spaces 200mg Linux - General 3 02-22-2008 02:37 PM
how to find the spaces on the folder??? ashley75 Linux - General 4 04-10-2006 03:56 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration