LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Regular Expression (https://www.linuxquestions.org/questions/linux-newbie-8/regular-expression-4175640001/)

sumitsahay 10-09-2018 02:29 AM

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

l0f4r0 10-09-2018 03:00 AM

Have you tried tr with option -d?

sumitsahay 10-09-2018 03:46 AM

Quote:

Originally Posted by l0f4r0 (Post 5912689)
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

syg00 10-09-2018 04:04 AM

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.

l0f4r0 10-09-2018 04:08 AM

Quote:

Originally Posted by sumitsahay (Post 5912700)
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 :)

sumitsahay 10-09-2018 04:24 AM

Quote:

Originally Posted by syg00 (Post 5912705)
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.

l0f4r0 10-09-2018 04:41 AM

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...

sumitsahay 10-09-2018 07:20 AM

Quote:

Originally Posted by l0f4r0 (Post 5912715)
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.

l0f4r0 10-09-2018 07:47 AM

Quote:

Originally Posted by sumitsahay (Post 5912756)
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

BW-userx 10-09-2018 07:56 AM

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

l0f4r0 10-09-2018 08:03 AM

Quote:

Originally Posted by BW-userx (Post 5912767)
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

BW-userx 10-09-2018 08:06 AM

Quote:

Originally Posted by l0f4r0 (Post 5912771)
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. ;)

l0f4r0 10-09-2018 08:18 AM

Quote:

Originally Posted by BW-userx (Post 5912773)
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?

BW-userx 10-09-2018 09:19 AM

Quote:

Originally Posted by l0f4r0 (Post 5912778)
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.


All times are GMT -5. The time now is 11:34 PM.