Rock26 Rock26 - 4 months ago 7
Bash Question

remove current path portion from each line in a text file

I have a bash script which generates text file. But I want to delete current path portion from each line. I tried to run "cut -d'/'
--complement -s -f1-4" command. But I want some dynamic way to do it. Can someone please help me with it? Thank you !

Sample text file:

/local/name1/test/dir/file1.c
/local/name1/test/dir/subdir/file2.h
/local/name1/test/dir2/file3.txt


Current path:

/local/name1/test/


Expected Result:

dir/file1.c
dir/subdir/file2.h
dir2/file3.txt

Answer

Using sed you can do:

sed 's~^/local/name1/test/~~' file

dir/file1.c
dir/subdir/file2.h
dir2/file3.txt

If your current path is saved in a variable p then use:

sed "s~^$p~~" file
Comments