0

Hei everyone!

I have this variable in shell containing paths separated by a space:

LINE="/path/to/manipulate1 /path/to/manipulate2"

I want to add additional path string in the beginning of the string and as well right after the space so that the variable will have the result something like this:

LINE="/additional/path1/to/path/to/manipulate1 /additional/path2/to/path/to/manipulate2"

I tried this one but only get the old paths

#!/bin/bash

LINE="/path/to/one /path/to/two"
NEW_PATH=`echo $LINE |  sed "s/^\([^ ]\+\) \([^ ]\+\)/\/add1\1 \/add2\2/"`
echo "$NEW_PATH"

Any help appreciated Thanks in advance

4
  • 1
    So what you want to add? What is path1 in the first case and path2 in the second case? Or did you mean to add the same for both? Commented Dec 30, 2010 at 9:58
  • It doesn't matter actually, just want to know how to add something in front of the string and something else after the space. Commented Dec 30, 2010 at 10:05
  • Yeah, I got it after a re-read. Haven't finished my morning coffee yet. Commented Dec 30, 2010 at 10:09
  • Can I put a directory with a space in your filesystem? Can I? Pleeease? Commented Jan 1, 2011 at 21:27

5 Answers 5

2

This of courses messes with any previous arguments you might need to keep.

set $LINE
LINE="/additional/path1$1 /additional/path2$2"

Tested in bash/dash/ksh.

Edit: If keeping the original arguments is needed, this might be useful:

orig=$@
<stuff from above>
set $orig
Sign up to request clarification or add additional context in comments.

Comments

1
firstPath=$(echo $LINE | cut -d' ' -f1)
secondPath=$(echo $LINE | cut -d' ' -f2)

firstPath="/additional/path1/to$firstPath"
secondPath="/additional/path2/to$secondPath"

1 Comment

@user558134: Remember to upvote those answers that prove useful to your problem, also, mark as accepted the one that solves it. ;)
1
$ test="/sbin /usr/sbin /bin /usr/bin /usr/local/bin /usr/X11R6/bin"

$ test2=$(for i in $test; do echo "/newroot${i}"; done)

$ echo $test2
/newroot/sbin /newroot/usr/sbin /newroot/bin /newroot/usr/bin /newroot/usr/local/bin /newroot/usr/X11R6/bin

Comments

1

Since you're using Bash:

If the additions are the same for each part:

LINE="/path/to/manipulate1 /path/to/manipulate2"
array=($LINE)
LINE=${array[@]/#//additional/path/to/}

If they are different:

LINE="/path/to/manipulate1 /path/to/manipulate2"
array=($LINE)
array[0]=/additional/path1/to${array[0]}
array[1]=/additional/path2/to${array[1]}
LINE=${array[@]}

Or, more flexibly:

LINE="/path/to/manipulate1 /path/to/manipulate2"
array=($LINE)
parts=(/additional/path1/to /additional/path2/to)
if (( ${#array[@]} == ${#parts[@]} ))
then
    for ((i=0; i<${#array[@]}; i++))
    do
        array[i]=${parts[i]}${array[i]}
    done
fi
LINE=${array[@]}

Comments

0
NEW_PATH=`echo $LINE |  sed "s/^\([^ ]\+\) \([^ ]\+\)/\/add1\1 \/add2\2/"`

results in NEW_PATH =

/add1/path/to/manipulate1 /add2/path/to/manipulate2

10 Comments

Might want to escape those backticks or use $(...)
I tried this one: NEW_PATH=$(echo $LINE | sed "s/^([^ ]\+) ([^ ]\+)/\/add1\1 \/add2\2/") but no new path
did you try with the back tick character? I'm using the line i've sent you in bash (with the backticks) and it works
I tried this again and only get the old paths, not the new ones
could you paste here the script?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.