LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   sed/awk - delete special character from a file (https://www.linuxquestions.org/questions/linux-newbie-8/sed-awk-delete-special-character-from-a-file-4175614474/)

freeroute 09-25-2017 07:36 AM

sed/awk - delete special character from a file
 
How could I remove backslash special character from a file with sed/awk?

input:
Code:

$P$DuOxmeTb7uGqNkkzenh6BqBu5OmpDF\/
$P$DPUN9ULZv9ICq.o53w4Kk\/685Q7mdW0
$P$DbHTs7pYRbB4s5sHzXh\/JK7kFa4SIT0

output:
Code:

$P$DuOxmeTb7uGqNkkzenh6BqBu5OmpDF/
$P$DPUN9ULZv9ICq.o53w4Kk/685Q7mdW0
$P$DbHTs7pYRbB4s5sHzXh/JK7kFa4SIT0

tried with "tr", it works:
Code:

tr -d "\\" <file | less
with "sed" I got error message:
Code:

sed -E 's/.*(\/).*/\/g' file | less

freeroute 09-25-2017 07:53 AM

I found the solution:
Code:

sed -E 's/\\\//\//g' file | less

grail 09-25-2017 08:54 AM

Why not sue the same code from tr in sed? Your first sed is in no way anything the same and your second seems to require additional information.
Code:

sed -E 's/\\//g' file

pan64 09-25-2017 09:15 AM

using sed you may try another delimiter:
Code:

sed -E 's!\\/!/!g' file | less
that will be much more readable

freeroute 09-25-2017 09:32 AM

Thanks. Yes the delimiter was forward slash, and it was hard to read the code...

freeroute 10-19-2017 04:35 AM

I try to delete the escape character using awk. (deleting with sed or tr works)
Command:
Code:

awk '{gsub ("\\", ""); print $0}' file
or
Code:

awk '{gsub ("'\'", ""); print $0}' file
Not working (substitute backslash with nothing). Why?

freeroute 10-19-2017 06:09 AM

Solution: awk '{gsub(/\\/, ""); print}' file.txt

Regex must be between forward slashes.


All times are GMT -5. The time now is 11:50 AM.