简体   繁体   中英

Remove basename (last directory) from a path and store them in a file

In bash, how to do it?

For eg i have a text file named FILE1 having 4 paths in it seperated by new line:

abc/def/zzz.txt
ghi.jkl/zzz.txt
mno.pqr/zzz.txt
stu.wvx/zzz.txt

I want to create another file named FILE2 from FILE1, which only includes:

abc/def/
ghi/jkl/
mno/pqr/
stu/wvx/

How to do this?

using sed :

sed -r  's|[^\/]+$||g' FILE1 > FILE2

and see regex demo .

#!/bin/bash

for line in $(cat FILE1)
do
    echo "${line%/*}/" >> FILE2
done

or

#!/bin/bash

while read -r line
do
    echo "${line%/*}/" >> FILE2
done < FILE1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM