LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   awk: How can I return a specified record (https://www.linuxquestions.org/questions/linux-newbie-8/awk-how-can-i-return-a-specified-record-366529/)

dimsh 09-24-2005 08:27 AM

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.

druuna 09-24-2005 08:41 AM

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.

dimsh 09-24-2005 09:57 AM

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.

druuna 09-24-2005 10:47 AM

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.

dimsh 09-24-2005 11:36 AM

Many thanks for the new command (for me) xargs ...


All times are GMT -5. The time now is 11:14 AM.