Shell-Script
如何編輯一組文件並保存在名稱相關的文件夾中
我有 54 個文件,其名稱由名稱“frames.XYZ.txt”和“index.XYZ.txt”組成。
我想編輯所有文件
- 刪除第一列,這是一個空格字元。
- 在第一行中,刪除第一個字元,它是一個引號字元,最後一個字元,它也是一個引號字元。
- 用逗號替換空格分隔空格。
例如,
名為“frames.SGM.txt”的文件的第一行
"tagsread pwread pwcol pwmin pwmax" 10 0.32748740570260465 0.2833372980968762 0.3021673043683319 0.3484962315474369 6 0.26616139593784266 0.28492773667197896 0.2269210356692535 0.30782630598198407 3 0.29228070334103584 0 0.26073331696489527 0.30912215637906254 1 0.3473179212993043 0 0.3473179212993043 0.3473179212993043 11 0.21031628861286614 0.17075257758590717 0.06358843520605208 0.39186036744185854
和文件“index.SGM.txt”:
"frameini framelst boxnum boxoriginal L W H read tags tagsread readtime despX despY theta vhr ha hb d" 0 2 2 2 0.762 0.508 0.508 1 19 19 0.054049999999999994 -0.024118623718731715 -0.08549821547541447 -0.517646968345433 1 3 1 3 3 3 0 0 0.229 0.152 0.152 1 1 1 0.005980000000000001 0.02078112515691111 0.011339458730730879 -0.008791631073522673 1 3 1 3 4 10 5 5 1.17 0.77 0.66 1 47 47 0.13178999999999996 0.24160268945952468 -0.17031344019338376 0.34959314590893964 0 3 1 3 11 12 1 1 0.457 0.457 0.457 1 11 11 0.03328999999999999 0.07247402538161593 0.01825733790023088 0.023974909088602784 1 3 1 3 13 25 6 6 0.915 0.915 0.915 1 87 87 0.23731999999999998 0.1611914340194872 -0.008643456696731612 -0.2777302231461403 1 3 1 3
請注意,在這兩個文件中,每行的第一個字元是一個空格,並且要刪除它
而且,我想讓其餘的編輯結果:
tagsread,pwread,pwcol,pwmin,pwmax 10,0.32748740570260465,0.2833372980968762,0.3021673043683319,0.3484962315474369 6,0.26616139593784266,0.28492773667197896,0.2269210356692535,0.30782630598198407 3,0.29228070334103584,0,0.26073331696489527,0.30912215637906254 1,0.3473179212993043,0,0.3473179212993043,0.3473179212993043 11,0.21031628861286614,0.17075257758590717,0.06358843520605208,0.39186036744185854
和
frameini,framelst,boxnum,boxoriginal,L,W,H,read,tags,tagsread,readtime,despX,despY,theta,vhr,ha,hb,d 0,2,2,2,0.762,0.508,0.508,1,19,19,0.054049999999999994,-0.024118623718731715,-0.08549821547541447,-0.517646968345433,1,3,1,3 3,3,0,0,0.229,0.152,0.152,1,1,1,0.005980000000000001,0.02078112515691111,0.011339458730730879,-0.008791631073522673,1,3,1,3 4,10,5,5,1.17,0.77,0.66,1,47,47,0.13178999999999996,0.24160268945952468,-0.17031344019338376,0.34959314590893964,0,3,1,3 11,12,1,1,0.457,0.457,0.457,1,11,11,0.03328999999999999,0.07247402538161593,0.01825733790023088,0.023974909088602784,1,3,1,3 13,25,6,6,0.915,0.915,0.915,1,87,87,0.23731999999999998,0.1611914340194872,-0.008643456696731612,-0.2777302231461403,1,3,1,3
此外,我想將它們保存在文件夾名稱為“XYZ”的不同文件夾中。比如上面兩個編輯好的文件“frames.SGM.txt”和“index.SGM.txt”應該放到\SGM目錄下。
我有 27 個 XYZ 字元串變體,所以它應該創建這 27 個目錄。
$ awk -v OFS=, '{gsub(/"/,""); $1=$1}1' frames.SGM.txt tagsread,pwread,pwcol,pwmin,pwmax 10,0.32748740570260465,0.2833372980968762,0.3021673043683319,0.3484962315474369 6,0.26616139593784266,0.28492773667197896,0.2269210356692535,0.30782630598198407 3,0.29228070334103584,0,0.26073331696489527,0.30912215637906254 1,0.3473179212993043,0,0.3473179212993043,0.3473179212993043 11,0.21031628861286614,0.17075257758590717,0.06358843520605208,0.39186036744185854
並根據文件名將輸出放在
/foo/bar
命名下的不同目錄中:for file in *.txt; do dir="${file%.*}" dir="/foo/bar/${dir##*.}" mkdir -p "$dir" awk -v OFS=, '{gsub(/"/,""); $1=$1}1' "$file" > "$dir/$file" done