The UNIX and Linux Forums  

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
code segment trob UNIX for Dummies Questions & Answers 1 04-23-2009 09:33 PM
find if a position is between a given start and end position fadista UNIX for Dummies Questions & Answers 9 12-04-2008 12:17 PM
how to find a position and print some string in the next and same position naveenkcl Shell Programming and Scripting 1 08-21-2008 02:18 PM
Change Position of word character cedrichiu Shell Programming and Scripting 6 03-12-2007 01:52 AM
Segment Fault zhshqzyc High Level Programming 9 04-07-2006 12:47 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-20-2009
ananthmm ananthmm is offline
Registered User
  
 

Join Date: Aug 2006
Posts: 6
How to change a segment in a particular position

I need help in removing a leading zero in a particular position.
For eg.: XYZ*04567472*0099*020091231*0123*0.12

In the above line, I want to replace "*0123" with "123" and "0.12" with ".12". I want to remove the leading zero only in position number 4 and 5 (the bolded segments)

I was able to replace 0.12 with .12 using --- sed 's/\*00*\./\*\./g'

But, I am unable to replace "0123" with "123". I tried using
sed '/^XYZ\*/s/\*00*/\*/4'. But it is not working.
Using, sed '/^XYZ\*/s/\*00*/\*/g' removes all leading zeros (which I dont want).

Any help with this is much appreciated.

Thanks,
Ananth.
  #2 (permalink)  
Old 07-20-2009
cjcox cjcox is offline
Registered User
  
 

Join Date: May 2005
Posts: 64
Code:
sed 's/^\([^*][^*]*[*][^*][^*]*[*][^*][^*]*[*][^*][^*]*\)[*]00*\([^*][^*]*\)[*]00*\(\..*\)/\1*\2*\3/'
I think that should work with most any sed variant.
  #3 (permalink)  
Old 07-20-2009
edidataguy edidataguy is offline
Registered User
  
 

Join Date: Jul 2009
Posts: 173
Try this:
Code:
xx='XYZ*04567472*0099*020091231*0123*0.12'
echo $xx | sed 's/\(\([^\*]*\*\)\{4\}\)0*\([^\*]*\*\)0*\(.*\)/\1\3\4/'
  #4 (permalink)  
Old 07-20-2009
ananthmm ananthmm is offline
Registered User
  
 

Join Date: Aug 2006
Posts: 6
cjcox, thanks, but sorry it is not working. :-(. I tried replacing \1\2\3 with other combinations, but still does not work.
edidataguy, yes it does work. I have a couple of questions:
1. (\([^\*]*\*\)\{4\}\) --> This means that we are searching for 4 occurrences of "*" from the beginning. After this we search for "0". But what is the last "*" for?
([^\*]*\*\ <---
2. Why do you use \1\3\4, why not \1\2\3\4?

---------- Post updated at 07:56 PM ---------- Previous update was at 07:41 PM ----------

Unfortunately, when I was testing it, I ran into some issues.

XYZ*004567472*0099*0020091231*00001*00012.50 became
XYZ*004567472*00*0020091231*1*12.50 (lost the 99 in the 2nd segment)

XYZ*03988651*099*020091231*01*012.50 became
XYZ*03988651*09*020091231*1*12.50

XYZ*03676802*00099*20091231*001*0012.50 became
XYZ*03676802*00*20091231*1*12.50 (seems to be retaining only 2 digits in the 2nd segment)
  #5 (permalink)  
Old 07-20-2009
ryandegreat25 ryandegreat25 is offline
Registered User
  
 

Join Date: Jul 2009
Posts: 236
try awk.. anyone can make it shorter?

Code:
-bash-3.2$ cat test
XYZ*04567472*0099*020091231*0123*0.12
-bash-3.2$ awk -F'*' '{printf $1"*"$2"*"$3"*"$4"*"substr($5,2)"*"substr($6,2)}' test
XYZ*04567472*0099*020091231*123*.12
-bash-3.2$
  #6 (permalink)  
Old 07-20-2009
edidataguy edidataguy is offline
Registered User
  
 

Join Date: Jul 2009
Posts: 173
This will need a lot of expl. to make. Google the net.
I will try my level best to explain.

1. (\([^\*]*\*\)\{4\}\) --> This means that we are searching for 4 occurrences of "*" from the beginning.
Yes. (Actually, I missed the "From the bginning" part. For your case, it does not matter. Still I have added it.)
Code:
echo $xx | sed 's/^\(\([^\*]*\*\)\{4\}\)0*\([^\*]*\*\)0*\(.*\)/\1\3\4/'
1b. After this we search for "0".
Yes.

1c. But what is the last "*" for? ([^\*]*\*\ <---
This is the part that says 'Search for chars that are not "*"' -->[^\*]
Any number of the above --> *
Followed by a literal * --> \* (\* go togather)

2. Why do you use \1\3\4, why not \1\2\3\4?
Each "(" in the FIRST part is represented by a number in the SECOND part. (s/FIRST/SECOND/)
In "s/^\(\([^\*]*\*\)\{4\}\)" I am skipping the second \2 because it is with in the first "\(\(".
You go ahead and add the "\2" also, as "\1\3\2\4", you will understand.

3. Unfortunately, when I was testing it, I ran into some issues.
I have no issues, it works fine for me.
You sure you have not missed something?
Try again with the new code. (I doubt it.)
  #7 (permalink)  
Old 07-21-2009
scottn scottn is online now Forum Advisor  
VIP Member
  
 

Join Date: Jun 2009
Location: Zürich, CH
Posts: 1,073
To remove first leading 0
Code:
awk -F'*' -v OFS="*" '{sub( /^0/, "", $5); sub( /^0/, "", $6); print }'
Or to remove all leading 0's
Code:
awk -F'*' -v OFS="*" '{sub( /^0*/, "", $5); sub( /^0*/, "", $6); print }'

Last edited by scottn; 07-21-2009 at 05:05 AM..
Reply

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


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