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 ...
- 03-30-2010 #1Just 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...
- 03-30-2010 #2
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 " 456New 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
- 03-30-2010 #3
probably you just did this guys homework, but who knows
- 03-30-2010 #4Just Joined!
- Join Date
- Mar 2010
- Posts
- 4
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...
- 03-31-2010 #5
sed wants its expression quoted, as meton_magic showed. Look closely.
- 03-31-2010 #6Linux User
- Join Date
- Jan 2005
- Location
- Saint Paul, MN
- Posts
- 262
- 03-31-2010 #7
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.
- 03-31-2010 #8
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
- 03-31-2010 #9
- 04-01-2010 #10
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


Reply With Quote
