AWK or SED to replace forward slash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK or SED to replace forward slash
# 1  
Old 02-27-2012
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
Code:
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 the file should look like this afterwards
Code:
mm dd ff /newplace/new/name1
mm dd ff /newplace/new/name2
mm dd ff /newplace/new/name3
mm dd ff /newplace/new/name4

i have tired awk and sed and not getting wait i want,
any help would be great

Moderator's Comments:
Mod Comment Please use next time
code tags for your code and data
# 2  
Old 02-27-2012
escape the / with a \
Code:
sed 's/\/dev/\/newdev\/new/' ~/tmp.dat
mm dd ff /newdev/new/name1
mm dd ff /newdev/new/name2
mm dd ff /newdev/new/name3
mm dd ff /newdev/new/name4

# 3  
Old 02-27-2012
Or just use a different separator in sed so you don't need to escape 367 different things:

Code:
sed 's#/dev/#/newdev/#' filename

# 4  
Old 02-27-2012
thanks,
# 5  
Old 02-27-2012
with awk

Code:
 awk '{sub(/\//,"/newplace/"); print}' filename

# 6  
Old 02-27-2012
Here's simple way's to achieve so...
Code:
# sed  "s/dev/newdev\/new/g"   my-file      OR
# awk -F'dev' '{print $1 "newplace/new" $2}'   my-file

--Shirish Shukla

Last edited by Shirishlnx; 02-27-2012 at 11:44 PM..
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. UNIX for Dummies Questions & Answers

Awk pattern with letters and forward slash

Hi, I have a tab delimited file "test.txt" like this: id1 342 C/T id2 7453 T/A/-/G/C id3 531 T/C id4 756 A/T/G id5 23 A/G id6 717 T/A/C id7 718 C/T/A And so on, with the possible choices for letters being A,C,T,G. I would like to exclude from my file all the lines that do not have... (3 Replies)
Discussion started by: francy.casa
3 Replies

5. UNIX for Dummies Questions & Answers

Replace Forward Slash with sed

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. find $1 -print0 | xargs -0 sed -i -e 's/^\//\\\//g' Can someone... (19 Replies)
Discussion started by: depakjan
19 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