Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
| 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
09-24-2005, 08:27 AM
|
#1
|
|
Member
Registered: Aug 2005
Distribution: Debian, Ubuntu, Fedora
Posts: 74
Rep:
|
awk: How can I return a specified record
Hi,
In awk , we choose which field to return by variables $1,$2 ,......
this will list the fields wanted for all records (where record is a single line of the returned values), the returned values may have more that 1 record usually.
I need to select a single record , mean to get the output of the awk result line by line.
how can this be doen ,
( I need this for many things , ex: deleting all directories with name ".sss" (by filtering the find results through awk then pass record-by-record to rm) , and may others usages.
Thanks.
|
|
|
|
09-24-2005, 08:41 AM
|
#2
|
|
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 8,718
|
Hi,
Hope I understand correctly, but is this what you want/need:
awk '/root/ { print $0 }' /etc/passwd
This only prints lines that contain root from the /etc/passwd file.
You could also check a specific field and print another field if a hit is found:
awk -F":" '$3 == "0" {print $1}' /etc/passwd
Print first field (login name) if third field (UID) equals zero.
Hope this helps.
|
|
|
|
09-24-2005, 09:57 AM
|
#3
|
|
Member
Registered: Aug 2005
Distribution: Debian, Ubuntu, Fedora
Posts: 74
Original Poster
Rep:
|
Thanks for your replay ,, let me give an example please:
Code:
find ./ -name ".svn"
will give:
./docroot/editor/skins/silver/toolbar/.svn
./docroot/editor/filemanager/.svn
./docroot/editor/filemanager/browser/.svn
./docroot/editor/filemanager/browser/default/images/.svn
./docroot/editor/filemanager/browser/default/images/icons/.svn
..... 100 more lines
I wonder how to delete them ? i have thought in awk , I want awk to return one line (RECORD) at a time so i can delete them through redirecting (awk or other) output to rm.
also:
Code:
ps aux | awk '/httpd/ {print $2}'
will give
1111
2222
3333
as process IDs
and I need to kill them all, so I need to get the values line-by-line or in awk terms: as records.
Thanks.
|
|
|
|
09-24-2005, 10:47 AM
|
#4
|
|
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 8,718
|
Quote:
Code:
find ./ -name ".svn"
will give:
./docroot/editor/skins/silver/toolbar/.svn
./docroot/editor/filemanager/.svn
./docroot/editor/filemanager/browser/.svn
./docroot/editor/filemanager/browser/default/images/.svn
./docroot/editor/filemanager/browser/default/images/icons/.svn
..... 100 more lines
I wonder how to delete them ? i have thought in awk , I want awk to return one line (RECORD) at a time so i can delete them through redirecting (awk or other) output to rm.
|
The 2 easiest ways are:
Use the -exec option that comes with find. I.e:
find ./ -name ".svn" -exec rm {} \;
If you want to know, beforehand, what will be deleted change rm to ls.
Another way is:
find ./ -name ".svn" | xargs rm
The find command, in both examples, will return what you are looking for and gives this to (example one) rm by using the -exec parameter, or pipes it to xargs rm in the second example.
Quote:
also:
Code:
ps aux | awk '/httpd/ {print $2}'
will give
1111
2222
3333
as process IDs
and I need to kill them all, so I need to get the values line-by-line or in awk terms: as records.
Thanks. [/B]
|
Here you can pipe it to xargs rm. I.e:
ps -aux | awk '/httpd/ {print $2}' | xargs rm
man xargs for some gory details.
Hope this helps.
|
|
|
|
09-24-2005, 11:36 AM
|
#5
|
|
Member
Registered: Aug 2005
Distribution: Debian, Ubuntu, Fedora
Posts: 74
Original Poster
Rep:
|
Many thanks for the new command (for me) xargs ...
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 04:42 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|