scj2012
February 23, 2013, 11:45am
1
Can someone explain to me exactly how it work. I have a line from a script here,
Processed=$(cat logcheck.txt | /bin/awk '{print substr($19,0,5)}'| sed -e 's/\[//g' | sed -e 's/\].*//g')
This is what is in the logcheck.txt,
[30376] 2013-02-23 10:00:41 DOW=7 HOUR=10:00 WK=4 DOM=23 MON=2 - Total [98], Disabled [0], Skipped [0], Failed [0], Processed [98]
Please explain the $19, 0, 5.
I'm assuming the $19 is because it's in the 19th position counting left to right? What about the 0 and 5?
MattyV
February 23, 2013, 12:31pm
2
Well, you are correct in stating that $19 is the 19th string. This is what I get with your original code...
echo '[30376] 2013-02-23 10:00:41 DOW=7 HOUR=10:00 WK=4 DOM=23 MON=2 -
Total [98], Disabled [0], Skipped [0], Failed [0], Processed [98]' | awk '{print substr($19,0,5)}'| sed -e 's/\[//g' | sed -e 's/\].*//g'
yields
98
Note that a modification of the awk statement precludes the need for the last two sed statements..
echo '[30376] 2013-02-23 10:00:41 DOW=7 HOUR=10:00 WK=4 DOM=23 MON=2
- Total [98], Disabled [0], Skipped [0], Failed [0], Processed [98]' | awk '{print substr($19,2,2)}'
also yields
98
substr takes [string], [position in the string to begin processing],[number of characters to return].
Because we are running awk $19 is the variable automatically assigned to the nineteenth
position.
It should be noted that my second example is not ALWAYS an adequate substitute for the original statement, because sometimes [98] may be [108], in which case the second would have returned 10, or [1] in which case the second statement would have returned 1].
Hope that helps.
1 Like
scj2012
February 23, 2013, 2:06pm
3
Matt,
From your 108 example
awk '{print substr($19,2,2)}'
I see how you get 10. How do you get [1] and 1]. Also what meant by second statement?
If you just want to get at the number in the last field between the square brackets you can use either of these awks...
cat file
[30376] 2013-02-23 10:00:41 DOW=7 HOUR=10:00 WK=4 DOM=23 MON=2 - Total [98], Disabled [0], Skipped [0], Failed [0], Processed [98]
awk -F"[[]|[]]" '{print $(NF-1)}' file
awk '{gsub("[[]|[]]","",$19);print $19}' file
Yoda
February 23, 2013, 2:34pm
5
Set opening & closing square brackets []
as field separators and extract the values inside it:
awk -F'[][]' '{ print $2, $4, $6, $8, $10, $12 }' logcheck.txt
30376 98 0 0 0 98
MattyV
February 23, 2013, 3:35pm
6
So what I meant was that if the stock value was 1 dollar, the 19th string would contain "[1]" substr [1],2,2 would result in "1]"
I meant the alternative awk statement I wrote using substr $19,2,2 instead of your original statement substr $19,2,5 | sed...