Replace Forward Slash with sed


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Replace Forward Slash with sed
# 8  
Old 09-28-2011
Hi Scottn,

Thanks for the code, but it still replaces the last line, any idea on how to skip the last line alone?
# 9  
Old 09-28-2011
Yes, it's the command I showed you, instead with single quotes, and two backslashes. More accurately put, it's radoulov's original solution with an extra backslash (and with $!s instead of 1s, since that was answering a slightly different question).

Code:
sed '$!s|^/|\\/|' file

This User Gave Thanks to Scott For This Post:
# 10  
Old 09-28-2011
i tried with some test file for which it worked correctly but for my actual file it still replaces the last line.. so on analysis i found there are two blank lines as shown below ><

Code:
 
end;
/
(Blank line here) 
(Blank line here)

Sorry if i am asking too much , if there is any way for this ? ..

Can we search for 'end;' and skip the / which is coming after it or something .. is it even doable?
# 11  
Old 09-28-2011
Quote:
... if i could change the script to exclude the last line alone from this replace.
This question makes me think that the OP needs something different than my interpretation of:

Quote:
... when the forward slash occurs as a first character in a file
And yes, I've pointed out that the code was implementation-specific.

To the OP: If you post a bigger sample of your input and an example of the expected output you'll get a solution faster.
# 12  
Old 09-28-2011
This is the sample

Code:
 
declare
act_errors number(10, 0);
at_least_one_exception EXCEPTION;
err_num NUMBER;
err_msg VARCHAR2(1000);
begin
act_errors := 0;
dbms_output.put_line('Starting to process  '|| 'cdd');
begin
insert into CS_CASE
(CASE_ID,id,desc) VALUES
('c12', 
'nbkany2' , 
'Credit Card
/
card' , 
);
exception
when others then
act_errors := 28;
err_num := SQLCODE;
err_msg := SUBSTR(SQLERRM, 1, 1000);
dbms_output.put_line('Statement number ' || act_errors || ' failed (search for "act_errors := ' || act_errors || '" in the source file)');
dbms_output.put_line(err_msg );
end;
/
(blank line here)
(blank line here)

and the output should be

Code:
 
declare
act_errors number(10, 0);
at_least_one_exception EXCEPTION;
err_num NUMBER;
err_msg VARCHAR2(1000);
begin
act_errors := 0;
dbms_output.put_line('Starting to process  '|| 'cdd');
begin
insert into CS_CASE
(CASE_ID,id,desc) VALUES
('c12', 
'nbkany2' , 
'Credit Card
\/
card' , 
);
exception
when others then
act_errors := 28;
err_num := SQLCODE;
err_msg := SUBSTR(SQLERRM, 1, 1000);
dbms_output.put_line('Statement number ' || act_errors || ' failed (search for "act_errors := ' || act_errors || '" in the source file)');
dbms_output.put_line(err_msg );
end;
/
(blank line here) 
(blank line here)

# 13  
Old 09-28-2011
That was just a sample.. there could be multiple occurances of '\' coming as first character in that file.

Also because of the last two blank lines
We can no longer use
Code:
$!s|^/|\\/|

since '/' is not is not in the last line as i expected to be.

Maybe we can write a sed which will not replace the last occurance of '/' alone? .. is it possible?

Last edited by radoulov; 09-28-2011 at 05:26 PM.. Reason: Code tags!
# 14  
Old 09-28-2011
What tool/program generates that output (the PL/SQL code)?
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace string until 3Rd occurance of forward slash(/)

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)
Discussion started by: himanshupant
5 Replies

2. Shell Programming and Scripting

Escaping Forward Slash

./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)
Discussion started by: alvinoo
3 Replies

3. Shell Programming and Scripting

sed command to replace slash in date format only

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)
Discussion started by: pchang
5 Replies

4. Shell Programming and Scripting

AWK or SED to replace forward slash

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)
Discussion started by: dshakey
5 Replies

5. Shell Programming and Scripting

Significance of forward slash(/) while specifying a directory

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)
Discussion started by: proactiveaditya
2 Replies

6. Shell Programming and Scripting

How to replace comma by slash using sed in an UTF8 file

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)
Discussion started by: fmofmo
1 Replies

7. Shell Programming and Scripting

Using sed to append backward slash before forward slash

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)
Discussion started by: sarbjit
2 Replies

8. Shell Programming and Scripting

escaping / (forward slash)

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)
Discussion started by: farooqpervaiz
1 Replies

9. Shell Programming and Scripting

Help with SED and forward slash

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)
Discussion started by: gseyforth
3 Replies

10. Shell Programming and Scripting

grep for forward slash

How can I use grep to grab a line that contains a forward slash? I've tried: grep "/pd " file, Inevitably it just grabs pd not /pd. (3 Replies)
Discussion started by: wxornot
3 Replies
Login or Register to Ask a Question