How to cut the last field in this shell string
LINE="/string/to/cut.txt"
So that the string would look like this
LINE="/string/to/"
Thanks in advance!
对于它的价值,cut
基于 - 的解决方案:
NEW_LINE="`echo "$LINE" | rev | cut -d/ -f2- | rev`/"
I think you could use the "dirname" command. It takes in input a file path, removes the filename part and returns the path. For example:
$ dirname "/string/to/cut.txt"
/string/to
这将适用于现代 Bourne 版本,如 Dash、BusyBox ash 等,以及 Bash、Korn shell 和 Z shell 等后代。
LINE="/string/to/cut.txt"
LINE=${LINE%/*}
或保留最后的斜线:
LINE=${LINE%/*}/
echo $LINE | grep -o '.*/'
也可以。
echo "/string/to/cut.txt" | awk -F'/' '{for (i=1; i<NF; i++) printf("%s/", $i)}'