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. |
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. |
Thanks for your replay ,, let me give an example please:
Code:
find ./ -name ".svn" also: Code:
ps aux | awk '/httpd/ {print $2}' Thanks. |
Quote:
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:
ps -aux | awk '/httpd/ {print $2}' | xargs rm man xargs for some gory details. Hope this helps. |
Many thanks for the new command (for me) xargs ...
|
All times are GMT -5. The time now is 11:14 AM. |