How to change a segment in a particular position


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to change a segment in a particular position
# 1  
Old 07-20-2009
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  
Old 07-20-2009
Code:
sed 's/^\([^*][^*]*[*][^*][^*]*[*][^*][^*]*[*][^*][^*]*\)[*]00*\([^*][^*]*\)[*]00*\(\..*\)/\1*\2*\3/'

I think that should work with most any sed variant.
# 3  
Old 07-20-2009
Try this:
Code:
xx='XYZ*04567472*0099*020091231*0123*0.12'
echo $xx | sed 's/\(\([^\*]*\*\)\{4\}\)0*\([^\*]*\*\)0*\(.*\)/\1\3\4/'

# 4  
Old 07-20-2009
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  
Old 07-20-2009
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  
Old 07-20-2009
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  
Old 07-21-2009
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 Scott; 07-21-2009 at 05:05 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search words in any quote position and then change the words

hi, i need to replace all words in any quote position and then need to change the words inside the file thousand of raw. textfile data : "Ninguno","Confirma","JuicioABC" "JuicioCOMP","Recurso","JuicioABC" "JuicioDELL","Nulidad","Nosino" "Solidade","JuicioEUR","Segundo" need... (1 Reply)
Discussion started by: benjietambling
1 Replies

2. UNIX for Beginners Questions & Answers

Delete columns with a specific title XXX, where the position change in each file

Goodmorning, I know how to cut a string and a column, and how to find a word. I have a file with over 100 columns. All columns have a title in the first line. I have to delete all columns with the XXX title. I can't use cut -f because the position of XXX columns change in each file, and in... (14 Replies)
Discussion started by: echo manolis
14 Replies

3. Shell Programming and Scripting

Search for a string at a particular position and replace with blank based on position

Hi, I have a file with multiple lines(fixed width dat file). I want to search for '02' in the positions 45-46 and if available, in that lines, I need to replace value in position 359 with blank. As I am new to unix, I am not able to figure out how to do this. Can you please help me to achieve... (9 Replies)
Discussion started by: Pradhikshan
9 Replies

4. Shell Programming and Scripting

Need command or script to print all lines from 2nd position to last but one position

hi guys, i want command or script to display the content of file from 2nd position to last but one position of a file abcdefghdasdasdsd 123,345,678,345,323 434,656,656,656,656 678,878,878,989,545 4565656667,65656 i want to display the same above file without first and... (2 Replies)
Discussion started by: hemanthsaikumar
2 Replies

5. Shell Programming and Scripting

Change text at a variable position

Hi there script guru's I have an input file /tmp/in.txt of which the data is seperated by a : (example of the data) test1:zz:2000:2000:zzz te:a:2000:3333:bbb testabs:x:2004:3000:cccc I would like to run a scrip (bash) changing the data after the second ":" example Test for the value of... (3 Replies)
Discussion started by: KobusE
3 Replies

6. UNIX for Dummies Questions & Answers

Change a character based on its position number

Hi I have a text file that I want to change some of the characters based on their position. My file contain multiple lines and characters should be counted continuously line by line. For example, I want to convert the 150th T to C. What can I do? Here is a portion of my file:... (10 Replies)
Discussion started by: a_bahreini
10 Replies

7. Shell Programming and Scripting

position change

Hi All, input file: echo "a|b|c|d|test|123|same" | awk 'BEGIN {FS=OFS="|"} {print $1,$2,$7,$4,$5,$6}' Got Output : a|b|same|d|test|123 This is the sample input file with less fields. But my original file contains more field values. How to replace last value to 3rd postion value. Because... (8 Replies)
Discussion started by: Jairaj
8 Replies

8. Programming

Data segment or Text segment

Hi, Whether the following piece of code is placed in the read-only memory of code (text) segment or data segment? char *a = "Hello"; I am getting two different answers while searching in google :( that's why the confusion is (7 Replies)
Discussion started by: royalibrahim
7 Replies

9. Shell Programming and Scripting

Change many columns position/order

Hi everyone, Please some help over here. (I´m using cygwing) I have files with 40 columns and 2000 lines in average. I´m trying to change the order position as follow. Original columns position:... (3 Replies)
Discussion started by: cgkmal
3 Replies

10. Shell Programming and Scripting

Change Position of word character

Hi, I have following format in file aaa with content below, and would like to seek help from forumer about how to change and swap the position on 2nd field. 5874957|901125| 95874960|650614| 95874966|870308| 901125 to be changed as 25-11-1990 for eg Can someone help please ?? :) ... (6 Replies)
Discussion started by: cedrichiu
6 Replies
Login or Register to Ask a Question