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
Triml leading zeros in unix kingofprussia UNIX for Dummies Questions & Answers 7 08-07-2008 05:04 AM
Help needed in padding leading zeros jakSun8 Shell Programming and Scripting 12 07-02-2008 07:41 PM
Removing leading zeros from a variable toshidas2000 Shell Programming and Scripting 6 02-27-2008 01:13 PM
how to retain leading zeros Manish Jha Shell Programming and Scripting 3 11-09-2006 05:59 PM
Leading zeros wtofu Shell Programming and Scripting 1 09-16-2006 02:52 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-30-2007
nvuradi nvuradi is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 23
truncating leading zeros of a column in a file

Hi

I have a file in which I have 5 columns which are delimited by “|” as shown

ABC|12|YAK|METRIC|000000019.5

XYZ|10|ABX|META|000000002.5





Now my requirement is to take the last column trim the leading zero’s for that column values and write back to the same file in the same order.

Any suggestions regarding the same?
is der any file utility do the same?

Thanks
Narendar
  #2 (permalink)  
Old 08-30-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,425
Code:
# awk  'BEGIN{FS=OFS="|"}{$NF=$NF+0}1' file
ABC|12|YAK|METRIC|19.5
XYZ|10|ABX|META|2.5
  #3 (permalink)  
Old 08-30-2007
nvuradi nvuradi is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 23
hi i am getting the following error when i used the awk command

awk 'BEGIN{FS=OFS="|"}{$NF=$NF+0} 1' $filename
Error:
awk: syntax error near line 1
awk: bailing out near line 1


if possible can you explain what is this awk command doing here?

Thanks
Narendar
  #4 (permalink)  
Old 08-30-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,425
Quote:
Originally Posted by nvuradi View Post
hi i am getting the following error when i used the awk command

awk 'BEGIN{FS=OFS="|"}{$NF=$NF+0} 1' $filename
Error:
awk: syntax error near line 1
awk: bailing out near line 1


if possible can you explain what is this awk command doing here?

Thanks
Narendar
change "1" to {print} .
read the man page for the definition of NF. I am bad at explaining, maybe someone is kind enough to help out.
  #5 (permalink)  
Old 08-30-2007
namishtiwari namishtiwari is offline Forum Advisor  
Registered User
  
 

Join Date: Aug 2007
Location: Bangalore
Posts: 377
Hi,
Try this code,its not tested though..

#!/bin/ksh
cat test19 | for i in file
do
cut -d"|" -f5 | tr -s "00" $i
done
  #6 (permalink)  
Old 08-30-2007
namishtiwari namishtiwari is offline Forum Advisor  
Registered User
  
 

Join Date: Aug 2007
Location: Bangalore
Posts: 377
Quote:
Originally Posted by nvuradi View Post
hi i am getting the following error when i used the awk command

awk 'BEGIN{FS=OFS="|"}{$NF=$NF+0} 1' $filename
Error:
awk: syntax error near line 1
awk: bailing out near line 1


if possible can you explain what is this awk command doing here?

Thanks
Narendar
change 1 to {print},its working perfectly fine.
  #7 (permalink)  
Old 08-30-2007
nvuradi nvuradi is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 23
Quote:
Originally Posted by namishtiwari View Post
change 1 to {print},its working perfectly fine.
yeah its printing the contents ..but i want to write it to same file....
Sponsored Links
Closed Thread

Bookmarks

Tags
awk, awk trim, trim, trim awk

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 08:50 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
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