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
removing leading zero risshanth UNIX for Dummies Questions & Answers 0 02-08-2008 06:51 AM
append zero's in the begining ganapati UNIX for Dummies Questions & Answers 3 01-17-2008 12:41 PM
Aleri is Not the Leading CEP Vendor, Sorry. iBot Complex Event Processing RSS News 0 12-07-2007 06:20 AM
Leading zeros wtofu Shell Programming and Scripting 1 09-16-2006 02:52 PM
Leading and Trailing Spaces sleepster Shell Programming and Scripting 7 10-29-2003 11:48 PM

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 12-19-2003
dbrundrett dbrundrett is offline
Registered User
  
 

Join Date: Jan 2002
Posts: 60
Leading Zero's

I'm trying to write a script that compare's some numeric values in a file, but in one of the files the value always has leading zero's. For example -

temp_8719_LM344_20031216091826: printera : 552 : 276 : 552 : 276

temp_8719_LM344_20031216091826: printera : 000552 :000276 : 000552 : 000276

How or what coding can I use to ignore these leading zeros?
  #2 (permalink)  
Old 12-19-2003
malcom's Avatar
malcom malcom is offline
Registered User
  
 

Join Date: Jul 2003
Location: Germany
Posts: 78
Good Morning,

well it depends on how many digits your numbers are max. If you can say, that you have only numbers with maxium 10 digits, you can read the values, and convert them to your maxiumum digit size with leading zeros, so you will have on both sides the same format and you can easily compare.

If you don't have a maxium size of digits, you should read the values as string first and use awk to remove leading zeros.

Regards
malcom
  #3 (permalink)  
Old 12-19-2003
dbrundrett dbrundrett is offline
Registered User
  
 

Join Date: Jan 2002
Posts: 60
Thanks for your reply

What is the best and most efficient coding to use in awk?
  #4 (permalink)  
Old 12-19-2003
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,407
In awk...

awk '{gsub(": *0*",": ")};1' $InFile > $OutFile

In sed...

sed 's/: *0*/: /g' $InFile > $OutFile

However, this conversion is not necessary if you are comparing numbers in a script when you use "-eq" and "-ne" instead of "=" and "!="
  #5 (permalink)  
Old 12-19-2003
dbrundrett dbrundrett is offline
Registered User
  
 

Join Date: Jan 2002
Posts: 60
Thanks very much
  #6 (permalink)  
Old 12-19-2003
google's Avatar
google google is offline Forum Advisor  
Moderator
  
 

Join Date: Jul 2002
Location: Atlanta
Posts: 740
However, this conversion is not necessary if you are comparing numbers in a script when you use "-eq" and "-ne" instead of "=" and "!="

Ygor brings up a good point that many people either forget or ignore. Integer comparison uses -eq, -lt, -gt, -ge etc while string comparison is done using = !=
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:27 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