Can sed perform editing operations ONLY in the matched region?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Can sed perform editing operations ONLY in the matched region?
# 1  
Old 03-19-2009
Can sed perform editing operations ONLY in the matched region?

Hi:

Let's suppose I want to replace all the | by > ONLY when | is between []. Usually (and it works) I would do something like

sed -e 's/\(\[[^|]*\)|\([^[]*\]\)/\1>\2/g'

where I have to "save" some portions of the matched region and use them with the \n metacharacter. I was wondering if I could execute something like that

SelectTheFragmentsThat Match(/\[[^]]*\]) and then execute in those pieces s/|/>/g and left the rest of the line unchanged

So, a lot of times I want to perform only editing operations in the region matched, and I want lo left thje rest of the line unchanged. Is that possible?

Thanks a lot

Isi
# 2  
Old 03-19-2009
show a sample file, and show how the output will look like
# 3  
Old 03-19-2009
Original text

AAAA [one|1] BBBB | CCCCC [two|2] DDDDD


desired result

AAAA [one>1] BBBB | CCCCC [two>2] DDDDD

only the | between [] are changed. I know how to perform the chage (see my post) but I would like to know if it exists a more "intuitive" way in sed.

Thanks a lot

Isi
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find a matched pattern and perform comparison on numbers next to it

Hi, I have been trying to extract rows that match pattern "cov" with the value next to it to be > 3. The 'cov' pattern may appear either in $3 or $4 (if using ";" as field separator). Below is the example:- input file ... (7 Replies)
Discussion started by: bunny_merah19
7 Replies

2. Shell Programming and Scripting

How to perform multiple operations on a number before storing to a variable?

(I am using bash) I have a command that will find the line number in a file by searching for a string where it exists. linenumber=$(grep -n "string" $FILENAME | cut -d : -fi) This returns the line number and removes the string. Now that I have the line number I want to subtract 4 from it and... (5 Replies)
Discussion started by: prodigious8
5 Replies

3. Shell Programming and Scripting

Getting lines between patterns and perform operations

Hi, I'm currently dev'ing using awk and I'm currently stuck. Here's the file, with comments on "<--- ": Record <--- First Pattern Amount 1 <--- Amount on first transaction TotalSales 0 <--- Total Sales Prior from previous transactions Time 1:00:00 <--- Time... (9 Replies)
Discussion started by: Jin_
9 Replies

4. Shell Programming and Scripting

awk - sed / reading from a data file and doing algebraic operations

Hi everyone, I am trying to write a bash script which reads a data file and does some algebraic operations. here is the structure of data.xml file that I have; 1 <data> 2 . 3 . 4 . 5 </data> 6 <data> 7 . 8 . 9 . 10</data> etc. Each data block contains same number of lines (say... (4 Replies)
Discussion started by: hayreter
4 Replies

5. Shell Programming and Scripting

Perform operations as a non root user

Hi All, I need a help in the below scenario. I want to perform few operations as a non root user but those operations can be performed only by the root user. For example I need to modify /etc/hosts file as a non root user. This is just one scenario. Could you please provide... (3 Replies)
Discussion started by: kalpeer
3 Replies

6. Shell Programming and Scripting

sed get matched string

Hi, how to figure out script name from a file which are having pattern .ksh. So wherever i have .ksh in a file i should get complete name of the file along with its extension as well. (7 Replies)
Discussion started by: vinsin55
7 Replies

7. Shell Programming and Scripting

loop through files in each folder and perform sed

Dear all, I have few log folders in directory (FILE) and i need to perform sed on each files inside each folders. Here is my script, and i wish to rename each file back to the same file name after modification. Can anyone guide me on my script below? eg: folder1 contain files... (2 Replies)
Discussion started by: ymeyaw
2 Replies

8. Shell Programming and Scripting

Perform Operations on One File Conditional on Data in Another File

Hello all, I am looking for a solution to the following problem. Perl or python solutions also welcome. Given this input: And this input: I want to get this output. The rule being that if the number in the first file is < 0.9, then the corresponding two columns on... (2 Replies)
Discussion started by: hydrabane
2 Replies

9. Shell Programming and Scripting

Using Sed to perform multiple substitutions?

Hello I have the following output which is returned with the Month in text format instead of numerical. The output I receive is performed by using Rational Synergy CM software commands from the Unix command line and piping Unix commands on the end. bash-3.00$ ccm query -n... (4 Replies)
Discussion started by: Glyn_Mo
4 Replies

10. Shell Programming and Scripting

Sed grep the first matched lines

Hi I have a myfile.txt contains the following: CONTEXT { AAAAA } ... CONTEXT { BBBBB } I want to extract the lines in between CONTEXT { ... }, one by one. Hence I wrote a command like the following, sed -n '/^CONTENT/,/^}/ { w a.txt }' myfile.txt The problem with this... (5 Replies)
Discussion started by: hezjing
5 Replies
Login or Register to Ask a Question