LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Strange sed results (https://www.linuxquestions.org/questions/programming-9/strange-sed-results-894096/)

merana 07-27-2011 10:25 AM

Strange sed results
 
Hi Everyone.

Long time since I posted... I have an odd issue that I am running into. My objective is to insert the following lines at the end of /etc/inputrc on an RHES box:

Code:

# alternate mappings for "page up" and "page down" to search the history
"\e[5~": history-search-backward
"\e[6~": history-search-forward

I would like this sed snippet to be portable and thus far it's worked. However, I ran into one box where after I ran the snippet, Subsequent sessions lost the ability to use the letter "e".

The original version I was using looked like this:

Code:

mv /etc/inputrc /tmp/inputrc.orig
sed 's/'\$endif'/\
# alternate mappings for "page up" and "page down" to search the history \
"\e[5~": history-search-backward \
"\e[6~": history-search-forward \
&/' /tmp/inputrc.orig > /etc/inputrc

Then instead of using s/ I thought perhaps i/ would be better so I re-did it as:

Code:

mv /etc/inputrc /tmp/inputrc.orig
sed '/'\$endif'/ {
i\

i\
# alternate mappings for "page up" and "page down" to search the history
i\
"\e[5~": history-search-backward
i\
"\e[6~": history-search-forward
}' /tmp/inputrc.orig > /etc/inputrc

Both of these produce the expected result BUT they both resulted in the loss of the letter "e".

As I mentioned earlier in the post, this was working on 'newer' RHES 5 and up but I have the misfortune of having to support some older RHES 4 boxes in a closed lab. This particular box is an RHES 4u6.

Anyone got any suggestion on how I find out what's happening? Also if some wiser sed heads can give me a more elegant version it would be appreciated.

Thanks in Advance!

Michael E

MTK358 07-27-2011 11:19 AM

How about this:

Code:

echo '# alternate mappings for "page up" and "page down" to search the history
"\e[5~": history-search-backward
"\e[6~": history-search-forward' >> /etc/inputrc


merana 07-27-2011 11:57 AM

Quote:

Originally Posted by MTK358 (Post 4426795)
How about this:

Code:

echo '# alternate mappings for "page up" and "page down" to search the history
"\e[5~": history-search-backward
"\e[6~": history-search-forward' >> /etc/inputrc


Nice try but then you lose the terminating $endif.

That has to be at the end so it's not just concatenation that has to happen but more of an "insert-between".

Thanks for your suggestion though!

crts 07-27-2011 01:43 PM

Hi,

the issue that I encountered with your 'sed' was that it swallowed the backslashes. This one worked for me
Code:

sed -i.orig '/$endif/ {i\
\
# alternate mappings for "page up" and "page down" to search the history\
"\\e[5~": history-search-backward\
"\\e[6~": history-search-forward
}' /etc/inputrc

I also removed the redundant '\i'. The above works with
Code:

$ sed --version
GNU sed version 4.2.1

[EDIT]
The -i.orig option will create a backup file before 'sed' makes any changes to the original file. This makes the preceding 'mv' redundant. If something goes wrong then you can restore the original file that has been backupped to /etc/inputrc.orig.

merana 08-02-2011 11:43 AM

Thanks crts!

That worked perfectly on the hosts I've tried thus far.

The -i.orig is an elegant method rather than my crude mv.

Kudos and Regards,
Michael E


All times are GMT -5. The time now is 09:54 AM.