Awk to replace directory paths


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk to replace directory paths
# 1  
Old 11-04-2011
Awk to replace directory paths

Hi all,

I have written a bash script to do a few things for my Splunk deployment, however, I am currently stuck on one part...

I need to the current working directory (I collect this with `pwd`) in the script as it could in theory be run from a number of locations. I'm not that great with awk and haven't used it in a while. Below is the lines i'm trying to convert to/from:
FROM:
Code:
/var/tmp/scripts/testbed/tcpdump/bin

TO:
Code:
\\/var\\/tmp\\/scripts\\/testbed\\/tcpdump\\/bin

I need to add the "\\" to the path as i'm using echo -e to write to another file so one "\" will be escaped. and then I need the other to be there in the file (as it's part of a regex) I hope this makes sense.

Thanks in advance,

Matt
# 2  
Old 11-04-2011
Code:
sed 's;/;\\\\/;g'

# 3  
Old 11-04-2011
Code:
echo '/var/tmp/scripts/testbed/tcpdump/bin' | sed 's#/#\\\\&#g

# 4  
Old 11-04-2011
Thanks guys that did the trick, now my regex just needs fixing....
I've not used sed before, i know it replaces text just unsure of the syntax
# 5  
Old 11-04-2011
Quote:
Originally Posted by TauntaunHerder
I need to add the "\\" to the path as i'm using echo -e to write to another file so one "\" will be escaped. and then I need the other to be there in the file (as it's part of a regex) I hope this makes sense.
If you explain in detail what you're actually trying to accomplish, I bet there's a simpler and more direct way than this.
# 6  
Old 11-04-2011
If you really want to use awk:
Code:
awk -F/ '{OFS="\\\\/"; $1=$1; print}'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with reading directory paths with spaces from a file

Hi I want to know how to handle the spaces in the below scenario. I have a file (CON_zip_path_1.txt) which has some directory paths with spaces in directory names . My requirement is to unzip these zip files to another path. Please see the code below and the error. CON_zip_path_1.txt... (4 Replies)
Discussion started by: paul1234
4 Replies

2. Shell Programming and Scripting

Show only the filenames under a directory without relative and absolute paths.

I am able to list all the filenames under a directory & its sub-directories except blent.tar on Linux find "/tmp/" -type f | grep -v blent.tar | rev | cut -d '/' -f1 | rev Desired Output: THIRDPARTYLICENSEREADME.txt javaws libjavaplugin_oji.so libjavaplugin_oji.so... (3 Replies)
Discussion started by: mohtashims
3 Replies

3. Shell Programming and Scripting

Replace directory paths in multiple files at once

I need to update about 2400 files in a directory subtree, with a new directory path inside the files I need to change this occurence in all files: /d2/R12AB/VIS/apps/tech_st/10.1.2 with this: /u01/PROD/apps/apps_st/10.1.3 I know how to change single words using "find . -type f -print0 |... (6 Replies)
Discussion started by: wicus
6 Replies

4. Shell Programming and Scripting

Unable to export Directory paths through Script

Following is the output of .profile file - $ cat /home/estdm2/.profile #!/bin/ksh set -o vi alias l="ls -ltr" umask 002 Following is the path setting script. $ cat DM_ENV_VARS_NEW.ksh #!/bin/ksh . /home/estdm2/.profile sEnv=$1 echo "We are in ${sEnv} environment" echo "STEP010... (3 Replies)
Discussion started by: sumeet
3 Replies

5. Shell Programming and Scripting

Find text containing paths and replace with a string in all the python files

I have 100+ python files in a single directory. I need to replace a specific path occurrence with a variable name. Following are the find and the replace strings: Findstring--"projects\\Debugger\\debugger_dp8051_01\\debugger_dp8051_01.cywrk" Replacestring--self.projpath I tried... (5 Replies)
Discussion started by: noorsam
5 Replies

6. UNIX for Dummies Questions & Answers

cat a list of directory paths only to a file

Hi! I would like to funnel a series of directories and subdirectories into a text file. This is the output I would like to see from a find command: /mypath/ABC_01/VISIT_01 /mypath/ABC_01/VISIT_02 /mypath/ABC_01/VISIT_03 /mypath/ABC_02/VISIT_01 /mypath/ABC_03/VISIT_01 I've tried: find... (2 Replies)
Discussion started by: goodbenito
2 Replies

7. UNIX Desktop Questions & Answers

how to display paths of files in a directory

hi guys does anyone know how to display the file paths of the files stored within a directory at the command terminal? e.g. if i have a directory called "home", how do i display the file paths of the files inside the directory? cheers (2 Replies)
Discussion started by: Villaman69
2 Replies

8. Shell Programming and Scripting

Find Directory from array of file names with paths

I have a script that generates a variable with the location of a file and its complete path. What i want to do is to "cd" to the directory where that file is located using the path name of the file. GIS has absolutely failed me. For example when i run my script it generates a variable called... (1 Reply)
Discussion started by: Knome
1 Replies

9. Solaris

find home directory paths for all users

How to find al the user's home directories? (2 Replies)
Discussion started by: a2156z
2 Replies

10. Shell Programming and Scripting

To find and replace paths consisting \

Hi all, I am new to unix and wanted to replace the string consisting \ with nothing. I tried the following but did not help. for y in `ls DIV*`; do sed s/C:\\Project\\AML\\bin//g $y > temp mv temp $y done I actually want to remove any occurance of C:\Project\ABC\bin in... (4 Replies)
Discussion started by: cv_pan
4 Replies
Login or Register to Ask a Question