LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-18-2017, 10:25 AM   #1
Reprovo
Member
 
Registered: Jul 2013
Posts: 51

Rep: Reputation: Disabled
multi-line comments in sed


Hi. I found the below example regarding pre-processing a multi-line variable in SED by using '/' forward slashes.

Code:
VAR='content with
multiple lines
  some with lead blanks
or even backslash\'

preprocessed_VAR=$(printf '%s\n' "$VAR" |

sed 's/\\/&&/g;s/^[[:blank:]]/\\&/;s/$/\\/')


sed -i -e "/i/a ${preprocessed_VAR%?}" test_append

What I don't understand is why the "%?" is removing all the added forward slashes in the below command:

Code:
  sed -i -e "/i/a ${preprocessed_VAR%?}" test_append
giving the below output in the test_append file:

content with
multiple lines
some with lead blanks
or even backslash\

If I just run:

Code:
 ${echo ${preprocessed_VAR%?}
i get the following:

content with\ multiple lines\ \ some with lead blanks\ or even backslash\\

It's removing only the last '\' as expected.

What's the difference in the SED command that removes all the added '\' ?
 
Old 12-18-2017, 12:34 PM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

Rep: Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724
Because sed interprets escaped characters like echo -e
 
Old 12-18-2017, 12:56 PM   #3
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,500

Rep: Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314
${preprocessed_VAR%?} is an expression in bash, not related to sed at all, see man bash, look for ${parameter%word}
If you want to check the content of a variable, use ":
Code:
user@host:/tmp$ VAR='content with
> multiple lines
>   some with lead blanks
> or even backslash\'
user@host:/tmp$ echo "$VAR"
content with
multiple lines
  some with lead blanks
or even backslash\
user@host:/tmp$ preprocessed_VAR=$(printf '%s\n' "$VAR" | sed 's/\\/&&/g;s/^[[:blank:]]/\\&/;s/$/\\/')
pan@suni:/tmp/film$ echo "${preprocessed_VAR}"
content with\
multiple lines\
\  some with lead blanks\
or even backslash\\\
user@host:/tmp$ echo "${preprocessed_VAR%?}"
content with\
multiple lines\
\  some with lead blanks\
or even backslash\\
host@user:/tmp$
your sed command will append the content of that variable in every line where at least one i was found.
When you execute the sed the \ inside the sed expression will be evaluated before executing the sed command itself.
 
  


Reply

Tags
expansion, parameter, sed, variables


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] grep/sed/awk multi line array earthgecko Programming 13 10-08-2012 02:06 PM
[SOLVED] How to escape single quotes in sed for multi line pattern XXLRay Linux - Software 1 07-18-2011 08:35 AM
help - how do i run a multi-line sed command in perl? rma88 Programming 2 10-09-2009 08:11 PM
Multi-line find/replace with GnuWin32 Sed Question EdrickV Programming 4 04-26-2009 02:32 PM
sed multi-line search/replace woes djmm Programming 8 03-17-2009 06:25 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration