LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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

Reply
 
Search this Thread
Old 09-24-2005, 08:27 AM   #1
dimsh
Member
 
Registered: Aug 2005
Distribution: Debian, Ubuntu, Fedora
Posts: 74

Rep: Reputation: 15
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.
 
Old 09-24-2005, 08:41 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
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.
 
Old 09-24-2005, 09:57 AM   #3
dimsh
Member
 
Registered: Aug 2005
Distribution: Debian, Ubuntu, Fedora
Posts: 74

Original Poster
Rep: Reputation: 15
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.
 
Old 09-24-2005, 10:47 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
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.
 
Old 09-24-2005, 11:36 AM   #5
dimsh
Member
 
Registered: Aug 2005
Distribution: Debian, Ubuntu, Fedora
Posts: 74

Original Poster
Rep: Reputation: 15
Many thanks for the new command (for me) xargs ...
 
  


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
Unable to record mic-in with SoundBlaster Live! while able to record other sources max76230 Linux - Newbie 2 03-14-2005 04:31 AM
How to return values into an array using awk Helene Programming 1 05-01-2004 10:05 PM
using awk wedgeworth Linux - Newbie 9 02-20-2004 07:48 AM
awk question dilberim82 Programming 2 10-01-2003 07:12 PM
Inserting a carriage return in awk legtester Linux - General 1 08-17-2003 05:29 PM


All times are GMT -5. The time now is 12:40 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration