我有 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 个目录。
答案1
$ 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
答案2
awk '{gsub(/^ /,"",$0)}{gsub(" ",",",$0)} NR==1{gsub(/"/,"",$0)}1' filename
经测试,命令运行良好
答案3
该脚本带有sed
:
for file in ./*.???.txt; do
sed -i '' 's/^ //; 1s/^"//; 1s/"$//; s/ /,/g' $file
dir=${file%.txt}
dir=${dir##*.}
mkdir -p "./$dir" &&
mv -iv "$file" "./$dir"
done