Solved - Sed - Positional Replace


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Solved - Sed - Positional Replace
# 1  
Old 05-20-2010
CPU & Memory Solved - Sed - Positional Replace

Hi All,

I don't know what I am doing wrong in the regex below.

I have the following string:
31000000010201005181121360000000100000000003000000YYY-YYY-YYY-20100518-104139.txt.YYY

I need to split it in parts:
- Group 1: 3100000001020100518112136
- Group 2: 000000010
- Group 3: 0000000003000000
- Group 4: YYY-YYY-YYY-20100518-104139.txt.YYY

Check below my sed regex execution:
Code:
$ a="31000000010201005181121360000000100000000003000000YYY-YYY-YYY-20100518-104139.txt.YYY"

$ echo "${a}" | sed 's/\(31[0-9]\{23\}\)\([0-9]\{9\}\)\([0-9]\{16\}\)\(.*$\)/\2xxx\3/g'

# The result:
31000000010201005181121360000000100000000003000000YYY-YYY-YYY-20100518-104139.txt.YYY

Why it prints the whole string instead of printing only the groups I asked? What I am doing wrong?

Thanks a lot!

---------- Post updated at 04:21 PM ---------- Previous update was at 03:51 PM ----------

Ok! I am going to answer my own question! Hahah... Sorry!

After doing some tests and splitting my regex in parts I found that the problem was in the $ in the last group: (.*$).

You can use the following regex, it worked!

Code:
echo "${a}" | sed 's/^\(31[0-9]\{23\}\)\([0-9]\{9\}\)\([0-9]\{16\}\)\(.*\)/\1XX\2XX\3XX\4/g'

Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

'sed' with Positional Parameters

I'm new with 'sed' and for sure something still I don't understand yet with it. If you see my output on ">Output..." portion, the new directory still on "source_dir" instead of "dest_dir". You may disregard for the "tar" part, this is just a test script, just for me to understand 'sed' using the... (3 Replies)
Discussion started by: daryl0505
3 Replies

2. UNIX for Dummies Questions & Answers

[Solved] Replace the second pattern alone in VI.

I have lines like: table10 table_name_10 table10 table_name_10 table20 table_name_20 table20 table_name_20 table30 table_name_30 table30 table_name_30 I want to change the second "table_names" in all lines to test_table_name. Required output would be: table10 table_name_10 table10... (19 Replies)
Discussion started by: gctex
19 Replies

3. Shell Programming and Scripting

[Solved] sed - how replace date in script

Hi All, I have a requirement to find and replace old date with new date value. Below is the scenario: # In the input file, date is MM/DD/YYYY format PREV_DTE=09/15/2013 I want to replace with 09/30/2013. It should look like PREV_DTE=09/30/2013 I am using below sed command :... (4 Replies)
Discussion started by: rockygsd
4 Replies

4. Shell Programming and Scripting

[Solved] sed to replace words

Hello All, I have file named filelist.txt a.bteq.ctl b.bteq.ctl c.bteq.ctl I want to replace the word bteq to tpt in this file. I used this sed command cat filelist.txt | sed 's/bteq/tpt/g' > filelist.txt But this command deletes all records from the filelist.txt Can... (2 Replies)
Discussion started by: nnani
2 Replies

5. UNIX for Dummies Questions & Answers

[Solved] Replace NA with column mean

Hi experts, I'm looking for a unix solution to replacing "NA" within a matrix with the mean of the column: $cat file ID a b c d day 10 5 100 50 cat 20 6 200 50 dog NA 8 NA 50 car 15 NA NA ... (3 Replies)
Discussion started by: torchij
3 Replies

6. Shell Programming and Scripting

[Solved] Replace records according to reference

Dear All, I am struggling with the following task and would appreciate some help. I have a large table (file1.txt). The first column of this table contains an ID. I would like to replace the ID with a label according to a reference file. Here is an example: cat infile.txt 0 AJ2312 310 ... (7 Replies)
Discussion started by: GDC
7 Replies

7. Programming

[Solved] SQL SELECT REPLACE

Hi All, I had a query related to sql select update replace command. i have a table named clusters and it looks like this name model characteristics sample1.1 +123 parent sample1.2 -456 clone sample1.3 +122 ... (5 Replies)
Discussion started by: sonia102
5 Replies

8. Shell Programming and Scripting

[Solved] sed : last character replace

Hi all , I have to write a shell script that takes a number as input , like 123 and the output will be 6 ,i.e the output will be the sum of digits of the input. I have an idea as follows, echo "123"|fold -1|tr '\n' '+'|bc But the problem is after " echo "123"|fold -1|tr '\n' '+' "... (5 Replies)
Discussion started by: M.Choudhury
5 Replies

9. UNIX for Dummies Questions & Answers

[Solved] Vi - replace words with points (.)

Hello guys, I'm trying to replace the word "i.e." for "ie." in Vi but everytime I used the search tool for start looking for it (this is: /i.e.), it finds every word that contains the "i" and "e" word. I tried the following command: :%s/i.e./ie./g However, it doesn't work. Any help... (2 Replies)
Discussion started by: Gery
2 Replies

10. Shell Programming and Scripting

AWK: replace single positional character given variables

I already have accomplished this task using sed and arrays, but since I get the variable using awk, I figured I'd ask this question and maybe I can get a cleaner solution using strictly awk.. I just can't quite grasp it in awk. Story: I'm automating the (re)configuration of network interfaces,... (3 Replies)
Discussion started by: System Shock
3 Replies
Login or Register to Ask a Question