我還是個新手,需要一些幫助。
- 我有一個檔案
stapel_old。 - 在同一目錄下,我還有
english_old_sync、math_old_sync和vocabulary_old_sync等檔案。
stapel_old的內容是:
english
數學
詞匯
例如英語的內容是:
basic_grammar.md
拼寫法.md
正字法.md
我想操作stapel_old中給出的所有檔案,就像在這個例子中一樣:
我想操作stapel_old中的所有檔案。
- 獲取
stapel_old'english'的第一行,(之后是math,以此類推) - 在這種情況下,將english轉換為english_old_sync, (或者之后是第二行中給出的內容,例如,math轉換為math_old_sync)
- 在
english_old_sync中逐行搜索".md"模式 。
- 并且在每一行的.md后面附加上
::#a1。
結果應該是,例如english_old_sync:
basic_grammar.md::#a1
spelling.md:::#a1。
orthography.md:::#a1
的math_old_sync:
geometry.md::#a1
fractions.md:::#a1。
以此類推。stapel_old應該保持不變。
我怎樣才能實作這一點? 我試著用sed -n, while loop (while read -r line), 而且我覺得這在某種程度上是正確的方法 - 但是我在檢查和閱讀了4個小時之后仍然得到錯誤,而且不是預期的結果。 謝謝你!
編輯
下面是作業代碼(檔案存盤在'olddata'檔案夾中):
clear
echo -e "$(tput setaf 1)$(tput setab 7)學習目錄:$(tput sgr 0)
"
# 把不應該成為閃存卡的目錄放在這里,命令。| grep -v 'name_of_directory_which_not_to_learn1' | grep -v 'directory2'
ls .../ | grep -v 00_gliederungsverweise | grep -v 0_weiter | grep -v bibliothek | grep -v notizen | grep -v Obsidian | grep -v z_nicht_uni | tee olddata/stapel_old
# count folders
echo -ne "
有多少不同的檔案夾。" && wc -l olddata/stapel_old | cut -d' ' -f1 | tee -a olddata/stapel_old
echo -e "這個學習目錄是否正確?[j ODER y]-->是;[其他]-->否
"
閱讀 lernvz_korrekt
if [ "$lernvz_korrekt" = j ] 。|| ["$lernvz_korrekt" = y ]。
then
讀 -n 1 -s -r -p "學習目錄正確。按任何鍵繼續..."
else。
讀 -n 1 -s -r -p "學習目錄不正確,請在第4行進行修改。按任何鍵繼續..."
退出。
fi
echo -e "
_____________________________
$(tput setaf 6)$(tput setab 5)發現卡片:$(tput sgr 0)$(tput setaf 6)
"
#GET & & WRITE FOLDER NAMES into olddata/stapel_old
anzahl_zeilen=$(cat olddata/stapel_old |& tail -1)
#GET NAMES of .md files of every stapel and write All to 'stapelname' _old_sync
i=0
name="var_$i"
for ( ( num=1; num <= $anzahl_zeilen; num ) )
do
i="$((i 1)/span>)"
name="var_$i"
name=$(cat olddata/stapel_old | sed -n "$num"/span>p)
找到 ../$name/ -name '*. md' | grep -v trash | grep -v Obsidian | rev | cut -d'/' -f1 | rev | tee olddata/$name"_old_sync" /span>
done
(tput sgr 0)
我嘗試添加:
input="olddata/stapel_old"/span>
while IFS= read -r line
do -r line
sed -n "$line"/span>p olddata/stapel_old
done < "$input"。
只改變english_old_sync的代碼是:
lines=$(wc -l olddata/english_old_sync | cut -d' -f1)
for ((num=1; num <= $lines; num )
do
content=$(sed -n "$num"/span>p olddata/english_old_sync)
sed -i "s/"$content"/""$content": ::#a1/g"" olddata/english_old_sync。
done
因此,現在,這需要一個內部for-loop,一個外部for-loop,保持english的變數,對嗎?
uj5u.com熱心網友回復:
stapel_old應該保持不變。
你可以嘗試一個while read回圈,并在回圈內嵌入sed。
#!/usr/bin/env bash
while IFS= read -r files; do
echo cp -v "$files" "${files}_old_sync" & &
echo sed '/^.*.md$/s/$/::#a1/' "{span class="hljs-variable">${files}_old_sync"
done < olddata/staple_old
在這種情況下,將english轉換為english_old_sync,(或者在這之后將第二行中給出的內容轉換,例如將math轉換為math_old_sync)
。
cp用一個新的名字復制檔案,如果目標是從檔案的內容中重命名原檔案名staple_old,那么將cp改為mvn和-i標志從sed被省略,包括它,如果需要。該腳本還假定在
staple_old檔案的內容中沒有空行/空白行。如果有的話,請在do所在的行后增加一個測驗。
|| continue[[ [-n $files ]] || continue它還假設
staple_old的內容是現有的檔案。以防萬一,添加一個額外的測驗。[[-e $files]]。|| { printf >&2 '%s no such file or directory. ' "$files"; continue; }
或者一個if陳述句。
if [[ ! -e $files ]]; then
printf >& 2 '%s no such file or directory
' "$files"
繼續
fi
參見
。help test另見
help continue
將它們組合在一起應該是這樣的:
while IFS= read -r files; do
[[ -n $files ]] 。||continue
[[-e $files ]] || {
printf >&2 '%s no such file or directory.
' "$files"。
繼續。
}
echo cp -v "$files" "${files}_old_sync" & &
echo sed '/^.*.md$/s/$/::#a1/' "{span class="hljs-variable">${files}_old_sync"
done < olddata/staple_old
- 移除
echo's 如果你對輸出感到滿意,那么腳本可以復制/重命名和編輯檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/309919.html
標籤:
上一篇:為什么從Makefile中呼叫printf時會有不同的表現?
下一篇:從一個介面到另一個介面的地圖
