understanding part of an awk script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers understanding part of an awk script
# 1  
Old 01-23-2010
understanding part of an awk script

Hi

I have an awk script running in ksh in which a section of code is picking out the datetime as follows:
Code:
 
dia=`echo $starttime | nawk '{ printf "%02d\n", substr($1,9,2)}'`
mes=`echo $starttime | nawk '{ printf "%02d\n", substr($1,6,2)}'`
ano=`echo $starttime | nawk '{ printf "%02d\n", substr($1,1,4)}'`
hora=`echo ${starttime#* } | nawk '{ printf "%02d\n", substr($1,1,2)}'`
minuto=`echo ${starttime#* } | nawk '{ printf "%02d\n", substr($1,4,2)}'`
 
# startime='2010-01-03 15:00'

and then assigning it to ftime as ftime=$ano$mes$dia$hora$minuto

But I don't understand how the above code is picking out the year, month, day, hour and minute.

Bearing the above starttime in mind, what would ftime be?

If I've missed any info out, please excuse me and let me know what it is?

Regards

Last edited by Scott; 01-23-2010 at 10:21 AM.. Reason: Please use code tags
# 2  
Old 01-23-2010
Well you can just execute and see:
Code:
$ starttime='2010-01-03 15:00'
$ dia=`echo $starttime | nawk '{ printf "%02d\n", substr($1,9,2)}'`
$ mes=`echo $starttime | nawk '{ printf "%02d\n", substr($1,6,2)}'`
$ ano=`echo $starttime | nawk '{ printf "%02d\n", substr($1,1,4)}'`
$ hora=`echo ${starttime#* } | nawk '{ printf "%02d\n", substr($1,1,2)}'`
$ minuto=`echo ${starttime#* } | nawk '{ printf "%02d\n", substr($1,4,2)}'`
$ ftime=$ano$mes$dia$hora$minuto
$ echo $ftime
201001031500

It is rather inefficient code though, as this this would accomplish the same if your shell supports it:
Code:
ftime=${starttime//[^0-9]}

-or-
Code:
ftime=$(echo "$starttime" | sed 's/[^0-9]//g')

# 3  
Old 01-23-2010
Thanks so much...Appreciated.

Would it be possible just to explain how this is working:

dia=`echo $starttime | nawk '{ printf "%02d\n", substr($1,9,2)}'`

?

Regards
# 4  
Old 01-23-2010
Echo the contents of starttime into nawk (do not preserve spacing)
substr($1,9,2) means take position 9 and 10 from this string
printf "%02d\n means print to digits and prepad with a zeroes if it there are fewer than to digits and add a carriage return (which is not implicit in the case of printf)
The backticks (`) are used to assign the result to variable dia.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Understanding awk 'expansion'

Heyas Recently i wanted to help someone with an awk script, but the end-script didnt work as expected. He wanted, if HOME was empty, to get the HOME of the current USER from /etc/passwd. At first i tried hardcoded with root: awk -F: '/^root/ {print $6}' /etc/passwd As that worked, i've... (4 Replies)
Discussion started by: sea
4 Replies

2. Shell Programming and Scripting

awk : Need Help in Understanding a command

Hello I am working on a Change request and Stuck at a point. The below awk command is used in the function. float_test ( ) { echo | awk 'END { exit ( !( '"$1"')); }' } I understand that awk 'END' is used to add one line at the end and exit is used to end the script with an error... (4 Replies)
Discussion started by: rahul2662
4 Replies

3. UNIX for Dummies Questions & Answers

Understanding awk

I found this on an awk site and would like to know what it does: /CARS/{x="";next} {if(x)print x;x=$0} END{if(x)print x}' Does it mean if it finds the word cars it skips that line and then prints the next one? (4 Replies)
Discussion started by: newbie2010
4 Replies

4. Shell Programming and Scripting

Help in Understanding awk if ( F ) syntax -

Hi Experts, I was looking at the below link, for finding words next to it, And unable to understand this syntax: Can any one please explain , what is meaning of this code: if ( F ) s = s ? s OFS $i : $i from:... (4 Replies)
Discussion started by: rveri
4 Replies

5. Shell Programming and Scripting

Help in understanding awk expression

Hi, Could somebody help me in understanding the following awk expression: awk -v n="POINT" '/%/{print $0 "\n" n ;next}1' < file name Thanks, Arun (6 Replies)
Discussion started by: arun_maffy
6 Replies

6. UNIX for Dummies Questions & Answers

Understanding / Modifying AWK command

Hey all, So I have an AWK command here awk '{if(FNR==NR) {arr++;next} if($0 in arr) { arr--; if (arr == 0) delete arr;next}{print $0 >"list2output.csv"}} END {for(i in arr){print i >"list1output.csv"}}' list1 list2 (refer to image for a more readable format) This code was submitted... (1 Reply)
Discussion started by: Aussiemick
1 Replies

7. Shell Programming and Scripting

AWK script issue for the part regular expression

Hi I am having a file as shown below FILE 1 TXDD00, TXDD01, TXDD02, TXDD03, TXDD04, TXDD05, TXDD06, TXDD07, TXDD08, TXDD09, TXDD10, TXDD11, TXDD12, TXDD13, TXDD14, TXDD15, TXDD16, TXDD17, TXDD18, TXDD19, TXDDCLK, TXDJTAGAMPL0, TXDJTAGAMPL1,... (3 Replies)
Discussion started by: jaita
3 Replies

8. UNIX for Advanced & Expert Users

understanding awk in this script

i am analyzing a query written by another developer ,need to understand part of script am looking at a code ..and it converts comma files to pipe delimited and also takes away quotes from any columns, source field format: 2510,"Debbie",NewYork changes to target: 2510|Debbie|NewYork ... (1 Reply)
Discussion started by: coolrock
1 Replies

9. Shell Programming and Scripting

Perl or Awk script to copy a part of text file.

Hi Gurus, I'm a total newbie to Perl and Awk scripting. Let me explain the scenario, there is a DB2 table with 5 columns and one of the column is a CLOB datatype containing XML. We need all the 4 columns but only a portion of string from the XML column. We decided to export DB2 table to a .del... (26 Replies)
Discussion started by: asandy1234
26 Replies

10. Shell Programming and Scripting

Understanding Awk and Cat

Hi Guys, I was recently come across some code to hopefully learn a little bit about putting Shell commands into PHP application to run on a Linux server. However, I don't understand the command AT ALL... and was wondering if anyone can interpret it: cat userIDs.dat | awk '{s=s+1; if... (1 Reply)
Discussion started by: jordRiot
1 Replies
Login or Register to Ask a Question