Programming and Scripting :: Little help needed



Heh, what happens if you put double quotes around the outer cat?

Personally, I'd just use IFS - it's easy to do and read.

Quote (^thehatsrule^ @ Jan. 30 2007,16:09)
If you plan on using sed, you could use "s/ /\ /g"
If you plan on using a shell script, you could use IFS
(i.e.
IFS="
"
or use 'read')

Or you could just remember to never use spaces in filenames :P

I don't understad this...
You are creating a variable here containing only one return char?

EDIT
Quote
      read  [-ers]  [-u  fd] [-t timeout] [-a aname] [-p prompt] [-n nchars]
      [-d delim] [name ...]
             One  line  is  read  from  the standard input, or from the file
             descriptor fd supplied as an argument to the -u option, and the
             first  word  is  assigned to the first name, the second word to
             the second name, and so  on,  with  leftover  words  and  their
             intervening separators assigned to the last name.  If there are
             fewer words read from the input stream than names, the  remain-
             ing names are assigned empty values.  The characters in IFS are
             used to split the line into words.  The backslash character (\)
             may  be used to remove any special meaning for the next charac-
             ter read and for line continuation.


Now I may know what you mean...

It worked just fine.
But. I'd like to have it on one line.
So I did this:
Code Sample
IFS=`echo -e "\n"` && cat `echo -e "one two three\nfour five six\nseven" | sed -e 's/ /\\ /g'`

Now It should work but it ain't. =D
It tells me:
Quote
cat: one two three
four five six
seven: No such file or directory

When it should tell:
Quote
cat: one two three: No such file or directory
cat: four five six: No such file or directory
cat: seven: No such file or directory


Ok. I'm just about to... "Lose it. It means go crazy. Nuts. Insane. Bonzo. No longer in possession of one's faculties. Three fries short of a Happy Meal. WACKO!!"

Well... you'd have to use something like a for loop to do that...
IFS=`printf "\n"`; for each_line in `cat list.txt`; do; cat "$each_line"; done
IFS is a special variable in posix shells that indicate the field separator (or something along those lines - usually set to whitespace)

Either way, you'll have a couple solutions... just choose one :P

It's probably not worth trying to get things on one line. Using command substitution (backticks) will fail once you get to a certain size and needs to be avoided if you want to process a lot of files.

This is what I'd probably do:

Code Sample
# 1st make sure file ends in a newline
sed -i '$ s/$/\n/' list.txt;

while read LINE; do
     # set single quotes around each filename to be safe
    LN=\'"${LINE}"\'
    echo "${LN}" | xargs cat
done <"list.txt"


This has the advantage that your list of filenames list.txt can be arbitrarily long.

Not tried with busybox.

Next Page...
original here.