Sponsored Content
Top Forums Programming find & Replace text using two non-unique delimiters. Post 303013822 by Don Cragun on Wednesday 28th of February 2018 10:02:43 PM
Old 02-28-2018
We assume that you know that exactly the same thing works in awk:
Code:
echo "This html code <text blah >contains <garbage blah blah >. All tags must go, <text > but some must be replaced with <garbage blah blah > without easing other info." |
    awk '{sub("<garbage[^>]*>", "important info")}1'

to make a substitution for the first occurrence producing the output:
Code:
This html code <text blah >contains important info. All tags must go, <text > but some must be replaced with <garbage blah blah > without easing other info.

or:
Code:
echo "This html code <text blah >contains <garbage blah blah >. All tags must go, <text > but some must be replaced with <garbage blah blah > without easing other info." |
    awk '{gsub("<garbage[^>]*>", "important info")}1'

to make a substitution for all occurrences producing the output:
Code:
This html code <text blah >contains important info. All tags must go, <text > but some must be replaced with important info without easing other info.

This User Gave Thanks to Don Cragun For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find & Replace

I get a text file with 70+ columns (seperated by Tab) and about 10000 rows. The 58th Column is all numbers. But sometimes 58th columns has "/xxx=##" after the numeric data. I want to truncate this string using the script. Any Ideas...:confused: (3 Replies)
Discussion started by: gagansharma
3 Replies

2. Shell Programming and Scripting

find & incremental replace?

Looking for a way using sed/awk/perl to replace port numbers in a file with an incrementing number. The original file looks like... Host cmg-iqdrw3p4 LocalForward *:9043 localhost:9043 Host cmg-iqdro3p3a LocalForward *:10000 localhost:10000 Host cmg-iqdro3p3b LocalForward... (2 Replies)
Discussion started by: treadwm
2 Replies

3. Shell Programming and Scripting

get part of file with unique & non-unique string

I have an archive file that holds a batch of statements. I would like to be able to extract a certain statement based on the unique customer # (ie. 123456). The end for each statement is noted by "ENDSTM". I can find the line number for the beginning of the statement section with sed. ... (5 Replies)
Discussion started by: andrewsc
5 Replies

4. Shell Programming and Scripting

Find & Replace string in multiple files & folders using perl

find . -type f -name "*.sql" -print|xargs perl -i -pe 's/pattern/replaced/g' this is simple logic to find and replace in multiple files & folders Hope this helps. Thanks Zaheer (0 Replies)
Discussion started by: Zaheer.mic
0 Replies

5. Homework & Coursework Questions

[Scripting]Find & replace using user input then replacing text after

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: (o) Checkout an auto part: should prompt the user for the name of the auto part and borrower's name: Name:... (2 Replies)
Discussion started by: SlapnutsGT
2 Replies

6. Shell Programming and Scripting

Find and add/replace text in text files

Hi. I would like to have experts help on below action. I have text files in which page nubmers exists in form like PAGE : 1 PAGE : 2 PAGE : 3 and so on there is other text too. I would like to know is it possible to check the last occurance of Page... (6 Replies)
Discussion started by: lodhi1978
6 Replies

7. Red Hat

copy & replace text

how can i copy a certain word from a text file then use this word to replace in another text file?? i tried to use something like: awk '{print "Hit the",$1,"with your",$2}' /aaa/qqqq.txt > uu.txt but i can't add an argument to point to the second file which i will replace in. please... (8 Replies)
Discussion started by: mos33
8 Replies

8. UNIX for Dummies Questions & Answers

Find & Replace

Hi I am looking to rename the contents of this dir, each one with a new timestamp, interval of a second for each so it the existing format is on lhs and what I want is to rename each of these to what is on rhs..hopefully it nake sense CDR.20060505.150006.gb CDR.20121211.191500.gb... (3 Replies)
Discussion started by: rob171171
3 Replies

9. Shell Programming and Scripting

Finding a text in files & replacing it with unique strings

Hallo Everyone. I have to admit I'm shell scripting illiterate . I need to find certain strings in several text files and replace each of the string by unique & corresponding text. I prepared a csv file with 3 columns: <filename>;<old_pattern>;<new_pattern> ... (5 Replies)
Discussion started by: gordom
5 Replies

10. Shell Programming and Scripting

Delete characters & find unique IP addresses with port

Hi, I have a file having following content. <sip:9376507346@97.208.31.7:51088 <sip:9907472291@97.208.31.7:51208 <sip:8103742422@97.208.31.7:51024 <sip:9579892841@97.208.31.7:51080 <sip:9370904222@97.208.31.7:51104 <sip:9327665215@97.208.31.7:51104 <sip:9098364262@97.208.31.7:51024... (2 Replies)
Discussion started by: SunilB2011
2 Replies
URI::Find::Delimited(3pm)				User Contributed Perl Documentation				 URI::Find::Delimited(3pm)

NAME
URI::Find::Delimited - Find URIs which may be wrapped in enclosing delimiters. DESCRIPTION
Works like URI::Find, but is prepared for URIs in your text to be wrapped in a pair of delimiters and optionally have a title. This will be useful for processing text that already has some minimal markup in it, like bulletin board posts or wiki text. SYNOPSIS
my $finder = URI::Find::Delimited->new; my $text = "This is a [http://the.earth.li/ titled link]."; $finder->find($text); print $text; METHODS
new my $finder = URI::Find::Delimited->new( callback => &callback, delimiter_re => [ '[', ']' ], ignore_quoted => 1 # defaults to 0 ); All arguments are optional; defaults are provided (see below). Creates a new URI::Find::Delimited object. This object works similarly to a URI::Find object, but as well as just looking for URIs it is also aware of the concept of a wrapped, titled URI. These look something like [http://foo.com/ the foo website] where: * "[" is the opening delimiter * "]" is the closing delimiter * "http://foo.com/" is the URI * "the foo website" is the title * the URI and title are separated by spaces and/or tabs The URI::Find::Delimited object will extract each of these parts separately and pass them to your callback. callback "callback" is a function which is called on each URI found. It is passed five arguments: the opening delimiter (if found), the closing delimiter (if found), the URI, the title (if found), and any whitespace found between the URI and title. The return value of the callback will replace the original URI in the text. If you do not supply your own callback, the object will create a default one which will put your URIs in 'a href' tags using the URI for the target and the title for the link text. If no title is provided for a URI then the URI itself will be used as the title. If the delimiters aren't balanced (eg if the opening one is present but no closing one is found) then the URI is treated as not being wrapped. Note: the default callback will not remove the delimiters from the text. It should be simple enough to write your own callback to remove them, based on the one in the source, if that's what you want. In fact there's an example in this distribution, in "t/delimited.t". delimiter_re The "delimiter_re" parameter is optional. If you do supply it then it should be a ref to an array containing two regexes. It defaults to using single square brackets as the delimiters. Don't use capturing groupings "( )" in your delimiters or things will break. Use non-capturing "(?: )" instead. ignore_quoted If the "ignore_quoted" parameter is supplied and set to a true value, then any URIs immediately preceded with a double-quote char- acter will not be matched, ie your callback will not be executed for them and they'll be treated just as normal text. This is kinda lame but it's in here because I need to be able to ignore things like <img src="http://foo.com/bar.gif"> A better implementation may happen at some point. SEE ALSO
URI::Find. AUTHOR
Kake Pugh (kake@earth.li). COPYRIGHT
Copyright (C) 2003 Kake Pugh. All Rights Reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. CREDITS
Tim Bagot helped me stop faffing over the name, by pointing out that RFC 2396 Appendix E uses "delimited". Dave Hinton helped me fix the regex to make it work for delimited URIs with no title. Nick Cleaton helped me make "ignore_quoted" work. Some of the code was taken from URI::Find. perl v5.8.8 2008-03-01 URI::Find::Delimited(3pm)
All times are GMT -4. The time now is 08:47 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy