LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 03-20-2012, 12:59 PM   #1
cristalp
Member
 
Registered: Aug 2011
Distribution: Linux Mint
Posts: 103

Rep: Reputation: Disabled
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.
 
Old 03-20-2012, 01:14 PM   #2
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
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'.

Last edited by firstfire; 03-20-2012 at 01:19 PM.
 
1 members found this post helpful.
Old 03-20-2012, 01:30 PM   #3
cristalp
Member
 
Registered: Aug 2011
Distribution: Linux Mint
Posts: 103

Original Poster
Rep: Reputation: Disabled
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 View Post
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'.
 
Old 03-20-2012, 10:40 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by cristalp View Post
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

Last edited by Tinkster; 03-20-2012 at 10:42 PM.
 
1 members found this post helpful.
Old 03-20-2012, 10:51 PM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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/
 
1 members found this post helpful.
Old 03-21-2012, 12:00 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,852
Blog Entries: 1

Rep: Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868
find(1) is also an option (especially combined with xargs -0)
 
1 members found this post helpful.
Old 03-21-2012, 01:35 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
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/*
 
1 members found this post helpful.
Old 03-21-2012, 05:28 AM   #8
cristalp
Member
 
Registered: Aug 2011
Distribution: Linux Mint
Posts: 103

Original Poster
Rep: Reputation: Disabled
Smile

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.
 
  


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
[bash][awk] command runs from shell, not from script JohnyDRipper Programming 2 03-09-2012 05:01 AM
Pass a shell variable to an AWK command chogall Programming 1 12-23-2010 10:12 AM
[SOLVED] Shell script using awk command gagou7 Linux - Newbie 6 11-05-2009 12:27 PM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 04:11 PM
How to run a shell command containing awk, and grep within a C program Linh Programming 3 06-05-2003 07:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration