• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5348
  • Last Modified:

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.
0
danmb
Asked:
danmb
  • 5
  • 3
  • 2
  • +1
1 Solution
 
TintinCommented:
sed '
s/hi/bye/g
s/go/come/g
..
' file.txt

0
 
danmbAuthor Commented:
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 ?
0
 
TintinCommented:
Just change single quotes to double quotes :-)

sed "
s/hi/bye/g
s/go/come/g
s/$X123/$XABC/
" file.txt
0
Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

 
danmbAuthor Commented:
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?
0
 
ahoffmannCommented:
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.
0
 
danmbAuthor Commented:
You are certainly entitled to your opinion, but your reply does not answer my question.
0
 
ahoffmannCommented:
sed "
s/hi/bye/g
s/go/come/g
s/$X123/$XABC/
s/doublequotefollows"'"'"/doublequoteremoved/
" file.txt

if your variables contain double quotes, then you have to escape them like in the example above
0
 
danmbAuthor Commented:
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.
0
 
danmbAuthor Commented:
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?

0
 
ahoffmannCommented:
> 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:-)
0
 
CetusMODCommented:
Closed, 250 points refunded.
CetusMOD
Community Support Moderator
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

  • 5
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now