2013年9月27日金曜日
フルパスのファイル名をPATHとファイル名
拡張子とファイル名に分割する正規表現を試してみた。
もっと良い方法があるかも?!
シェルスクリプト
stimac:~ st$ cat test2.sh
#!/bin/bash
test_file=/Users/st/Desktop/rails3book-121108/chapter1/README.rdoc
echo ${test_file}
echo ${test_file} | sed 's/.*\///g'
echo ${test_file} | sed 's/\/[^/]*$//g'
echo ${test_file} | sed 's/.*\.//g'
echo ${test_file} | sed 's/.*\///g' | sed 's/\.[^.]*$//g'
実行結果
stimac:~ st$ sh test2.sh
/Users/st/Desktop/rails3book-121108/chapter1/README.rdoc
README.rdoc
/Users/st/Desktop/rails3book-121108/chapter1
rdoc
README
stimac:~ st$
BSDな人にはこんな方法もあるようだ?!
stimac:~ st$ cat test1.sh
#!/bin/sh
test_path="/Users/st/Desktop/rails3book-121108/chapter1/README.rdoc"
string_filename=${test_path##*/}
string_filename_without_extension=${string_filename%.*}
string_path=${test_path%/*}
string_extension=${test_path##*.}
echo ${string_filename}
echo ${string_filename_without_extension}
echo ${string_path}
echo ${string_extension}
実行結果
stimac:~ st$ sh test1.sh
README.rdoc
README
/Users/st/Desktop/rails3book-121108/chapter1
rdoc
stimac:chapter1 st$
こちらが正当なのだろう。
正規表現を覚えるか?
暗黙の使い方を覚えられれば苦労はないな。
登録:
コメントの投稿 (Atom)
0 件のコメント:
コメントを投稿