LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Generate shell command wit awk (https://www.linuxquestions.org/questions/programming-9/generate-shell-command-wit-awk-935520/)

cristalp 03-20-2012 12:59 PM

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
~/workingdir2/file2.ppt
~/workingdir3/file3.ppt
~/workingdir4/file4.ppt

What I want to do is to copy all these files to a conman directories.

Can I do something like
Code:

awk 'END{for(i=1;i<=n;i++) getline < print "~/workingdir" i "/file1.ppt" " ~/commondir"}'
Similar to this, I would expect a general way to do this kind of repeating commanding with rm, mkdir, mv , etc, or even more complicated commanding in a simple one liner command.

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.

firstfire 03-20-2012 01:14 PM

Hi.

You definitely need to look at shell globbing.
To copy files just do
Code:

cp ~/workingdir*/file*.ppt ~/commondir
Read about "Pattern Matching" and "Brace Expansion" in `man bash'.

cristalp 03-20-2012 01:30 PM

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:

Originally Posted by firstfire (Post 4631788)
Hi.

You definitely need to look at shell globbing.
To copy files just do
Code:

cp ~/workingdir*/file*.ppt ~/commondir
Read about "Pattern Matching" and "Brace Expansion" in `man bash'.


Tinkster 03-20-2012 10:40 PM

Quote:

Originally Posted by cristalp (Post 4631766)
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
~/workingdir2/file2.ppt
~/workingdir3/file3.ppt
~/workingdir4/file4.ppt

What I want to do is to copy all these files to a conman directories.

Can I do something like
Code:

awk 'END{for(i=1;i<=n;i++) getline < print "~/workingdir" i "/file1.ppt" " ~/commondir"}'
Similar to this, I would expect a general way to do this kind of repeating commanding with rm, mkdir, mv , etc, or even more complicated commanding in a simple one liner command.

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.

To answer your question: sure!

Code:

$ ls
test.png
$ echo | awk '{print "cp test.png test2.png"}'|bash
$ ls
test.png test2.png

Not very useful, but yes, it can be done =o)



Cheers,
Tink

jschiwal 03-20-2012 10:51 PM

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/

NevemTeve 03-21-2012 12:00 AM

find(1) is also an option (especially combined with xargs -0)

grail 03-21-2012 01:35 AM

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/*

cristalp 03-21-2012 05:28 AM

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.