Replace a filename with full path using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace a filename with full path using sed
# 1  
Old 10-06-2008
Replace a filename with full path using sed

hi,

i need to replace a line a file with a new raw device location..
original file..

/opt/sybase/ASE1502/ASE-15_0/bin/dataserver \
-d/data/TST_AKS1/sybdevices/master.dat \
-e/logs/sybase/TST_AKS1/SFO_TST_AKS1.log \
-c/apps/sybase/ASE1502/ASE-15_0/TST_AKS1.cfg \
-M/apps/sybase/ASE1502/ASE-15_0 \


i need to change -d value to /dev/vx/rdsk/sandg-100/raw200-01
so new file should look like

/opt/sybase/ASE1502/ASE-15_0/bin/dataserver \
-d/dev/vx/rdsk/sandg-100/raw200-01 \
-e/logs/sybase/TST_AKS1/SFO_TST_AKS1.log \
-c/apps/sybase/ASE1502/ASE-15_0/TST_AKS1.cfg \
-M/apps/sybase/ASE1502/ASE-15_0 \

i tried using sed
i set the new file name in a varaiable

export mydev=/dev/vx/rdsk/sandg-100/raw200-01
sed s/^-d.*/$mydev/g

it didn't work..

any idea how to make work, it would be nice if i can use a shell variable inside sed command to replace the value...

i even tried awk, but didn't work either

awk '{ if ($1 ~= "-d") print $0 else print $mydevice }' <filename

thanks
AK
# 2  
Old 10-06-2008
Quote:
Originally Posted by aksaravanan
hi,

i need to replace a line a file with a new raw device location..
original file..

/opt/sybase/ASE1502/ASE-15_0/bin/dataserver \
-d/data/TST_AKS1/sybdevices/master.dat \
-e/logs/sybase/TST_AKS1/SFO_TST_AKS1.log \
-c/apps/sybase/ASE1502/ASE-15_0/TST_AKS1.cfg \
-M/apps/sybase/ASE1502/ASE-15_0 \


i need to change -d value to /dev/vx/rdsk/sandg-100/raw200-01
so new file should look like

/opt/sybase/ASE1502/ASE-15_0/bin/dataserver \
-d/dev/vx/rdsk/sandg-100/raw200-01 \
-e/logs/sybase/TST_AKS1/SFO_TST_AKS1.log \
-c/apps/sybase/ASE1502/ASE-15_0/TST_AKS1.cfg \
-M/apps/sybase/ASE1502/ASE-15_0 \

i tried using sed
i set the new file name in a varaiable

export mydev=/dev/vx/rdsk/sandg-100/raw200-01
sed s/^-d.*/$mydev/g

it didn't work..

any idea how to make work, it would be nice if i can use a shell variable inside sed command to replace the value...

Use a different delimiter when you have slashes in one of the fields:

Code:
mydev=/dev/vx/rdsk/sandg-100/raw200-01
sed "s|^-d.*|$mydev|g"

Quote:
i even tried awk, but didn't work either

awk '{ if ($1 ~= "-d") print $0 else print $mydevice }' <filename

Code:
awk -v mydev="$mydev" '/^-d/ { print mydev; next } { print }' filename

# 3  
Old 10-06-2008
Very simple to use SED here:

Code:
sed '/-d\/data/c\
-d/dev/vx/rdsk/sandg-100/raw200-01 \\
' filename

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find and replace the path value in files, pattern is not full known.

Hi, I need to do find and replace, but the pattern is not full known. for example, my file has /proj/app-d1/sun or /data/site-d1/conf here app-d1 and site-d1 is not constant. It may be different in different files. common part is /proj/xx/sun and /data/xxx/conf i want to find where ever... (6 Replies)
Discussion started by: rbalaj16
6 Replies

2. UNIX for Beginners Questions & Answers

Bash script - Remove the 3 top level of a full path filename

Hello. Source file are in : /a/b/c/d/e/f/g/some_file Destination is : /d/e where sub-directories "f" and "g" may missing or not. After copying I want /a/b/c/d/e/f/g/file1 in /d/e/f/g/file1 On source /a is top-level directory On destination /d is top-level directory I would like... (2 Replies)
Discussion started by: jcdole
2 Replies

3. Shell Programming and Scripting

Pattern match a path anywhere in the line and replace it with new path

I want to pattern match only path part from below and replace them with new path string. LoadModule jk_module /fldrA/fldrBaf/fldrCaa/modules/mod_jk.so JkWorkersFile /fldrA/fldrBaf/fldrCaa/config/OHS/ohs1/workers.properties JkLogFile... (4 Replies)
Discussion started by: kchinnam
4 Replies

4. UNIX for Dummies Questions & Answers

Sed- Replace space in filename by a \

`echo $file | sed 's/ / /g'` Hey guys I want help in converting the spaces in my file names to '\ ' . Example: UK maps --> UK\ maps Could someone please help me. I have tried the following sequences already (none of them work): 1)s/ /\ /g 2)s/ /\\ /g 3)s/ /\\\ /g Can someone... (7 Replies)
Discussion started by: INNSAV1
7 Replies

5. Shell Programming and Scripting

sed to replace pattern with filename

Hi all, I'm trying to replace a pattern/string in about 100 files with the filename using following commands but getting nowhere: for f in *.fa; do sed "s/^>.*/>$f/g" $f > $f_v1.fa; done for f in *.fa; do sed 's/^>.*/>`echo $f`/' > $fa_v1.fa; done Basically I want to change any line... (5 Replies)
Discussion started by: ivpz
5 Replies

6. Shell Programming and Scripting

find and replace a path with sed?

Hi, I am attempting to find the path /subject/grin* and replace it with /subject/$i My attempt: for i in $(ls) do sed -e 's:"/subjects/grin.*":"/subjects/$i.*":g' and several variants with no luck. Please help. Thanks! (8 Replies)
Discussion started by: JC_1
8 Replies

7. Shell Programming and Scripting

How to extract strings from full path when full path is not fixed

/Path/snowbird9/nrfCompMgrRave1230100920.log.gz:09/20/2010 06:14:51 ERROR Error Message. /Path/snowbird6/nrfCompMgrRave1220100920.log.gz:09/20/2010 06:14:51 ERROR Error Message. /Path/snowbird14/nrfCompMgrRave920100920.log.gz:09/20/2010 06:14:51 ERROR Error Message.... (0 Replies)
Discussion started by: Shirisha
0 Replies

8. Shell Programming and Scripting

Retrieve directory path from full file path through sh

Hi, I have a file abcd.txt which has contents in the form of full path file names i.e. $home> vi abcd.txt /a/b/c/r1.txt /q/w/e/r2.txt /z/x/c/r3.txt Now I want to retrieve only the directory path name for each row i.e /a/b/c/ /q/w/e/ How to get the same through shell script?... (7 Replies)
Discussion started by: royzlife
7 Replies

9. Shell Programming and Scripting

getting full path from relative path

given a relative path, how do i convert it into a full one. i.e. if i am in /home/polypus and i am given foo/bar then to get a full path i can just concatinate it with pwd, but what if i am given "../mama" how do i programmatically convert: /home/polypus and ../mama into ... (4 Replies)
Discussion started by: polypus
4 Replies

10. Shell Programming and Scripting

need help on sed (replace string without changing filename)

I have awhole bunch of files and I want to edit stringA with stringB without changing the filename. I have tried the following sed commands: sed "s/stringA/stringB/g" * This will print the correct results but does not actually save it with the new content of the file. when I do a cat on... (5 Replies)
Discussion started by: jjoves
5 Replies
Login or Register to Ask a Question