How to use sed on a literal string containing regex meta characters?

I want to delete the lines from a file which match an arbitrary literal string.
The string is contained in a variable, and may itself contain various regex meta characters such as [,],^,$,|, etc
So, my problem is that sed wants to interpret these meta characters itself. For example:
x='abc[xyz$'
sed -i "/$x/d" file
produces a sed error about the unmatched [, and no doubt it wants to interpret the $ too.
(Of course, I could try and change the string $x so that every possible regex meta character was escaped with a \, but that seems immensely cumbersome. Also, can't see a way of using single or double quotes cleverly.)
Sorry if I am being thick about this, but any ideas or alternative methods would be most welcome!

frostschutz wrote:
With sed, you have to escape meta characters indivudually. That's just how it works. Of course you could do the escaping using sed as well, by replacing all meta characters (including \ and /) with \character.
If you want to match strings literally, use a different tool. Or do it in Bash.
while read line
do
[ "$line" != "$x" ] && echo "$line"
done < file
In Perl/PCRE instead of escaping indivudually there is \Q...\E but sed does not understand that, and \Q\E does not solve all problems either, if there is a \E in the $x you have to escape it with \E\\E\Q or something like it.
Thank you for your swift reply. Looks like sed can't be much of a friend in these circumstances!
Probably will have to go with the Bash file handling method.
Oh well ...

Similar Messages

  • How to use replace(CharacterSequence, CharacterSequence) in String class

    Need simple java program using the following
    how to use replace(CharacterSequence, CharacterSequence) in String class
    Kindly help

    From http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#replace(java.lang.CharSequence,%20java.lang.CharSequence)
    replace
    public String replace(CharSequence target,
    CharSequence replacement)Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence. The replacement proceeds from the beginning of the string to the end, for example, replacing "aa" with "b" in the string "aaa" will result in "ba" rather than "ab".
    Parameters:
    target - The sequence of char values to be replaced
    replacement - The replacement sequence of char values
    Returns:
    The resulting string
    Throws:
    NullPointerException - if target or replacement is null.
    Since:
    1.5

  • How to use execute immediate for character string value 300

    how to use execute immediate for character string value > 300 , the parameter for this would be passed.
    Case 1: When length = 300
    declare
    str1 varchar2(20) := 'select * from dual';
    str2 varchar2(20) := ' union ';
    result varchar2(300);
    begin
    result := str1 || str2 ||
    str1 || str2 ||
    str1 || str2 ||
    str1 || str2 ||
    str1 || str2 ||
    str1 || str2 ||
    str1 || str2 ||
    str1 || str2 ||
    str1 || str2 ||
    str1 || str2 ||
    str1 || ' ';
    dbms_output.put_line('length : '|| length(result));
    execute immediate result;
    end;
    /SQL> 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
    length : 300
    PL/SQL procedure successfully completed.
    Case 2: When length > 300 ( 301)
    SQL> set serveroutput on size 2000;
    declare
    str1 varchar2(20) := 'select * from dual';
    str2 varchar2(20) := ' union ';
    result varchar2(300);
    begin
    result := str1 || str2 ||
    str1 || str2 ||
    str1 || str2 ||
    str1 || str2 ||
    str1 || str2 ||
    str1 || str2 ||
    str1 || str2 ||
    str1 || str2 ||
    str1 || str2 ||
    str1 || str2 ||
    str1 || ' ';
    dbms_output.put_line('length : '|| length(result));
    execute immediate result;
    end;
    /SQL> 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
    declare
    ERROR at line 1:
    ORA-06502: PL/SQL: numeric or value error: character string buffer too small
    ORA-06512: at line 6

    result varchar2(300);Answer shouldn't be here ? In the greater variable ?
    Nicolas.

  • How to use regular expression to find string

    hi,
    who know how to get all digits from the string "Alerts 4520 ( 227550 )  (  98 Available  )" by regular expression, thanks
    br, Andrew

    Liu,
    You can use RegEx as   
    d+
    Whether you are using CL_ABAP_REGEX class then
    report  zars.
    data: regex   type ref to cl_abap_regex,
          matcher type ref to cl_abap_matcher,
          match   type c length 1.
    create object regex exporting pattern = 'd+'
                                  ignore_case = ''.
    matcher = regex->create_matcher( text = 'Test123tes456' ).
    match = matcher->match( ).
    write match
    You can find more details regarding REGEX and POSIX examples here
    http://www.regular-expressions.info/tutorial.html

  • How to use a var as a string

    hello, sorry for my question,
    but I want to use a var as a string:
    I know it is a completely beginners-question, but I didn´t need it until now:
    var zahl = Math.floor((Math.random()*99)+1);
    sym.$("frame")
              .css(
                                  {'background-image':'url("images/zahl.jpg")'
    I tried it with {zahl} and [zahl] but it does not work.
    Help would be great
    Oliver

    Thanks a lot,
    but sorry, I have no sucsess:
    var zahl = Math.floor((Math.random()*99)+1);
    sym.$("frame")
    .css(
                                                                {'background-image':'url("images/" + zahl + ".jpg")'
    here is the whole code again so that you can see if I made a fault.
    THX

  • Encode/Decode a String containing Chinese etc. characters to/from unicode

    Hi, I am working to encode a multilingual String (English, chinese, japanese, Latin etc) as unicode. The encoded string is used as follows
    1)decoded by the User Interface for display purpose. The user interface is a web UI and
    2)read by user. So it should be human readable when it contains only English and special characters.
    Thus having a string that contains letters from English, Japanese, CHinese etc, we want the Japanese/Chinese characters to be encoded by the hex values such as &#31169; be encoded as %E7%A7%81.
    However, it is preferable that other special characters like ! ? , space etc not be encoded and left as such.
    The encoding of characters is achievable by using Java.net.URLEncoder but it also replaces all special characters including space character, which becomes a pain for the reader.
    Unfortunately URLEncoder.java does not have any API to configure which characters to encode. Any suggestions how I can proceed, or what encoder i can use?
    Thanks in advance!
    Regards

    I have to say that I disagree that "%E7%A7%81" is "human-readable". On the contrary, I would say that "&#31169;" is human-readable; even though I don't understand it myself, there are a lot of humans who do.
    But let's say that "human-readable" wasn't the right term. Maybe you just wanted a representation of an arbitrary Unicode string in ASCII characters only. In which case I would recommend Base-64 encoding. It does make everything unreadable, though, whereas your requirements appear to be to only make languages other than English be unreadable. So if you don't like that you are going to have to write your own encoder.
    Note also that your original premise:
    Hi, I am working to encode a multilingual String (English, chinese, japanese, Latin etc) as unicode.is misguided, since all data in Java Strings is already Unicode characters.

  • How to use ora:parseEscapedXML() Function with String Content

    Hello,
    I am receiving a ResponseMessage that returns a string, but the content of the string is actually XML. I have tried to use the ora:parseEscapedXML() function to parses the string (see XML Fragment below) to return structured XML data that can be assigned to typed BPEL variables. The documentation is very limited for this (I've looked in the Developer Guide draft that is available at the OraBPEL site).
    2 customer records from XML literal fragment copied from string result:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <recordset>
    <customer>
    <CustNum>AROUT</CustNum>
    <CustLName>Hardy</CustLName>
    <CustFname>Thomas</CustFname>
    <CustEmail>[email protected]</CustEmail>
    <CustAddress>120 Hanover Sq.</CustAddress>
    <CustCity>London</CustCity>
    <CustRegion></CustRegion>
    <CustPostalCode>WA1 1DP</CustPostalCode>
    <CustCountry>UK</CustCountry>
    </customer>
    <customer>
    <CustNum>BSBEV</CustNum>
    <CustLName>Ashworth</CustLName>
    <CustFname>Victoria</CustFname>
    <CustEmail>Victoria.Ashworth@B&apos;s.com</CustEmail>
    <CustAddress>Fauntleroy Circus</CustAddress>
    <CustCity>London</CustCity>
    <CustRegion></CustRegion>
    <CustPostalCode>EC2 5NT</CustPostalCode>
    <CustCountry>UK</CustCountry>
    </customer>
    The copy rule for assigning CustNum to accountnumber that fails:
    <copy>
    <from expression="ora:parseEscapedXML(bpws:getVariableData('DataService_OutputVariable','invokeSpecMsgReturn','/recordset/customer/CustNum'))"/>
    <to variable="outputVariable" part="payload" query="/client:QueryResult/client:customer/client:accountNumber"/>
    </copy>
    Any help would be greatly appreciated.
    Thanks,
    Sean

    Hello,
    I am attempting to provide more information in order to hopefully get some better guidance here.
    1) The BPEL process I built receives input from a client that is one of city name or region name or postal code or country name and returns a list of customer records with their AccountNumber, LastName, FirstName, Email, Address, City, Region, PostalCode and Country. The BPEL flow invokes a packaged integration Web Service that retuns the records in a string whose content is XML (see XML fragment below).
    2) The string content returned is vaiable based on the input (i.e. city name="London")
    Two customer records from XML literal fragment copied from string result:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <recordset>
    <customer>
    <CustNum>AROUT</CustNum>
    <CustLName>Hardy</CustLName>
    <CustFname>Thomas</CustFname>
    <CustEmail>[email protected]</CustEmail>
    <CustAddress>120 Hanover Sq.</CustAddress>
    <CustCity>London</CustCity>
    <CustRegion></CustRegion>
    <CustPostalCode>WA1 1DP</CustPostalCode>
    <CustCountry>UK</CustCountry>
    </customer>
    <customer>
    <CustNum>BSBEV</CustNum>
    <CustLName>Ashworth</CustLName>
    <CustFname>Victoria</CustFname>
    <CustEmail>Victoria.Ashworth@B&apos;s.com</CustEmail>
    <CustAddress>Fauntleroy Circus</CustAddress>
    <CustCity>London</CustCity>
    <CustRegion></CustRegion>
    <CustPostalCode>EC2 5NT</CustPostalCode>
    <CustCountry>UK</CustCountry>
    </customer>
    I want to <assign> each record from the packaged integration service result (the string whose content is XML) to a complexType variable defined within the BPEL Process WSDL (see example below).
    XML fragment from BPEL WSDL:
    <element name="QueryResult" type="client:recordType"/>>
    <complexType name="recordType">
    <sequence>
    <element name="customer" type="client:customerType" maxOccurs="unbounded"/>
    </sequence>
    </complexType>
    <complexType name="customerType">
    <sequence>
    <element name="accountNumber" type="string"/>
    <element name="lastName" type="string"/>      
    <element name="firstName" type="string"/>
    <element name="email" type="string"/>
    <element name="address" type="string"/>
    <element name="city" type="string"/>
    <element name="region" type="string"/>
    <element name="postalcode" type="string"/>
    <element name="country" type="string"/>
    </sequence>                         
    </complexType>
    I have read through the tutorial and reviwed the XPath Funtion example in the reference (C:\OraBPELPM_1\integration\bpelpm\orabpel\samples\references\XPathFunction), but I am stsill very unclear as to how to handle this. I would appreciate any and all help.
    Thanks,
    Sean

  • How to use a WebService Model with String Arrays as its input  parameter

    Hi All,
    I am facing the following scenario -
    From WebDynpro Java i need to pass 2 String Arrays to a Webservice.
    The Webservice takes in 2 input String Arrays , say, Str_Arr1[] and Str_Arr2[] and returns a string output , say Str_Return.
    I am consuming this WebService in WebDynpro Java using WEB SERVICE MODEL. (NWDS 04).
    Can you please help out in how to consume a WSModel in WD where the WS requires String Arrays?

    Hi Hajra..
    chec this link...
    http://mail-archives.apache.org/mod_mbox/beehive-commits/200507.mbox/<[email protected]>
    http://www.theserverside.com/tt/articles/article.tss?l=MonsonHaefel-Column2
    Urs GS

  • How to use DB toolkit to input string 255 into ACCESS Database

    I am converting an existing LV-Application from LV5.1.1 w/SQL toolkit to LV7.1 with DB-Conn. Toolkit.  One of the tables in my MS-Access database has a field of TYPE=memo (allows string lengths > 255 characters).  I can't seem to find how to insert this field in the DB-table using the "DB Tools Insert Data.VI" without getting an error.  I have tried converting the input string to a [U8] array, but that also doesn't work.  Just inputing the long string (390-chrs) also gives an error.  Reducing the string length to < 255 allows the new record to be input into the table without errors.  However, I need to be able to input the longer strings for the application to work.
    Does anyone have a solution to this problem.
    Thanks,
    Jim Cardinal

    Something like this
    David
    Message Edited by David Crawford on 01-12-2006 11:53 AM
    Attachments:
    Insert Text to Memo Using DBT.jpg ‏14 KB

  • How to use Sockets to send/recieve Strings in XML form?

    Hello there to everyone reading my Post !
    I am entering a total new subject in my learning about Java Programming, where I will need to interact with a server, I was wondering If somebody could help me with a Question I have; here it is:
    A server provided me a Socket, (and that is of course an IP address and a Port), And I am supposed to connect to that server using a Java program that connects to servers using a Socket class, then I am supposed to send a String representing a valid XML, and then the server replies me With another String representing a valid XML which I am supposed to store in a String type.
    Is this exact write/read operation possible? If so, could someone please give me some code samples to solve this problem?
    PD: The server Does this operation only: recieve/send Strings. as its called transactional switch.

    Hi ejp, thank you very much for the quickest of replies.
    I am wondering If could you please give me a more detailed source code sample, and the reasons are:
    Well, first I am a newbie. and secondly, and most importantly, I actually can try to send the String that represents the XML, but then...
    I don't have any idea about how to know when my program has finished sending the String. and therefore I wouldn't know where to put the lines of code that start reading the corresponding response String.
    Could you please help me out?
    PD: I've been using DOM to read XMLs. Does DOM Objects also allow to read/write XMLs using Sockets?
    Thanks in advance!

  • How to use certain parts of a string-parsing???

    I have a form which pulls some data from a db. For example, it pulls the following about what office I work in:
    DS/ISC/GCS/ABC/DEFG
    The DS refers to the larger office, ISC referes to the next office down, and so forth. I CANNOT CHANGE THESE VALUES.
    What I'd like to do is 'disect' this long string based on where the "/" lie in the string.
    I'm basically only interested in the first two offices. In this example DS and ISC. So, when I want to display the parent office, the only string to display is DS and not DS/. Then on another line, I want to display the sub office ISC and not ISC/ . I guess I could parse? But I'm not sure exactly how to do this. If there is another 'easy' way to do this, I'd open to any suggestions.
    TIA

    http://forum.java.sun.com/thread.jspa?threadID=682574&tstart=0

  • How to use textfiled to display one string that some characters are embed font support some not

    hi everyone
    i try to use embed font on a textfield,however,the embed font can only support limit character glyphs.
    so i was think is it possible that if the font include character,i use that font,if not include,i use default system font
    like the text function on http://picmonkey.com
    if i want to use embed font to textfield,i have to set textFiled.embedFonts=true
    however,if i did that,the characters not included in font can't not be displayed,even i change textformat.font="_sans"
    so how to solve this problem?
    what did picmonkey do?

    use textFiled.embedFonts=false

  • How to use create-nodeset-from-delimited-string()

    Hi All,
    I am getting the input data as following
    name rollno sub
    Anu 1 Maths|science|social
    and the output should be as follows:
    name rollno sub
    Anu 1 Maths
    Anu 1 Science
    Anu 1 Social
    I need to implememt this by using oraext:create-nodeset-from-delimited-string() in XSLT Pls help me out in resolving this issue
    Regards,
    Anasuya

    can u elaborate the query with ur input xml and output xml for clear understanding instead of
    name rollno sub
    Anu 1 Maths|science|social
    and the output should be as follows:
    name rollno sub
    Anu 1 Maths
    Anu 1 Science
    Anu 1 Social
    check the following links it might help u
    http://www.soabyte.com/2011/01/delimited-string-to-xml-nodeset.html
    Edited by: olety on Nov 2, 2011 2:29 AM

  • How to use Browser Control for displaying String Results..

    Hi All,
    I m new to java. I am developing desktop application for java getting problem at some stage , i hav some string Results which i got some how by searching in some text files. Now i want to show / format this string in Browser control.
    Please help me in this...
    Thanks in Advance
    Nicky

    i m sonu could u piz tell me how to read integer from keyboard

  • How to use forward slash in a string

    ok, this is probably very simple, but i have a url that i need to pass into my app that looks like:
    public void resize(URL url, String id)
    }I'm trying to pass it a simple address of an image stored somewhere on the net but i am just faced with this instead:
    class java.lang.NoSuchMethodError
    resizeImage.resizeImage.resize(Ljava/lang/String;Ljava/lang/String;)V
    Now i know that this is because there is something wrong with the arguments being passed into the method, which i'm doing like this:
    imgManip.resize("http://www.screwfix.com/sfd/i/cat/59/p3248959_x.jpg","99"); what am i doing wrong?

    Is a String a URL? Answer: No.
    Create a URL from that string.

Maybe you are looking for

  • Problems with hp officejet pro 8610 printing labels

    I have a new HP offficejet Pro 8610 less than 6 months old. I was printing address lables (Avery 8160) and everything seemed fine after the first group then when I started the second set they were out of alignment I readjusted the paper and tried aga

  • System preferences energy saver isn't working

    I've noticed that after installing 10.6.6 that my energy saver preferences are sporadic. I have it set to reduce brightness after 10 minutes and after an hour it still bright. I've dumped com.apple.systempreferences.plist but the problem still exists

  • MSS report disappeared

    Hi all, We installed MSS business package on portal. Under the "reports", we click on the link of the report name then select the criteria in launchpad, after submit, nothing shows up. The browser window just disappeared. Anyone has any idea about th

  • Can I get a native dump (Dr. Watson) when JNI code causes an exception?

    Is there a way to get the JVM to just not trap an exception, so that we can get a native dump? I have tried -XX:OnError="drwtsn32 -p %p". This did produce a Dr. Watson dump, but not a good one. I placed a deliberate error in the code, and called it o

  • HT4623 How can I install iOS 6 on my ipad 2

    Help me on how to install iOS 6. Due to that in the setting it doesn't have icon to update