i need to replace '/' forward slash with \/(backward slash follwed by a forward slash) using sed command when the forward slash occurs as a first character in a file..
Tried something like this but doesn't seem to work.
Can someone help on this?
To "exclude the last line alone", you mean change all lines except the last one?
Actually, radoulov's sed didn't work for me, strangely.
That's because the replacement text in radoulov's solution is using an implementation-defined sequence. Since the slash is no longer the regular expression delimiter, preceding it with a backslash is undefined (as is a backslash before any non-special character).
In the replacement text of a sed substitute command:
Quote:
Originally Posted by POSIX
The meaning of a <backslash> immediately followed by any character other than '&' , <backslash>, a digit, or the delimiter character used for this command, is unspecified.
My guess is that scottn's sed treated the \/ as a / and replaced each / found with an identical /. However, since it is an implementation-defined situation, a sed implementation is free to yell at you, shake its fist at you, and refuse to process any data ... if it were so inclined.
The \\\/ in scottn's solution exhibits the same shortcoming as radoulov's: an undefined \/ sequence. \\\/ may yield the desired result with some sed implementations, but it is still depending on unspecified behavior.
A portable solution could use s|^/|\\/| or (the uglier, less readable) s/^\//\\\//.
Regards,
Alister
Last edited by alister; 09-28-2011 at 01:40 PM..
These 2 Users Gave Thanks to alister For This Post:
When radoulov's solution didn't work, I did the first thing I almost always do: Switch to different quotes - to double quotes in this case.
When it still didn't work, I did what I usually do second: Threw another slash at it (now we have sed "s|/|\\/"), which still didn't work. So I threw another slash at it(!) and bingo!
I have a file abc.txt which has records like
456 /home/fgg/abdc.txt
3567 /home/fdss/vfgb.txt
23 /home/asd/dfght.txt
I WANT TO REMOVE STRING UNTIL 3RD OCCURANCE OF FORWARD SLASH
Output should be like
abdc.txt
vfgb.txt
dfght.txt (5 Replies)
./split2.sh: line 1: split/ssl/pop3s.txt: No such file or directory
sort: cannot read: split/ssl/pop3s.txt: No such file or directory
Hi there,
I am pulling data from the following source:
ssl/http
ssl/http
ssl/http-alt
ssl/https
ssl/https
ssl/https
ssl/https
ssl/https
ssl/https... (3 Replies)
Hello experts.
I haven't been able to find a solution for this using the sed command.
I only want to replace the forward slash with string "FW_SLASH" only if there's a number right after the slash while preserving the original number.
I have a file containing 2 entries:
Original File:... (5 Replies)
hi
hope somebody can help, there seems to be bit on the net about this, but still cant make it work the way i need.
i have a file live this
mm dd ff /dev/name1
mm dd ff /dev/name2
mm dd ff /dev/name3
mm dd ff /dev/name4
i need to update /dev/name1 etc to /newdev/new/name1 etc
so... (5 Replies)
What is the significance of the forward slash(/) while specifying a directory?
cp -av /dir/ /opt/
and
cp -av /dir /opt
Does effectively the same job it seems? (2 Replies)
Hello all,
I'd like to replace "," by "/" in a utf8 file from postion X to Y. Comma "," is also defined as delimiter.
12345678901234567890,123456789012345,12345678901234567890,
aaaa,aaaa,aaaaa ,bbb,bbbb,bbbbb ,cccccc,cc ,
Result should be... (1 Reply)
Hi all,
I need to know way of inserting backward slash before forward slash. My problem is that i need to supply directory path as an argument while invoking cshell script. This argument is further used in script (i.e. sed is used to insert this path in some file). So i need to place \ in front... (2 Replies)
how to escape / (forward slash) in a string.
I have following scnerio:
sed s/${var1}{$var2}
var1 and var2 both contain slashes, but sed gives error if there is a slash in var1 or var2. sed is used here to replace var1 with var2.
Thanks in advance (1 Reply)
Using the script: (Called replaceit)
#!/bin/ksh
String=$1
Replace=$2
sed -e "s/${orig}/${new}/g" oldfile.txt > newfile.txt
In oldfile.txt, I'm looking for: getenv("Work")
And change it To: /u/web
I execute the script:
replaceit "getenv(\""Work\"")" /u/web
I'm getting sed... (3 Replies)