Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 11
I want to use SED to do the following: In a text file replace any occurrences of the three character string ZZZ with a quotation mark " and... replace all ...
  1. #1
    Just Joined!
    Join Date
    Mar 2010
    Posts
    4

    SED Regular Expression

    I want to use SED to do the following:

    In a text file replace any occurrences of the three character string ZZZ with a quotation mark " and...
    replace all occurrences of a comma with a semi-colon.

    It is the S/ / / command which is stumping me on the first issue...inparticular how to get the replace string to be quote.

    Any help would be appreciated...

  2. #2
    Linux Enthusiast meton_magis's Avatar
    Join Date
    Oct 2006
    Location
    arizona
    Posts
    665
    you need to escape quotes when used within a shell. to replace in a file

    123 ZZZ 456

    with
    123 " 456

    use
    sed 's/ZZZ/\"/g' filename

    and it will print to stdout
    123 " 456
    New to the internet, technical forums, or the hacker / open source community??
    Read this to learn good posting habits http://www.catb.org/~esr/faqs/smart-questions.html

    RHCE for RHEL version 5
    RHCT for RHEL version 4

  3. #3
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    probably you just did this guys homework, but who knows

  4. #4
    Just Joined!
    Join Date
    Mar 2010
    Posts
    4

    Smile Cheers..

    Unfortunately not homework... I am using SED (the GNUWIN32 version) because a 'wonderful product' called Microsoft Excel 2007 will not output a CSV file with the text fields surrounded by quotation marks (as one would expect), and I do not want to invest my time in Visual Basic. If only everything were this simple in Windows.

    I tried the escape mechanism but the Win version got confused when I added >outfilename at the end of the line..

    sed s/ZZZ/\"/g Infile >outfile

    returns Invalid Argument in the GNUWIN version and this threw me...

    Cheers Guys...

  5. #5
    Linux Enthusiast Mudgen's Avatar
    Join Date
    Feb 2007
    Location
    Virginia
    Posts
    623
    sed wants its expression quoted, as meton_magic showed. Look closely.

  6. #6
    Linux User
    Join Date
    Jan 2005
    Location
    Saint Paul, MN
    Posts
    262
    Quote Originally Posted by greyhairweenie View Post
    sed wants its expression quoted, as meton_magic showed. Look closely.
    The reason that the pattern is quoted is so the shell will not match it files when it is expanding the command before it is actually executed.

  7. #7
    Linux Enthusiast Mudgen's Avatar
    Join Date
    Feb 2007
    Location
    Virginia
    Posts
    623
    Quote Originally Posted by alf55 View Post
    The reason that the pattern is quoted is so the shell will not match it files when it is expanding the command before it is actually executed.
    I guess you mean so the shell won't expand globbing metacharacters into filenames. The quoting is to avoid confusion of metacharacters in general. I always quote sed expressions, out of habit. Many consider it a "best practice".

    Looking back at the thread, I may have been wrong about quoting being the problem here, however. It may be some idiosyncrasy of the Win version of sed that's being used.

  8. #8
    Linux Enthusiast meton_magis's Avatar
    Join Date
    Oct 2006
    Location
    arizona
    Posts
    665
    Quote Originally Posted by greyhairweenie View Post

    Looking back at the thread, I may have been wrong about quoting being the problem here, however. It may be ... Win .. that's being used.
    fixed.

    I'm not an anti windows zealot (even though I personally hate it, and used to be very outspoken) but trying to get GNU utilities to work on windows in an expected manner is ALWAYS going to be a pain in the rear.

    sed and awk are tied in very close to bourne compatible shells. It is expected that you will use them there. You may be able to use some functions on windows, but using them from within the windows shell is going to be more trouble than it is worth.

    are you using cygwin, or cmd.exe, or powershell?

    powershell is a MUCH better scripting environment than cmd.exe, and you may have better lock running your sed from within that shell. Cygwin is of course going to be the easiest to get sed running in, but most likely the hardest to get setup.

    cmd.exe is not even worth running
    New to the internet, technical forums, or the hacker / open source community??
    Read this to learn good posting habits http://www.catb.org/~esr/faqs/smart-questions.html

    RHCE for RHEL version 5
    RHCT for RHEL version 4

  9. #9
    Linux Engineer hazel's Avatar
    Join Date
    May 2004
    Location
    Harrow, UK
    Posts
    955
    Quote Originally Posted by Innoko View Post
    I am using SED (the GNUWIN32 version) because a 'wonderful product' called Microsoft Excel 2007 will not output a CSV file with the text fields surrounded by quotation marks (as one would expect), and I do not want to invest my time in Visual Basic. If only everything were this simple in Windows.
    So why use Windows at all if it's such a pain in the a***?
    "I'm just a little old lady; don't try to dazzle me with jargon!"

  10. #10
    Linux Enthusiast meton_magis's Avatar
    Join Date
    Oct 2006
    Location
    arizona
    Posts
    665
    A lot of people are forced to use windows at work. I know I have to
    New to the internet, technical forums, or the hacker / open source community??
    Read this to learn good posting habits http://www.catb.org/~esr/faqs/smart-questions.html

    RHCE for RHEL version 5
    RHCT for RHEL version 4

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...