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
# 1  
Old 03-22-2013
Help me for using the awk command in UNIX

Hi,

Can you please help me to find/display out last Friday's date of the month using awk command in Unix/Linux.

i have found this command

Code:
cal 03 2013 | awk 'NF>5{last = $6} END{print last}'

it show the output in single digit date like :29

But i need its output will shown in Fri Mar 29 2013

Please help.

Last edited by vbe; 03-22-2013 at 03:52 PM.. Reason: code tags
# 2  
Old 03-22-2013
You don't need awk for this requirement.

If you have ksh93, it has built-in printf that supports %T formatting option:
Code:
#!/bin/ksh93

printf "%(%a %b %d %Y)T\n" "last friday in Mar 2013"

# 3  
Old 03-22-2013
No i don't wana to use Ksh shell.
# 4  
Old 03-22-2013
Are you limited to awk or can you use other unix tools? If not, why? Is this a homework assignment? Also, what operating systems must this support? Just GNU/Linux?

Finally, thank you for starting your own thread. Everything works much better this way.

Regards,
Alister
# 5  
Old 03-22-2013
Awk has very C like functions for time. You can get the time, reformat it into components, adjust the day and date, make time and reformat it into components again. For instance, if it is Saturday, last Friday might be defined as yesterday, or 8 days back. Time Functions - The GNU Awk User's Guide Which awk or O/S is this?
# 6  
Old 03-22-2013
No its not my homework assignment,this task have assigned by my senior,

I just want to need modification in awk because output should be shown in
[Fri Mar 29 2013] this format.

another solution/way are most welcome (without ksh) to resolve my below mentioned queries.

To find/display out last Friday's date of the month
# 7  
Old 03-22-2013
When you ask for help, please provide information regarding the operating system and shell, at minimum. When this information is not provided, some members will not help you because it's very possible that the effort they would devote to your solution would be a waste of time. I asked you for this information above but you have still not provided it.

I will assume you are using a Linux distro. If my solution doesn't work, then you must provide the information requested.

Building from your initial approach:
Code:
m=03
y=2013
d=$(cal $m $y | awk 'NF>5 {fri=$6} END {print fri}')
date -d "$y$m$d" +'%a %b %d %Y'

Regards,
Alister
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