Help me for using the awk command in UNIX


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help me for using the awk command in UNIX
# 8  
Old 03-22-2013
If you google this site for "tm2tm" you will see the C version of using mktime, localtime and strftime in it, which awk will be very close to. Do you have the GNU awk with its time functions described above. Have you thought out the definition of last Friday for every day of the week? localtime will tell you the day of the week, but you need to calculate how many days back Friday last if from now, run localtime on the current time adjusted that many day, and localtime will give you the day of month. systime -> strftime -> DOW calc -> modified systime value -> strftime -> day of month (and anything else you care to ask for). There are 86400 seconds in a day, and systime is in seconds since 1970 GMT. One pitfall might be the DST shift, if you run this at the wrong time of the wrong days. To be really safe, adjust the time of noon taday back N days to get the right day at noon +/- an hour.

Last edited by DGPickett; 03-22-2013 at 05:25 PM..
# 9  
Old 03-22-2013
Hi alister,

I am using Centos 6.2 and i need to run this command on bash shell,
# 10  
Old 03-22-2013
Quote:
Originally Posted by DGPickett
If you google this site for "tm2tm" you will see the C version of using mktime, localtime and strftime in it, which awk will be very close to. Do you have the GNU awk with its time functions described above. Have you thought out the definition of last Friday for every day of the week? localtime will tell you the day of the week, but you need to calculate how many days back Friday last if from now, run localtime on the current time adjusted that many day, and localtime will give you the day of month. systime -> strftime -> DOW calc -> modified systime value -> strftime -> day of month (and anything else you care to ask for). There are 86400 seconds in a day, and systime is in seconds since 1970 GMT. One pitfall might be the DST shift, if you run this at the wrong time of day. To be really safe, adjust the time of noon taday back N days to get the right day at noon +/- an hour.
That's a lot of hoops to jump through when all of the info is already available in cal's output.

Quote:
Originally Posted by avig
Hi alister,

I am using Centos 6.2 and i need to run this command on bash shell,
I already provided a solution that should work, in my previous post. Here's another that removes the dependency on GNU date.
Code:
cal 03 2013 | awk 'NR==1 {m=substr($1, 1, 3); y=$2} NF>5 {d=$6} END {print "Fri", m, d, y}'

I don't guarantee that it will work with all cal implementations, since cal's output format isn't standardized, but it should work on any Linux.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 11  
Old 03-22-2013
Does your AWK come from GNU? I would guess so. Try some date-time functions and see.
# 12  
Old 03-22-2013
Hi Alister,

Thanks So much for your kind support,

really appreciated.

regards,
Ashish.
# 13  
Old 03-22-2013
Well, he said he wanted to do it inside awk. Does he want the day number of last friday or the last friday of the given month? I get confused so easy some days! Smilie
# 14  
Old 03-22-2013
Hi DGPickett,

Don't be confuse,as i have clear mentioned in my starting thread Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

awk command to retrieve record 23 and 89 from UNIX file

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I am looking for awk command to retrieve only the record number 23 and record number 89 from a unix file?... (6 Replies)
Discussion started by: rakeshp
6 Replies

2. UNIX for Beginners Questions & Answers

awk command to retrieve record 23 and 89 from UNIX file

Hi Everyone, I am looking for awk command to retrieve only the record number 23 and record number 89 from a unix file? Please let me know what is the awk command for this? Regards Rakesh (1 Reply)
Discussion started by: rakeshp
1 Replies

3. Shell Programming and Scripting

awk command to manipulate csv file in UNIX

Hi, I am new to awk and unix programming and trying to manipulate a csv file. My current csv file looks like this: col1,col2,col3,col4,col5,col4,col5,col6,col7,col8 223,V,c,2,4,f,r,,y,z 223,V,c,3,2,f,r,,y,z 223,V,c,1,4,f,r,,y,z 223,V,c,4,3,f,r,,y,z 227,V,c,3,1,f,r,,y,z... (8 Replies)
Discussion started by: Prit Siv
8 Replies

4. Shell Programming and Scripting

awk command to manipulate csv file in UNIX

Hi, I am new to awk/unix and am trying to put together a script to manipulate the date column in a csv file. I have file1.csv with the following contents: Date,ID,Number,Amount,Volume,Size 01-Apr-2014,WERFG,998,105873.96,10873.96,1342.11 01-Apr-2014,POYFR,267,5681.44,5681.44,462.96 I... (2 Replies)
Discussion started by: Prit Siv
2 Replies

5. Shell Programming and Scripting

If else condition inside for loop of awk command in UNIX shell scripting

Hi , Please excuse me for opening a new thread i am unable to find out the syntax error in my if else condition inside for loop in awk command , my actual aim is to print formatted html td tag when if condition (True) having string as "failed", could anyone please advise what is the right... (2 Replies)
Discussion started by: karthikram
2 Replies

6. UNIX for Advanced & Expert Users

awk sub command in Unix

Hi, I am new to unix shell script and I have some trouble on the awk sub I would like to pick the Date "July 10 2012" into $corr_date by using sub() function, but it is not successful. The inputted text file: pic.*.txt July 10 2012 20:30:50 , 1234567.jpg July 10 2012 20:30:52 , 5648978.jpg... (1 Reply)
Discussion started by: meroko
1 Replies

7. Shell Programming and Scripting

awk command in script gives error while same awk command at prompt runs fine: Why?

Hello all, Here is what my bash script does: sums number columns, saves the tot in new column, outputs if tot >= threshold val: > cat getnon0file.sh #!/bin/bash this="getnon0file.sh" USAGE=$this" InFile="xyz.38" Min="0.05" # awk '{sum=0; for(n=2; n<=NF; n++){sum+=$n};... (4 Replies)
Discussion started by: catalys
4 Replies

8. Shell Programming and Scripting

assign a command line argument and a unix command to awk variables

Hi , I have a piece of code ...wherein I need to assign the following ... 1) A command line argument to a variable e.g origCount=ARGV 2) A unix command to a variable e.g result=`wc -l testFile.txt` in my awk shell script When I do this : print "origCount" origCount --> I get the... (0 Replies)
Discussion started by: sweta_doshi
0 Replies

9. Shell Programming and Scripting

How to convert unix command into Awk statement

Hi all, How can i use the below unix command in AWK . Can any one please suggest me how i can use. sed -e "s/which first.sh/which \$0/g" $shell > $shell.sal where $0=current program name(say current.sh) $shell=second.sh (1 Reply)
Discussion started by: krishna_gnv
1 Replies

10. Shell Programming and Scripting

AWK : read unix command

Hi, Is there a way to have something working like the read Unix command to stop a awk ask a question and get the result. I've try system("read a") but it does not work. Thanks. Franck. (6 Replies)
Discussion started by: fwirbel
6 Replies
Login or Register to Ask a Question