Generate shell command wit awk
Dear all,
Sometimes, we need to do repeated work for copy or mkdir directories. I am thinking is that possible to redirect the output of awk to input as command to the shell? For example I have several files in different directories: Code:
~/workingdir1/file1.ppt Can I do something like Code:
awk 'END{for(i=1;i<=n;i++) getline < print "~/workingdir" i "/file1.ppt" " ~/commondir"}' I know I can write shell script to do this. But I am wondering if there are some easier ways with awk sinse with awk I just need one line and I do not need to generate a file then type chmod +x file, ./file. Or there are some other simple ways for the similar purpose? How to do it? Thanks for your kind help. |
Hi.
You definitely need to look at shell globbing. To copy files just do Code:
cp ~/workingdir*/file*.ppt ~/commondir |
That's so simple! Thanks a lot for your quick and helpful answer!. Let's still keep this question open at this moment to see if some others come up with any different ideas.
Quote:
|
Quote:
Code:
$ ls Cheers, Tink |
If you have a list of files, that you want to copy or move to a target directory, look at the -t option of cp & mv.
You can pipe the list through "tr" to replace new lines with NULs and pipe the result to xargs. This will help with filenames containing "evil" characters. Also xargs can help where file globbing would result in out of memory errors, or when using the output of the find command. example: tr '\n' '\0' <filelist | xargs -0 -n 1000 cp -t /path/to/targetdir/ |
find(1) is also an option (especially combined with xargs -0)
|
And of course always other ways:
Code:
awk 'BEGIN{for(i = 1; i < ARGC; i++)print | "cp "ARGV[i]}" /path/to/targetdir/"' /path/to/files/* |
Thank you so much guys! Thanks a lot for all the kind support and contributions! Hope this thread can help others who has the same question and help us learn more about shell command.
|
All times are GMT -5. The time now is 11:09 AM. |