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
# 15  
Old 03-22-2013
Yes, last Friday is the Friday of a week ago, which is -2 days on Sunday and -8 days on Saturday, but watch out near midnight on the DST shift weekends. If you have no DST, that is simpler.
# 16  
Old 03-23-2013
Hi Alister,

Provided code is working fine (except day format)

Code:
cal 03 2013 | awk 'NR==1 {m=substr($1, 1, 3); y=$2} NF>5 {d=$6} END {print "Fri", m, d, y}'

e:g-when we change d=$6 to d=$5 its show the correct date but day output os not ok its show

Fri Mar 28 2013

But it need to show Thu Mar 28 2013
# 17  
Old 03-23-2013
What about trying to understand alister's proposal, and coming up with a solution yourself? That would be very educational!

While doing that, you could compare your attempts to e.g.
Code:
DayOfWeek=5

cal 03 2013 |
awk     'NR==1  {m = substr($1, 1, 3); y = $2}
         NR==2  {DN = $DOW}
         NF>5   {d = $DOW} END {print DN, m, d, y}
        ' DOW=$DayOfWeek
Do Mä 28 2013                                               # my current locale

# 18  
Old 03-23-2013
Hi Rudic,

Thanks for the reply and giving important tips,

Code:
cal 03 2013 | awk 'NR==1 {m=substr($1, 1, 3); y=$2} NF>5 {d=$5} END {print "Fri", m, d, y}'

Please provide the solution when i hit this above mentioned command on my bash shell its output show

Fri Mar 28 2013

But the required output should be

Thu Mar 28 2013

Note:-i don't want to change manually code from {print "Fri", m, d, y} to {print "Thu", m, d, y} for correct output.
can its should be automatically?
# 19  
Old 03-23-2013
post#17?
# 20  
Old 03-23-2013
Thanks Rudic,

Smilie
# 21  
Old 03-25-2013
Hi All,

I have found the scripts,and works same as required,
Code:
#!/bin/bash

## Adjust these to taste (e.g. populate from command line):
daynum=${1:-6}
monthnum=04
year=2013

months=( '' Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec )
days=( '' Sun Mon Tue Wed Thu Fri Sat )
monthnum=${monthnum#0}

date=$(
cal "$monthnum" "$year" |
awk -v daynum="$daynum" 'NF>daynum-1 {last = $daynum} END{print last}'
)
printf "%s %s %d %d\n" "${days[daynum]}" "${months[monthnum]}" "$date" "$year"

Its work charmm!!!!!!!Smilie

Last edited by Franklin52; 03-25-2013 at 04:12 AM.. Reason: Please use code tags for data and code samples
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