First of all I'm new here and i want to know how can i use cut and sed in awk of any method to edit my test data for keep that data to array
This is my sample data:
isdat pts/0 10.80.24.3 Fri Sep 20 10:20:30 2019 - Fri Sep 20 11:55:51 2019 (01:35)
normally i can choose usage_time by awk '{print $15}'
it's gonna be like this
output : (01:35)
and i will cut by cut -d ':' -f1 | sed -e 's/(/ /'
cut -d ':' -f1 | sed -e 's/(/ /'
for hour and this for minute cut -d ':' -f2 | sed -e 's/)/ /'
But cut and sed can't use in awk how can i solve this problem
You don't use them. Awk is perfectly capable of handling that on its own:
$ awk '
$2~/^pts/ { # the grep part
split($15,a,/:/) # split $15 by :
for(i in a) # process all parts (2)
gsub(/[()]/,"",a[i]) # remove parentheses
print a[1],a[2] # output both parts
}' file
Output:
01 35
You have a fine answer from James. Another slight twist using the last field (eg $NF
) can be:
awk '{sub(/:/," ",$NF); print substr($NF,2,length($NF)-2)}' file
Where you simply replace the ':'
with ' '
and then take the substring between (..)
.
Result
Piping your input to the expression above (and removing file
) results in:
01 35
There is always more than one way to skin-the-cat with awk
.
Per the comment, if you do want to limit output only to when the 2nd field begins with pts
, then you can do:
awk '$2~/^pts/{sub(/:/," ",$NF); print substr($NF,2,length($NF)-2)}' file
No magic.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.