The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
If statement - How to write a null statement april Shell Programming and Scripting 3 04-16-2008 01:14 PM
AWK - conditional cause Rafael.Buria Shell Programming and Scripting 2 01-28-2008 01:24 PM
conditional statement lalelle Shell Programming and Scripting 8 08-21-2007 08:57 AM
quoting in conditional statement 3Gmobile Shell Programming and Scripting 2 08-14-2006 03:14 AM
more elegant way for conditional statement needed candyflip2000 Shell Programming and Scripting 3 05-18-2006 07:52 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-08-2006
3Gmobile 3Gmobile is offline
Registered User
  
 

Join Date: Aug 2006
Posts: 18
awk conditional statement

how can i use awk or sed to do a conditional statement, so that
HH:MM
if MM not great than 30 , then MM=00
else MM=30

ie:
10:34 will display 10:30
10:29 will display 10:00

a=$(echo 10:34 | awk ......)


Thanks in advance
  #2 (permalink)  
Old 08-08-2006
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
Supporter
  
 

Join Date: Dec 2004
Location: Singapore
Posts: 2,350
Use awk. You can't use sed (atleast to my knowledge).
Code:
echo "10:34" | nawk -F: '{if($2<30) printf("%d:00",$1)
else printf("%d:30",$1)}'
  #3 (permalink)  
Old 08-08-2006
yll yll is offline
Registered User
  
 

Join Date: Aug 2006
Posts: 2
echo HH:MM | awk -F: '{if ($2>30) { New_mm=30 } else {New_mm=00}; print $1":"New_mm}'

Have fun!!!
  #4 (permalink)  
Old 08-08-2006
3Gmobile 3Gmobile is offline
Registered User
  
 

Join Date: Aug 2006
Posts: 18
thank !

but how can i make the output to be 00 instead of only contain one 0
  #5 (permalink)  
Old 08-08-2006
3Gmobile 3Gmobile is offline
Registered User
  
 

Join Date: Aug 2006
Posts: 18
oh, thanks ! i got it...
  #6 (permalink)  
Old 08-08-2006
3Gmobile 3Gmobile is offline
Registered User
  
 

Join Date: Aug 2006
Posts: 18
sorry
how if my input field is HH:MM:XX:YY:ZZ..... and i want to replace the MM with either 00 or 30 ?

thanks
  #7 (permalink)  
Old 08-08-2006
mukundranjan mukundranjan is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 20
Hi friend,

I have written below 2 line of code,
1. create a file with of diffrent value that we will use for testing.
2. Cat that file and check for the second parameter if it is greater than 30 then it will print 30. else it will print 00. You can put any value in place of other parameter.

Code:
1.  for((i=0;i<60;i++)); do echo "HH:$i:XX:YY:ZZ" >> tmp.txt; done
2. cat tmp.txt | awk -F":" '{ if($2<30) printf("%s:00:%s:%s:%s\n",$1,$3,$4,$5);} { if($2>30) printf("%s:30:%s:%s:%s\n",$1,$3,$4,$5);} '
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:07 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0