我有一堆文件,在方括号中有其他信息。我想通过删除该部分来重命名它们。
例如:
function getCoordinate() {
// Distict SQL Query for first loop
(SELECT DISTINCT(id_shipment) as id_shipment FROM tbl_tracking)
$id_shipment = $this->Maps_model->getIdShipment();
// First Loop
foreach ($id_shipment as $value) {
// The object i want to loop for views
$json = array(
'type' => 'Feature',
'properties' => json_decode('{}'),
'geometry' => array(
'type' => 'LineString',
'coordinates' => array()
));
//Query for getting coordinate
(SELECT a.latitude,a.latitude,b.ship_name from tbl_tracking a
LEFT JOIN tbl_shipment b on a.id_shipment = b.id
WHERE a.id_shipment = $value['id_shipment']);
$coordinate = $this->Maps_model->getCoordinate($value['id_shipment']);
//Second Loop inside The First Loop
foreach ($coordinate as $key) {
$json['geometry']['coordinates'] = array (
(float)$key['latitude'],
(float)$key['longitude']
);
}
echo json_encode($json);
}
}
想重命名为:
filename [additional info 1].ext1
anotherfile [different info 2].ext2
我尝试使用正则表达式(并使用重命名命令),但没有成功。有人知道一种只包含“ [....]”部分(包括前导空格)的重命名命令条的方法。
答案 0 :(得分:-1)
为每个文件循环并使用sed进行一些简单的正则表达式匹配:
for i in *.ext*; do
mv "$i" "$(sed 's/ \[[^]]*\]\././' <<<"$i")"
done
经过测试:
$ touch 'filename [additional info 1].ext1' 'filename [additional info 2].ext2'
$ for i in *.ext*; do mv -v "$i" "$(sed 's/ \[[^]]*\]\././' <<<"$i")"; done
renamed 'filename [additional info 1].ext1' -> 'filename.ext1'
renamed 'filename [additional info 2].ext2' -> 'filename.ext2'