Help with awk Print Substr


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with awk Print Substr
# 1  
Old 02-23-2013
Help with awk Print Substr

Can someone explain to me exactly how it work. I have a line from a script here,

Code:
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,


Code:
[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?

Last edited by Scrutinizer; 02-24-2013 at 04:07 AM.. Reason: code tags
# 2  
Old 02-23-2013
How it works...

Well, you are correct in stating that $19 is the 19th string. This is what I get with your original code...
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
Code:
98

Note that a modification of the awk statement precludes the need for the last two sed statements..
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,2,2)}'

also yields
Code:
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.Smilie
This User Gave Thanks to MattyV For This Post:
# 3  
Old 02-23-2013
Matt,

From your 108 example

Code:
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?

Last edited by Scrutinizer; 02-24-2013 at 04:09 AM.. Reason: code tags
# 4  
Old 02-23-2013
If you just want to get at the number in the last field between the square brackets you can use either of these awks...
Code:
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

# 5  
Old 02-23-2013
Set opening & closing square brackets [] as field separators and extract the values inside it:
Code:
awk -F'[][]' '{ print $2, $4, $6, $8, $10, $12 }' logcheck.txt
30376 98 0 0 0 98

# 6  
Old 02-23-2013
Quote:
Originally Posted by scj2012
Matt,

From your 108 example

awk '{print substr($19,2,2)}'
I see how you get 10. How do you get [1] and 1].
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]"
Quote:
Also what meant by second statement?
I meant the alternative awk statement I wrote using substr $19,2,2 instead of your original statement substr $19,2,5 | sed...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk and substr

Hello All; I have an input file 'abc.txt' with below text: 512345977,213458,100021 512345978,213454,100031 512345979,213452,100051 512345980,213455,100061 512345981,213456,100071 512345982,213456,100091 512345983,213457,100041 512345984,213451,100011 I need to paste the first field... (10 Replies)
Discussion started by: mystition
10 Replies

2. Shell Programming and Scripting

HELP : awk substr

Hi, - In a file test.wmi Col1 | firstName | lastName 4003 | toto_titi_CT- | otot_itit - I want to have only ( colones $7,$13 and $15) with code 4003 and 4002. for colone $13 I want to have the whole name untill _CT- or _GC- 1- I used the command egrep with awk #egrep -i... (2 Replies)
Discussion started by: georg2014
2 Replies

3. Shell Programming and Scripting

awk substr

Hello life savers!! Is there any way to use substr in awk command for returning one part of a string from declared start and stop point? I mean I know we have this: substr(string, start, length) Do we have anything like possible to use in awk ? : substr(string, start, stop) ... (9 Replies)
Discussion started by: @man
9 Replies

4. Shell Programming and Scripting

Substr with awk

Hi to all, I'm here again, cause I need your help to solve another issue for me. I have some files that have this name format: date_filename.csv In my shell I must rename each file removing the date so that the file name is filename.csv To do this I use this command: fnames=`ls ${fname}|... (2 Replies)
Discussion started by: leobdj
2 Replies

5. Shell Programming and Scripting

awk substr

HI I am using awk and substr function to list out the directory names in the present working directory . I am using below code ls -l | awk '{ if ((substr($1,1,1)) -eq d) {print $9 }}' But the problem is i am getting all the files and directories listed where as the requirement i wrote... (7 Replies)
Discussion started by: prabhu_kumar
7 Replies

6. Shell Programming and Scripting

Help with awk and substr

I have the following to find lines matching "COMPLETE" and extract parts of it using substr. sed -n "/COMPLETE/p" 1.txt | awk 'BEGIN { FS = "\" } {printf"%s %s:%s \n", substr($3,17,3),substr($6,4,1), substr($7,4,1)}' | sort | uniq > temp.txt Worked fine until the numbers in 2nd & 3rd substr... (5 Replies)
Discussion started by: zpn
5 Replies

7. Shell Programming and Scripting

awk substr

Hi I have multiple files that name begins bidb_yyyymm. (yyyymm = current year month of file creation). What I want to do is look at the files and where yyyymm is older than 1 month I want to remove the file from the server. I was looking at looping through the files and getting the yyyymm... (2 Replies)
Discussion started by: colesga
2 Replies

8. UNIX for Dummies Questions & Answers

awk or substr

i have a variable 200612 the last two digits of this variable should be between 1 and 12, it should not be greater than 12 or less than 1 (for ex: 00 or 13,14,15 is not accepted) how do i check for this conditions in a unix shell script. thanks Ram (3 Replies)
Discussion started by: ramky79
3 Replies

9. Shell Programming and Scripting

How to use awk substr ?

Hi all, I have a flatfile I would like to get ext = 7950 , how do I do that ? if ($1 == "CTI-ProgramStart") { ext = substr($9,index($9,"Extension")+11,4); But why it is not working ???? Please help . Thanks (1 Reply)
Discussion started by: sabercats
1 Replies

10. Shell Programming and Scripting

awk substr?

Sorry if this has been posted before, I searched but not sure what I really want to do. I have a file with records that show who has logged into my application: 2003-03-14:I:root: Log_mesg: registered servername:userid. (more after this) I want to pull out the userid, date and time into... (2 Replies)
Discussion started by: MizzGail
2 Replies
Login or Register to Ask a Question