LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 10-09-2018, 02:29 AM   #1
sumitsahay
LQ Newbie
 
Registered: Jun 2018
Posts: 13

Rep: Reputation: Disabled
Post Regular Expression


Hi All,

I struggling to replace special characters from string below is the string example.

"\nhostname \n\r"

I want to remove \n and \r from the string, as of now unable to do so.

Looking forward for reply.

Regards
Sumit Sahay
7044112109
 
Old 10-09-2018, 03:00 AM   #2
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
Have you tried tr with option -d?
 
Old 10-09-2018, 03:46 AM   #3
sumitsahay
LQ Newbie
 
Registered: Jun 2018
Posts: 13

Original Poster
Rep: Reputation: Disabled
Post

Quote:
Originally Posted by l0f4r0 View Post
Have you tried tr with option -d?
Thanks for your reply.
I was trying below one,

sed -i 's/"\nhostname \n\r"/"hostname"/g' known_host.txt

Regards
Sumit Sahay
 
Old 10-09-2018, 04:04 AM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
sed doesn't get to see the newline (\n) - it is stripped before the record is passed to be processed. Hence your regex will never match.
 
Old 10-09-2018, 04:08 AM   #5
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
Quote:
Originally Posted by sumitsahay View Post
I was trying below one,
Code:
sed -i 's/"\nhostname \n\r"/"hostname"/g' known_host.txt
As syg00 said, sed works with lines one by one so "\n" will never match your regex...except if you modify the pattern space (with instruction "N" in your case).
...But I warn you it's easier with tr. Your choice
 
Old 10-09-2018, 04:24 AM   #6
sumitsahay
LQ Newbie
 
Registered: Jun 2018
Posts: 13

Original Poster
Rep: Reputation: Disabled
Post

Quote:
Originally Posted by syg00 View Post
sed doesn't get to see the newline (\n) - it is stripped before the record is passed to be processed. Hence your regex will never match.

Could you please navigate me to achieve the output.

I want to replace \n and \r from the string.
 
Old 10-09-2018, 04:41 AM   #7
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
I've already give you a way to achieve what you want with sed (if I were you I would read some documentation about pattern space modification, for example https://www.gnu.org/software/sed/man...ine-techniques). You need to associate instructions "N" and "s" (+"p" if you used option "-n").
But as stated above, it's much easier with tr applied to the content of your variable...
 
Old 10-09-2018, 07:20 AM   #8
sumitsahay
LQ Newbie
 
Registered: Jun 2018
Posts: 13

Original Poster
Rep: Reputation: Disabled
Post

Quote:
Originally Posted by l0f4r0 View Post
I've already give you a way to achieve what you want with sed (if I were you I would read some documentation about pattern space modification, for example https://www.gnu.org/software/sed/man...ine-techniques). You need to associate instructions "N" and "s" (+"p" if you used option "-n").
But as stated above, it's much easier with tr applied to the content of your variable...
Sorry, not getting your point, I was trying to use tr to replace \n and \r but for me it is not working.
 
Old 10-09-2018, 07:47 AM   #9
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
Quote:
Originally Posted by sumitsahay View Post
Sorry, not getting your point, I was trying to use tr to replace \n and \r but for me it is not working.
Ok, give us the command you tried and its output
 
Old 10-09-2018, 07:56 AM   #10
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
if the \n and \n\r are explicit within the text then try this, you'll have to mod it for your own usage.
Code:
someline="\nsometinghere\n\r"

$ results="$(echo $someline | sed 's|\\nsometinghere\\n\\r|somethinghere|')"

$ echo $results
somethinghere
escaping the escapes. some tr commands and how tos
http://www.softpanorama.org/Tools/tr.shtml

Last edited by BW-userx; 10-09-2018 at 08:00 AM.
 
Old 10-09-2018, 08:03 AM   #11
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
Quote:
Originally Posted by BW-userx View Post
if the \n and \n\r are explicit within the text then try this, you'll have to mod it for your own usage.
Code:
someline="\nsometinghere\n\r"
$ results="$(echo $someline | sed 's|\\nsometinghere\\n\\r|somethinghere|')"
$ echo $results
somethinghere
escaping the escapes. some tr commands and how tos
http://www.softpanorama.org/Tools/tr.shtml
Doesn't work for me
 
Old 10-09-2018, 08:06 AM   #12
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by l0f4r0 View Post
Doesn't work for me
you assigned a variable with exactly \nsomethinghere\n\r then ran that code to match it, and it did not work? humm I find that hard to take in as it should not matter across distros
Code:
someline="\nhereweAre\n\r"
userx@manjaroieo:~
$ echo $someline
\nhereweAre\n\r


userx@manjaroieo:~
$ results="$(echo $someline | sed 's|\\nhereweAre\\n\\r|somethinghere|')"
userx@manjaroieo:~
$ echo $results
somethinghere
check your code.

Last edited by BW-userx; 10-09-2018 at 08:13 AM.
 
Old 10-09-2018, 08:18 AM   #13
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
Quote:
Originally Posted by BW-userx View Post
you assigned a variable with exactly \nsomethinghere\n\r then ran that code to match it, and it did not work? humm I find that hard to take in as it should not matter across distros
check your code.
Here is my output:
Code:
someline="\nhereweAre\n\r"
echo $someline

hereweAre

results="$(echo $someline | sed 's|\\nhereweAre\\n\\r|somethinghere|')"
echo $results

hereweAre
I think it depends on echo implementation

@sumitsahay: Does it work for you?
 
Old 10-09-2018, 09:19 AM   #14
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by l0f4r0 View Post
Here is my output:
Code:
someline="\nhereweAre\n\r"
echo $someline

hereweAre

results="$(echo $someline | sed 's|\\nhereweAre\\n\\r|somethinghere|')"
echo $results

hereweAre
I think it depends on echo implementation

@sumitsahay: Does it work for you?
that's insane, I've never seen that before, your var assignment is quoted, and it still does not add the \n and \n\r to it.
 
  


Reply



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] jhalfs sed: -e expression #1, char 55:Invalid preceding regular expression percy_vere_uk Linux From Scratch 10 07-22-2017 07:15 AM
[SOLVED] Need help with regular expression, /[a-z][\.\?!]\s+[A-Z]/ blueskynet Linux - Newbie 9 04-29-2012 01:41 PM
[SOLVED] Help with regular expression xavitxus Programming 3 08-28-2011 04:44 PM
Regular Expression harkonen Programming 6 07-12-2008 12:06 PM
regular expression (.*?) uttam_h Programming 6 05-30-2008 05:45 PM

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

All times are GMT -5. The time now is 02:00 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