เมื่อฉันใช้find
มักจะพบผลลัพธ์หลายรายการเช่น
find -name pom.xml
./projectA/pom.xml
./projectB/pom.xml
./projectC/pom.xml
ฉันมักจะต้องการเลือกเฉพาะผลลัพธ์ที่เฉพาะเจาะจง (เช่นedit ./projectB/pom.xml
) มีวิธีในการแจกแจงfind
เอาต์พุตและเลือกไฟล์ที่จะส่งผ่านไปยังแอปพลิเคชันอื่นหรือไม่? ชอบ:
find <print line nums?> -name pom.xml
1 ./projectA/pom.xml
2 ./projectB/pom.xml
3 ./projectC/pom.xml
!! | <get 2nd entry> | xargs myEditor
?
[แก้ไข] ฉันได้ชนกับเพอร์คิวลีอาร์ดบางตัวด้วยวิธีแก้ปัญหาบางอย่างที่กล่าวถึง ดังนั้นฉันต้องการอธิบายขั้นตอนในการทำซ้ำ:
git clone http://git.eclipse.org/gitroot/platform/eclipse.platform.swt.git
cd eclipse.platform.swt.git
<now try looking for 'pom.xml' and 'feature.xml' files>
[แก้ไข] วิธีแก้ไข 1 จนถึงตอนนี้การรวมกันของ 'nl' (เอาท์พุท enumirate), ส่วนหัวและหางดูเหมือนว่าจะทำงานถ้าฉันรวมพวกมันเข้ากับฟังก์ชั่นและใช้ $ (!!)
เช่น:
find -name pom.xml | nl #look for files, enumirate output.
#I then define a function called "nls"
nls () {
head -n $1 | tail -n 1
}
# I then type: (suppose I want to select item #2)
<my command> $(!!s 2)
# I press enter, it expands like: (suppose my command is vim)
vim $(find -name pom.xml |nls 2)
# bang, file #2 opens in vim and Bob's your uncle.
[แก้ไข] โซลูชันที่ 2 การใช้ "เลือก" ดูเหมือนว่าจะทำงานได้ดีเช่นกัน อดีต:
findexec () {
# Usage: findexec <cmd> <name/pattern>
# ex: findexec vim pom.xml
IFS=$'\n';
select file in $(find -type f -name "$2"); do
#$EDITOR "$file"
"$1" "$file"
break
done;
unset IFS
}
Your find command | head -TheNumberYouWant
ตอบสนองความต้องการของคุณหรือไม่ (ด้วยสายของคุณ:!! | head -2 | xargs myEditor
)