Shell skriptlari berilgan yo'ldan muayyan yo'lni aniqlaydi

To'liq yo'lim /data7/stmt_data16/pdf/RL/20170202/INLAND/641/K_EDGE1_641 va pdf katalogidan faqatgina yo'lni chiqarib olishni istayman, ya'ni /pdf/RL/20170202/INLAND/641/K_EDGE1_641 ga qarang.

Qanday qilib bu shell skriptida sed yoki awk buyruqlar bilan erishish mumkin?

0
Sizning tashabbusingiz qayerda?
qo'shib qo'ydi muallif grail, manba
Bu yerda yana bitta quetion mavjud yo'lni/data7/stmt_data16/pdf/RL/20170202/INLAND/641/K_EDGE1_641 dan "K_EDGE1_641" dan olingan fayl yo'lini diskdan olib qo'yishni istayman. , unda qanday qilib bunday qilish mumkin?
qo'shib qo'ydi muallif user1863300, manba

6 javoblar

Agar sizning qobig'ingiz bash bo'lsa, unda siz na sed, na awk kerak emas:

path='/data7/stmt_data16/pdf/RL/20170202/INLAND/641/K_EDGE1_641'
echo "${path#*/pdf/}"
RL/20170202/INLAND/641/K_EDGE1_641

Bu esa /pdf/ -ni qisqartiradi, lekin uni qo'l bilan qo'shib qo'yish mumkin:

echo "/pdf/${path#*/pdf/}"
1
qo'shib qo'ydi

AWK approach with string match and substr functions:

path='/data7/stmt_data16/pdf/RL/20170202/INLAND/641/K_EDGE1_641'
echo "$path" | awk 'match($0, /\/pdf.*/) {print substr($0, RSTART, RLENGTH) }'

Chiqish:

/pdf/RL/20170202/INLAND/641/K_EDGE1_641

SED approach:

path='/data7/stmt_data16/pdf/RL/20170202/INLAND/641/K_EDGE1_641'
echo "$path" | sed -n 's/.*\(\/pdf.*\)/\1/p'
0
qo'shib qo'ydi
path='/data7/stmt_data16/pdf/RL/20170202/INLAND/641/K_EDGE1_641'

echo "$path" |awk -F '_data16' '{print $2}'
/pdf/RL/20170202/INLAND/641/K_EDGE1_641
0
qo'shib qo'ydi
Iltimos, ba'zi tushuntirishlarni bering. Odatda kod faqatgina javoblarga to'g'ri keladi.
qo'shib qo'ydi muallif Gurwinder Singh, manba
bu sizning ma'lumotlaringiz16 doimo borligini bilsangizgina ishlaydi. Agar yo'lda ma'lumotlar17 bo'lsa, ishlamaydi.
qo'shib qo'ydi muallif George Vasiliou, manba

Shuningdek, quyidagi kabi bajarilishi mumkin:

sed -r 's#(.*)(/pdf/.*)#\2#' <<<"$yourpathvvar"

Sinash:

mypath="/data7/stmt_data16/pdf/RL/20170202/INLAND/641/K_EDGE1_641"
sed -r 's#(.*)(/pdf/.*)#\2#' <<<"$mypath"
/pdf/RL/20170202/INLAND/641/K_EDGE1_641

You can test it your self here: http://www.tutorialspoint.com/execute_bash_online.php?PID=0Bw_CjBb95KQMcGZudm8ySlRseTQ

0
qo'shib qo'ydi
@ user1863300 offcourse ishlaydi - testga qarang va o'zingizni sinab ko'ring. Ehtimol, tizimingizda turli versiyalar bormi?
qo'shib qo'ydi muallif George Vasiliou, manba
bu ishlamayapti
qo'shib qo'ydi muallif user1863300, manba

Bu erda juda ko'p echimlar topilgan ... yana bitta toza bashni qo'lga olamiz:

echo "${mypath/*pdf//pdf}"
0
qo'shib qo'ydi
@ user1863300 bilan basename $ var
qo'shib qo'ydi muallif George Vasiliou, manba
Rahmat, Bu yerda yana bitta quetion, men berilgan yo'l/data7/stmt_data16/pdf/RL/20170202/INLAND/641/K_EDGE1_641 dan "K_EDGE1_641" dan oxirgi fayl yo'lini diskdan chiqarishni istayman. fayl nomi, shuning uchun buni qanday qilish mumkin?
qo'shib qo'ydi muallif user1863300, manba

grep bilan yozing

$ path='/data7/stmt_data16/pdf/RL/20170202/INLAND/641/K_EDGE1_641'
$ echo $path | grep -o '/pdf.*'
$ /pdf/RL/20170202/INLAND/641/K_EDGE1_641

-o -print only match

0
qo'shib qo'ydi