0

I have 54 files whose names are formed with names "frames.XYZ.txt" and "index.XYZ.txt".

I would like to edit all the files by

  1. Removing the first column, which is a space char.
  2. In the first line, remove the first char which is a quote char, and the last char, which is also a quote char.
  3. Replacing the space separation spaces by commas.

For example,

first lines of the file named "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

and of the file "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

Note that in the two files the first character of every line is a space and want to remove it

And, I would like to make the rest of edits resulting:

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

and

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

Besides, I would like to save them in different folders with the folder name "XYZ". For example, the above two edited files "frames.SGM.txt" and "index.SGM.txt" should go to a \SGM directory.

I have 27 XYZ string variations so it should create those 27 directories.

0

3 Answers 3

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

and to put the output in a different directory under /foo/bar named based on the file name:

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
1
awk '{gsub(/^ /,"",$0)}{gsub(" ",",",$0)} NR==1{gsub(/"/,"",$0)}1' filename

Tested and command worked fine

0
0

This script with 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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .