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
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
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.