LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 08-15-2004, 03:31 PM   #1
newbie_ken
LQ Newbie
 
Registered: Aug 2004
Posts: 3

Rep: Reputation: 0
shell script programming problem


hi guys,

i'm new to linux and i am facing a problem with filenames that have spaces in them.

find $folder -type f -printf "%f/n" > file.tmp

I use the above line to copy filenames in a folder to a temp file, but if the filename contain spaces it will be written as 2 separate lines.

example: the filename "kenny rogers" will be displayed as

kenny
rogers

what should i do to?

thanks
Ken
 
Old 08-15-2004, 03:55 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Your command works as expected, but your script doesn't. We'll need to see the script to give an accurate solution.

Last edited by Dark_Helmet; 08-15-2004 at 03:58 PM.
 
Old 08-15-2004, 11:23 PM   #3
newbie_ken
LQ Newbie
 
Registered: Aug 2004
Posts: 3

Original Poster
Rep: Reputation: 0
Im trying to find files in a directory and sort them out according to highest frequency, then alphabetical order.

My code:

#!/bin/sh

start_folder=""

if [ "$1"="" ]; then
start_folder=.
else
start_folder=$1
fi

find $start_folder -type f -printf "%f\n" > file1.tmp
cat file1.tmp|uniq > file2.tmp

for line in `cat file2.tmp` # problem starts here as the for loop treats the space as a delimiter
do
count=`grep -x $line file1.tmp|wc -l`
echo $line $count >> file3.tmp
done

sort -k 2nr -k 1 file3.tmp | cut -d " " -f1 | head

rm *.tmp
 
Old 08-16-2004, 12:04 AM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Ok, I was worried you might be doing something non-standard. This is my normal solution to this kind of problem:

Code:
#!/bin/sh

start_folder=""

if [ "$1"="" ] ; then
  start_folder=.
else
  start_folder=$1
fi

find $start_folder -type f -printf "%f\n" > file1.tmp
cat file1.tmp|uniq > file2.tmp

old_ifs=$IFS
IFS=$'\n'
for line in `cat file2.tmp` # problem starts here as the for loop treats the space as a delimiter
do
  count=`grep -x "$line" file1.tmp|wc -l`
  echo $line $count >> file3.tmp
done
IFS=$old_ifs

sort -k 2nr -k 1 file3.tmp | cut -d " " -f1 | head

rm *.tmp
As a very quick explanation, the IFS variable defines what characters act as delimiters. So the lines in red simply store the normal value, replace that list with the newline as the only delimiter, do your processing, and restore the old value.

Also, the reason you need to surround $line in double quotes is for a similar reason. If your files contain spaces, grep can't know when your expression ends and filename(s) begin.
 
Old 08-16-2004, 04:57 AM   #5
newbie_ken
LQ Newbie
 
Registered: Aug 2004
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks a lot!!! Dark.
You're a life saver.
By the way what commands can i use to test for empty files or directories?
I'm still new to linux and not used to the syntax.
 
Old 08-16-2004, 12:19 PM   #6
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
The normal method is to use something like this:
Code:
if [ -e $some_filename ] ; then
  file/directory exists
else
  file/directory not found
fi
The [ ] is a shorthand for the test command. You can snoop around what tests are available by reading the man pages: man test
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 06:32 PM
question about shell-script programming GSX Programming 11 11-22-2005 05:58 PM
creating shell script programming using KNOPPIX.. help cinderella Linux - Newbie 4 12-20-2004 08:12 PM
How to delete a line from a text file with shell script programming Bassam General 1 01-28-2004 09:51 PM
shell script programming steltner Linux - General 0 05-17-2003 08:00 AM


All times are GMT -5. The time now is 11:43 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration