0

I have the following string:

/book/A00001/2018/01/15/Chamber_Music

And I want to get using the sed command:

/book/A00001/2018/01/15/

Thanks

Regards

lanz
  • 481
  • 1
  • 5
  • 10

3 Answers3

1

Maybe are you looking for: sed "s/\(.*\)Chamber_Music/\1/g"

manzerbredes
  • 310
  • 2
  • 13
0

No need to use sed, you can use normal shell string handling:

filename='gash.txt'
new_filename="$filename.new"

while read line
do
    line=${line%/*}
    echo $line 

done <"$filename" >"$new_filename"  

#mv "$new_filename" "$filename"     # Commented out to be optional

Given your input in your second question:

/book/A00001/2018/01/15
/book/A00001/2018/01/15
/book/A00001/2018/01/15
/book/A00001/2018/01/15
/book/A00001/2018/01/15
/book/A00001/2018/01/15
cdarke
  • 42,728
  • 8
  • 80
  • 84
0

You can change the regexp sed delimiter.... see the s command documentation. If you use, e.g.

sed 's:([a-zA-Z0-9/]*)[a-zA-Z]*$:\1:'

then, the / loses its special treatment and the : character assumes it. Of course, you can store the matching pattern in an environment variable, before to feed it to sed(1), and substitute all / into \/ to escape every /.

pattern=`echo "([a-zA-Z0-9/])[a-zA-Z]*\$" | sed 's/\//\\//'`
sed "s/${pattern}/\\1/"

but this is more complex.

Luis Colorado
  • 10,974
  • 1
  • 16
  • 31