ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
This is a theoretical problem at this point for me, but nonetheless it's something that's been bothering me for awhile.
Say I have a list of files in a text file, one file per line. Some files have characters that need escaping (spaces, etc.). I want to pass each file as an argument to a program. How?
Code:
% cat filelist
./dir 1/file 1
./dir 2/file 2
% cat `cat filelist`
cat: ./dir: No such file or directory
cat: 1/file: No such file or directory
cat: 1: No such file or directory
cat: ./dir: No such file or directory
cat: 2/file: No such file or directory
cat: 2: No such file or directory
% cat "`cat filelist`"
cat: ./dir 1/file 1
./dir 2/file 2: No such file or directory
bash 3.2.25(1)$ mkdir 'dir 1' 'dir 2'
bash 3.2.25(1)$ echo FileOne>'./dir 1/file 1'
bash 3.2.25(1)$ echo FileTwo>'./dir 2/file 2'
bash 3.2.25(1)$ echo './dir 1/file 1
> ./dir 2/file 2'>filelist
bash 3.2.25(1)$ cat `cat filelist`
cat: ./dir: No such file or directory
cat: 1/file: No such file or directory
cat: 1: No such file or directory
cat: ./dir: No such file or directory
cat: 2/file: No such file or directory
cat: 2: No such file or directory
bash 3.2.25(1)$ (IFS=$'\n';cat `cat filelist`)
FileOne
FileTwo
If you set the IFS to newline only in your script, you'll then be able to use the cat cmd to read in each line.
You'll still have to be careful to escape spaces during further processing.
radoulov, chrism1: Great, thanks! Guess I need to read the bash manual a bit more.
bigearsbilly: That actually calls command once per argument, no? For 'cat' it has the same result, obviously. Still, it's always good to learn another trick that might help sometime down the road.
syg00: Yeah, "Learn perl" would be a great solution, if a little much in the short term.
bigearsbilly: That actually calls command once per argument, no? For 'cat' it has the same result, obviously. Still, it's always good to learn another trick that might help sometime down the road.
"cat"'s purpose is to concatenate files (although almost everyone i see use it to display files). use tools like awk (or even the while loop) if you want to process files.
Quote:
syg00: Yeah, "Learn perl" would be a great solution, if a little much in the short term.
I know cat is meant to concatinate files, it was just the simplest thing I could think of for an example.
Can this be done with awk? I've used awk a little but it was mostly just copying examples and replacing a few strings... I think awk is the next thing on my list of Unix tools to explore properly.
of course. depending on what your program that takes in argument is
Code:
awk '{
cmd="program \047"$0"\047" #single quote the file path with spaces
system(cmd) #or cmd|getline
close(cmd)
}' file
Quote:
I've used awk a little but it was mostly just copying examples and replacing a few strings...
its more than that. it loops over files so almost always, i don't have to use while/for loops. its grep/sed/cat/etc combined, so almost always, i only use awk for my job. Of course, this is just what i do. No need to disagree with me on this.
Quote:
I think awk is the next thing on my list of Unix tools to explore properly.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.