LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 09-10-2017, 01:02 PM   #1
say_hi_ravi
Member
 
Registered: Jan 2008
Posts: 75

Rep: Reputation: 15
sed: \ in variable dissappears in append feature


I want to append below find command in a file under a line matching a string.

command:
Quote:
find /path/to/dir -type f \( -name '*.gz' -o -name '*.log' \) -mtime + ${days} -exec rm -fr {} \;
File:
Quote:
$ cat disk.sh
for id abc
I am taking a command in variable and then using sed to add command via variable under a line below match.

Quote:
$ COMM=`echo "find /path/to/dir -type f \( -name '*.gz' -o -name '*.log' \) -mtime + ${days} -exec rm -fr {} \;"`

$ sed -i "/for id abc/a ${COMM}" disk.sh
However in output \ dissappears.

Quote:
$ cat disk.sh
for id abc
find /path/to/dir -type f ( -name '*.gz' -o -name '*.log' ) -mtime + ${days} -exec rm -fr {} ;
But variable seem to have captured the whole command perfectly.

Quote:
$ echo ${COMM}
find /path/to/dir -type f \( -name '*.gz' -o -name '*.log' \) -mtime + ${days} -exec rm -fr {} \;
 
Old 09-10-2017, 01:44 PM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,740

Rep: Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198
Yes, the sed command will interpret the backslash itself, and the sequences "\(" and "\)" are just "(" and ")" respectively. If you want the sed command to treat the backslashes literally, you would have to escape each of the with another backslash. But, then the shell will see the sequence "\\" within double quotes, where the backslash keeps its special meaning, and will just replace those two characters with a single backslash. The way you are using single and double quotes, the shell will be also doing that when setting the COMM variable. You can prevent that by reversing your usage of single and double quotes (and getting rid of that useless use of "echo" is a good idea too).
Code:
COMM='find /path/to/dir -type f \\( -name "*.gz" -o -name "*.log" \\) -mtime + ${days} -exec rm -fr {} \\;'
sed -i "/for id abc/a ${COMM}" disk.sh
(I really with this darned forum software would stop occasionally eating backslashes.)
 
Old 09-10-2017, 03:15 PM   #3
say_hi_ravi
Member
 
Registered: Jan 2008
Posts: 75

Original Poster
Rep: Reputation: 15
Thanks for reply.

Tried to escape using one more \ and got rid of echo too. ${days} gets printed literally instead of its value. Escaping the curly brackets and $ didnt work too.

Also Assigned the command to double quoted variable with escaping \ with one more \. Again the original issue of \ missing
 
Old 09-10-2017, 04:07 PM   #4
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,740

Rep: Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198
Sorry, I missed that there was a ${days} expansion in that assignment of the COMM variable. You would need to keep that outside of the single quotes:
Code:
COMM='find /path/to/dir -type f \\( -name "*.gz" -o -name "*.log" \\) -mtime + '${days}' -exec rm -fr {} \\;'
                                                                               ^       ^
                                                             end quoted string |       | start a new quoted string
To stay with the double quotes as you orginally had then, each backslash needs to be quadrupled.
Code:
COMM="find /path/to/dir -type f \\\\( -name '*.gz' -o -name '*.log' \\\\) -mtime + ${days} -exec rm -fr {} \\\\;"
Quoting and backslash escapes in strings that are going to be parsed and re-parsed is messy. It's hard to get around without re-structuring what you are doing.
 
1 members found this post helpful.
Old 09-10-2017, 04:51 PM   #5
say_hi_ravi
Member
 
Registered: Jan 2008
Posts: 75

Original Poster
Rep: Reputation: 15
Bingo!!

Many Thanks, Sir!!
 
  


Reply


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
\ in variable vanishes while in sed append feature say_hi_ravi Programming 2 09-10-2017 03:22 PM
how can i append the text after pattern match using variable in sed ravikushal Linux - Newbie 18 07-23-2013 08:56 AM
[SOLVED] sed append string in variable to last line of file. SilversleevesX Linux - Newbie 7 11-27-2011 11:13 PM
using sed to append a variable after a certain line jadeddog Programming 6 11-05-2008 12:49 AM
sed script to append variable text gmartin Linux - General 4 12-27-2006 04:44 PM

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

All times are GMT -5. The time now is 11:52 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
Open Source Consulting | Domain Registration