Path a variable to sed that includes a path


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Path a variable to sed that includes a path
# 1  
Old 03-04-2011
Path a variable to sed that includes a path

Hi I'm trying to select text between two lines, I'm using sed to to this, but I need to pass variables to it. For example

start="BEGIN /home/mavkoup/data"
end="END"

sed -n -e '/${start}/,/${end}/g' doesn't work. I've tried double quotes as well. I think there's a problem with the / in the pathname. I've also tried using sed to replace them with \/ but I can't get that to work either.

Many thanks for your help.
# 2  
Old 03-04-2011
Hi.

Replace the / with something else.

Code:
sed "s#....#....#"
sed "s!...!...!"
etc.

# 3  
Old 03-04-2011
But the file I'm selecting text from already contains the path names. Replacing them in the file is not appropriate. My file has a bunch of

BEGIN path name

some text

END
BEGIN another path name

some text

END

I know the different path names used, example /home/mavkoup/data, home/mavkoup/info/ and so on.

I want to grep a specific BEGIN END block by using a variable and the block I want changes.
# 4  
Old 03-04-2011
Oops. Sorry I misread your post.

Try with awk?
Code:
awk '$0 ~ S { p=1 }; p; $0 ~ E { exit }' S="$start" E="$end" file


Last edited by Scott; 03-04-2011 at 10:09 AM..
# 5  
Old 03-04-2011
I never got the hang of awk, and I can't get that command to work lol. I'll keep working on it.

---------- Post updated at 10:37 AM ---------- Previous update was at 09:36 AM ----------

Arg I'm annoyed. Here's my actual code:

Code:
Some process sets $var to:
var="/home/mavkoup/data"

start_f="BEGIN_FILE_INFO `echo ${var}`"

cat input.txt | sed -n -e "/${start_f}/,/END_FILE_INFO/p" > output.txt

I get the error sed: command garbled: /BEGIN_FILE_INFO /home/mavkoup/data/,/END/p

If instead I use
var="BEGIN_FILE_INFO" the code runs fine.

---------- Post updated at 10:53 AM ---------- Previous update was at 10:37 AM ----------

FYI I got it. Needed to figure out how to replace the / with \/ in the path. s/\//\\\//g wouldn't work, needed it to be s:\/:\\\/:g and then everything was good again!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Convert Relative path to Absolute path, without changing directory to the file location.

Hello, I am creating a file with all the source folders included in my git branch, when i grep for the used source, i found source included as relative path instead of absolute path, how can convert relative path to absolute path without changing directory to that folder and using readlink -f ? ... (4 Replies)
Discussion started by: Sekhar419
4 Replies

2. UNIX for Advanced & Expert Users

Command to see the logical volume path, device mapper path and its corresponding dm device path

Currently I am using this laborious command lvdisplay | awk '/LV Path/ {p=$3} /LV Name/ {n=$3} /VG Name/ {v=$3} /Block device/ {d=$3; sub(".*:", "/dev/dm-", d); printf "%s\t%s\t%s\n", p, "/dev/mapper/"v"-"n, d}' Would like to know if there is any shorter method to get this mapping of... (2 Replies)
Discussion started by: royalibrahim
2 Replies

3. Shell Programming and Scripting

Moving files from parent path to multiple child path using bash in efficient way

Hi All, Can you please provide some pointers to move files from Base path to multiple paths in efficient way.Folder Structure is already created. /Path/AdminUser/User1/1111/Reports/aaa.txt to /Path/User1/1111/Reports/aaa.txt /Path/AdminUser/User1/2222/Reports/bbb.txt to... (6 Replies)
Discussion started by: karthikgv417
6 Replies

4. Shell Programming and Scripting

sed - replacement file path with variable - Escaping / character

Hi,, I have the line below in a file: $!VarSet |LFDSFN1| = '"E:\APC\Trials\20140705_427_Prototype Trial\Data\T4_20140705_Trial_Cycle_Data_13_T_Norm.txt" "VERSION=100 FILEEXT=\"*.txt\" FILEDESC=\"General Text\" "+""+"TITLE{SEARCH=NONE NAME=\"New Dataset\" LINE=1I want to write a script to change... (2 Replies)
Discussion started by: carlr
2 Replies

5. Shell Programming and Scripting

Appending a path in user's PATH variable

Hello Folks, I want to append a path in user's PATH variable which should be available in current session. Background Numerous persons will run a utility. Aim is to add the absolute path of the utility the first time it runs so that next runs have the PATH in env & users can directly run... (6 Replies)
Discussion started by: vibhor_agarwali
6 Replies

6. Shell Programming and Scripting

one liner to extract path from PATH variable

Hi, Could anyone help me in writing a single line code by either using (sed, awk, perl or whatever) to extract a specific path from the PATH environment variable? for eg: suppose the PATH is being set as follows PATH=/usr/bin/:/usr/local/bin:/bin:/usr/sbin:/usr/bin/java:/usr/bin/perl3.4 ... (2 Replies)
Discussion started by: royalibrahim
2 Replies

7. Shell Programming and Scripting

remove a path from PATH environment variable

Hi I need a script which will remove a path from PATH environment variable. For example $echo PATH /usr/local/bin:/usr/bin:test/rmve:/usr/games $echo rmv test/rmve Here I need a shell script which will remove rmv path (test/rmve) from PATH... (9 Replies)
Discussion started by: madhu84
9 Replies

8. Shell Programming and Scripting

need sed command to read a path and set to variable

I have a variable called PATH that contains a path example: /Users/rtipton/Desktop/testusers/test I need a sed command to set a variable called USER to the last directory name in that path PATH="/Users/rtipton/Desktop/testusers/test" and from that PATH i need USER to = test I know sed... (4 Replies)
Discussion started by: tret
4 Replies

9. Shell Programming and Scripting

Sed variable substitution when variable constructed of a directory path

Hello, i have another sed question.. I'm trying to do variable substition with sed and i'm running into a problem. my var1 is a string constructed like this: filename1 filerev1 filepath1 my var2 is another string constructed like this: filename2 filerev2 filepath2 when i do... (2 Replies)
Discussion started by: alrinno
2 Replies

10. Shell Programming and Scripting

SED - How to return PATH from PATH/NAME

Hi, How can I get /usr/people/me/ from /usr/people/me/file.abc with sed? Thanks... (2 Replies)
Discussion started by: cybotic
2 Replies
Login or Register to Ask a Question