danmb
asked on
Short multi-line SED script via HEREDOC in Bash
Hi,
Red Hat Linux, in Bash
I'm looking for a way for a Bash script to run a short multi-line SED script originating from a HEREDOC following the SED command line.
Is there a way to redirect the -e or -f parameters of SED to accept its input from a HEREDOC following the SED command line?
Here's a simplified example of what I want to do, which does not work:
#!/bin/bash
sed -f<<HEREDOC file.txt
s/hi/bye/g
s/go/come/g
s/$X123/$XABC/
HEREDOC
Thanks.
Red Hat Linux, in Bash
I'm looking for a way for a Bash script to run a short multi-line SED script originating from a HEREDOC following the SED command line.
Is there a way to redirect the -e or -f parameters of SED to accept its input from a HEREDOC following the SED command line?
Here's a simplified example of what I want to do, which does not work:
#!/bin/bash
sed -f<<HEREDOC file.txt
s/hi/bye/g
s/go/come/g
s/$X123/$XABC/
HEREDOC
Thanks.
ASKER
I noticed you didn't include this line from my example:
s/$X123/$XABC/
which uses variable substitution.
Variable substitution doesn't work in single quotes.
Next ?
s/$X123/$XABC/
which uses variable substitution.
Variable substitution doesn't work in single quotes.
Next ?
Just change single quotes to double quotes :-)
sed "
s/hi/bye/g
s/go/come/g
s/$X123/$XABC/
" file.txt
sed "
s/hi/bye/g
s/go/come/g
s/$X123/$XABC/
" file.txt
ASKER
Problem: some of the substitutions I have to do contain double quotes. That will prematurely terminate the SED command line.
I'd really prefer to put the SED commands in a Heredoc, which is what I originally asked for.
Anyone?
I'd really prefer to put the SED commands in a Heredoc, which is what I originally asked for.
Anyone?
get rid of command line and here documents if you have variables and special characters
it's just a academic question/solution which causes more problems than it solves.
it's just a academic question/solution which causes more problems than it solves.
ASKER
You are certainly entitled to your opinion, but your reply does not answer my question.
sed "
s/hi/bye/g
s/go/come/g
s/$X123/$XABC/
s/doublequotefollows"'"'"/ doublequot eremoved/
" file.txt
if your variables contain double quotes, then you have to escape them like in the example above
s/hi/bye/g
s/go/come/g
s/$X123/$XABC/
s/doublequotefollows"'"'"/
" file.txt
if your variables contain double quotes, then you have to escape them like in the example above
ASKER
Again, that's really nice, but my question was how to do this in a heredoc.
About escaping characters in regular expressions: my first exposure to regular expressions was in Perl, which does not require that characters like +, ( and ) be escaped. My personal impression is that regular expressions in bash are an undecipherable mess, what with all the escapes. I don't want to complicate matters further by escaping quotes, because I'm editing lines that are in the format option="parameter". That's why I want to do it in a heredoc.
I'd be happy to award the points to whoever answers the question I asked.
About escaping characters in regular expressions: my first exposure to regular expressions was in Perl, which does not require that characters like +, ( and ) be escaped. My personal impression is that regular expressions in bash are an undecipherable mess, what with all the escapes. I don't want to complicate matters further by escaping quotes, because I'm editing lines that are in the format option="parameter". That's why I want to do it in a heredoc.
I'd be happy to award the points to whoever answers the question I asked.
ASKER
I had a brainstorm, so I'm going to answer my own question.
The way to have SED get its input from a HEREDOC is as follows:
1) use CAT to output the HEREDOC,
2) pipe it to SED (which receives the HEREDOC on STDIN),
3) and use the --file=- switch to tell SED to execute the script file on STDIN ("-").
In other words:
cat <<EOF | sed --file=- infile > outfile
s/hi/bye/g
s/go/come/g
s/$X123/$XABC/
EOF
That should do it. And I don't have to escape quotes, since HEREDOC will happily accept anything until EOF.
My hangup was trying to integrate the HEREDOC into the SED command line. It can't be done. You have to cat the HERDOC and pipe it to SED on stdin. Also, my background goes back to VMS (that dates me) which refers to STDIN as SYS$INPUT. I couldn't for the life of me figure out what the Unix equivalent of SYS$INPUT was, until I saw the "-" parameter. Eureka.
OK, so I answered my own question. Now what? Do I award myself the points?
The way to have SED get its input from a HEREDOC is as follows:
1) use CAT to output the HEREDOC,
2) pipe it to SED (which receives the HEREDOC on STDIN),
3) and use the --file=- switch to tell SED to execute the script file on STDIN ("-").
In other words:
cat <<EOF | sed --file=- infile > outfile
s/hi/bye/g
s/go/come/g
s/$X123/$XABC/
EOF
That should do it. And I don't have to escape quotes, since HEREDOC will happily accept anything until EOF.
My hangup was trying to integrate the HEREDOC into the SED command line. It can't be done. You have to cat the HERDOC and pipe it to SED on stdin. Also, my background goes back to VMS (that dates me) which refers to STDIN as SYS$INPUT. I couldn't for the life of me figure out what the Unix equivalent of SYS$INPUT was, until I saw the "-" parameter. Eureka.
OK, so I answered my own question. Now what? Do I award myself the points?
> OK, so I answered my own question. Now what? Do I award myself the points?
well done ;-)
GranMod, PAQ with refund (you know how to do it:-)
well done ;-)
GranMod, PAQ with refund (you know how to do it:-)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
s/hi/bye/g
s/go/come/g
..
' file.txt