LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-05-2014, 12:47 AM   #1
santosh0782
Member
 
Registered: Nov 2013
Posts: 132

Rep: Reputation: Disabled
How to remove leading zeros in a function


Hi,

I have a File1 with data as:
cat File1:
Code:
<row>
 <OWNER>SAN</OWNER>
 <AREA>GLOBAL BUSINESS SOLUTIONS</AREA>
 <GROUP>TRANSACTION CLEARING SYSTEM</GROUP>
 <SERVICE>TRANSACTION CLEARING SYSTEM</SERVICE>
 <KPI>STST401</KPI>
 <STATE>0.0</STATE>
 <LASTUPDATED>1393971372739</LASTUPDATED>
 <DESCRIPTION>GOOD RUN , 0000991776 , 000092743794 Tue 04/03/14 22:16</DESCRIPTION>
 <SOURCE>COC</SOURCE>
 <CITYPE>KPI</CITYPE>
</row>
i have written a function to pull the details as:
Code:
Good_Job_With_Count_Info()
{
  # get specific info from rtView
  jobTarget=( $( grep -A3 "STST401" "File1" | tail -1 | tr '[><]' ' ' ) )


        jb_rtv_date1=${jobTarget[8]}  #Date DD/MM/YY
        jb_rtv_time1=${jobTarget[9]} #Time HH:MM
        jb_rtv_status=${jobTarget[1]} can we take "GOOD RUN"  for this field from the File1 
        jb_rtv_day=${jobTarget[7]}
        jb_rtv_hour=${jobTarget[9]%:*}
        jb_rtv_min=${jobTarget[9]#*:}
        jb_rtv_count=${jobTarget[4]} For this field, how could we remove the leading zeros
        jb_rtv_amount=${jobTarget[6]} For this field, how could we remove the leading zeros 

        #echo "jb_rtv_date1=${jb_rtv_date1}"
        #echo "jb_rtv_time1=${jb_rtv_time1}"
        #echo "jb_rtv_status=${jb_rtv_status}"
        #echo "jb_rtv_day=${jb_rtv_day}"
        #echo "jb_rtv_hour=${jb_rtv_hour}"
        #echo "jb_rtv_min=${jb_rtv_min}"
        #echo "jb_rtv_count=${jb_rtv_count}"
        #echo "jb_rtv_amount=${jb_rtv_amount}"
}
 
Old 03-05-2014, 12:58 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
Code:
A=000234
echo ${A##*0}
instead of jobTarget=( $( grep -A3 "STST401" "File1" | tail -1 | tr '[><]' ' ' ) ) I would rather use a single awk.
 
Old 03-05-2014, 01:19 AM   #3
santosh0782
Member
 
Registered: Nov 2013
Posts: 132

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
Code:
A=000234
echo ${A##*0}
instead of jobTarget=( $( grep -A3 "STST401" "File1" | tail -1 | tr '[><]' ' ' ) ) I would rather use a single awk.

how could we use single awk for the above?
 
Old 03-05-2014, 01:26 AM   #4
santosh0782
Member
 
Registered: Nov 2013
Posts: 132

Original Poster
Rep: Reputation: Disabled
As suggested you in post#2
have written code like this:
Code:
{
  # get specific info from rtView
  jobTarget=( $( grep -A3 "STST401" "File1" | tail -1 | tr '[><]' ' ' ) )


        jb_rtv_date1=${jobTarget[8]}  #Date DD/MM/YY
        jb_rtv_time1=${jobTarget[9]} #Time HH:MM
        jb_rtv_status=${jobTarget[1]}
        jb_rtv_day=${jobTarget[7]}
        jb_rtv_hour=${jobTarget[9]%:*}
        jb_rtv_min=${jobTarget[9]#*:}
        jb_rtv_count=${jobTarget[4]##*0} This is not working properly 
         jb_rtv_amount=${jobTarget[6]##*0}This is not working properly 

        #echo "jb_rtv_date1=${jb_rtv_date1}"
        #echo "jb_rtv_time1=${jb_rtv_time1}"
        #echo "jb_rtv_status=${jb_rtv_status}"
        #echo "jb_rtv_day=${jb_rtv_day}"
        #echo "jb_rtv_hour=${jb_rtv_hour}"
        #echo "jb_rtv_min=${jb_rtv_min}"
        echo "jb_rtv_count=${jb_rtv_count}"
        echo "jb_rtv_amount=${jb_rtv_amount}"
}
 
Old 03-05-2014, 01:48 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
Code:
awk ' BEGIN { FS="[<> ,:]*" }   # I do not really know what kind of separator do you need
      /STST401/ { l=NR+3 };
      NR == l { print $8" "$9" "$2" "$6+0" "$7}' file1 # here $6+0 will remove leading 0
you should adjust the print line to fit your needs
 
1 members found this post helpful.
Old 03-05-2014, 03:53 AM   #6
santosh0782
Member
 
Registered: Nov 2013
Posts: 132

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
Code:
awk ' BEGIN { FS="[<> ,:]*" }   # I do not really know what kind of separator do you need
      /STST401/ { l=NR+3 };
      NR == l { print $8" "$9" "$2" "$6+0" "$7}' file1 # here $6+0 will remove leading 0
you should adjust the print line to fit your needs


ok..Thanks a lot :-)

but in this code can we remove leading zeros:
Code:
{
  # get specific info from rtView
  jobTarget=( $( grep -A3 "STST401" "File1" | tail -1 | tr '[><]' ' ' ) )


        jb_rtv_date1=${jobTarget[8]}  #Date DD/MM/YY
        jb_rtv_time1=${jobTarget[9]} #Time HH:MM
        jb_rtv_status=${jobTarget[1]}
        jb_rtv_day=${jobTarget[7]}
        jb_rtv_hour=${jobTarget[9]%:*}
        jb_rtv_min=${jobTarget[9]#*:}
        jb_rtv_count=${jobTarget[4]##*0} Here,This is not working can we remove leading zeros for this 4th field  
         jb_rtv_amount=${jobTarget[6]##*0}Here,This is not working can we remove leading zeros for this 6th field 

        #echo "jb_rtv_date1=${jb_rtv_date1}"
        #echo "jb_rtv_time1=${jb_rtv_time1}"
        #echo "jb_rtv_status=${jb_rtv_status}"
        #echo "jb_rtv_day=${jb_rtv_day}"
        #echo "jb_rtv_hour=${jb_rtv_hour}"
        #echo "jb_rtv_min=${jb_rtv_min}"
        echo "jb_rtv_count=${jb_rtv_count}"
        echo "jb_rtv_amount=${jb_rtv_amount}"
}

Last edited by santosh0782; 03-05-2014 at 05:34 AM.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH: Keeping/replacing leading zeros with BC Checksumfail Programming 3 07-20-2012 03:29 PM
How to add leading zeros to numbers for file names rootaccess Linux - General 20 06-16-2012 07:35 PM
[SOLVED] Bash: remove leading spaces with only expansions romagnolo Linux - General 15 02-13-2012 06:39 AM
Renaming files (adding leading zeros) General Linux - Software 3 01-09-2011 09:17 AM
[SOLVED] bash script trying to remove a leading ' and a tailing ' spartiati Programming 25 03-30-2010 03:33 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration