|
Answer» Hi. I searched the forum and found some anwers that allmost helped me, but not enough, because of my missing programming skillz. So i hope that some of u guys&girls can help me out.
I need to convert this txt file containing this
Wind forecast from NOAA RAP Lat Lon Time 45.322-75.8882000Z QNH hPaQFE hPa 1017.51002.2 Alt DirSpdTemp ftAGL degktsdegC 0102 4 8 1000134 9 7 2000213 23 12 3000241 28 13 4000254 30 12 5000263 33 12 6000264 37 12 7000260 40 11 8000256 43 8 9000253 46 6 10000252 47 4 11000253 48 2
into a CSV looking like this
;Altitude(feet), Direction(degrees), Speed(knots) 0,120,4 1000,134,09 2000,213,23 3000,241,28 4000,254,30 5000,263,33 6000,264,37 7000,260,40 8000,256,43 9000,253,46 10000,252,47 11000,252,48
So the top 7 lines are replaced with 1 line. And the last colum showing temperature is REMOVED. (if possible)
Can any1 help solving this, in a batch file ?Your wanted output doesn't match the input values, but, however...
Process.bat:
@echo off echo ;Altitude(feet), Direction(degrees), Speed(knots) > output.csv for /f "skip=7 tokens=1,2,3" %%A in (input.txt) do echo %%A,%%B,%%C >> output.csv
Example of usage, showing input & output files:
C:\Batch>type input.txt Wind forecast from NOAA RAP Lat Lon Time 45.322 -75.888 2000Z QNH hPa QFE hPa 1017.5 1002.2 Alt Dir Spd Temp ftAGL deg kts degC 0 102 4 8 1000 134 9 7 2000 213 23 12 3000 241 28 13 4000 254 30 12 5000 263 33 12 6000 264 37 12 7000 260 40 11 8000 256 43 8 9000 253 46 6 10000 252 47 4 11000 253 48 2
C:\Batch>process.bat C:\Batch>type output.csv ;Altitude(feet), Direction(degrees), Speed(knots) 0,102,4 1000,134,9 2000,213,23 3000,241,28 4000,254,30 5000,263,33 6000,264,37 7000,260,40 8000,256,43 9000,253,46 10000,252,47 11000,253,48 What!! I was making way longer scritps to make this happen and you come up with something as short and beautiful as this.
Tx man! Works like a charm.You can get it into just 1 line:
Batch script line:
@echo ;Altitude(feet), Direction(degrees), Speed(knots) > output.csv & for /f "skip=7 tokens=1,2,3" %%A in (input.txt) do @echo %%A,%%B,%%C >> output.csv
If you wanted to paste it into a console window, change all of the double percents to single percents:
Console command:
@echo ;Altitude(feet), Direction(degrees), Speed(knots) > output.csv & for /f "skip=7 tokens=1,2,3" %A in (input.txt) do @echo %A,%B,%C >> output.csv
The batch script as written is a bit limited; it only works with an input file called "input.txt" and it only produces an output file called "output.txt". This version requires an input file name as a parameter and emits an output file with the input name with a .csv extension, like so NOA1172xvy.txt ---> NOA1172xvy.csv
Process3.bat:
@echo ;Altitude(feet), Direction(degrees), Speed(knots) > "%~n1.csv" & for /f "skip=7 tokens=1,2,3" %%A in ('type "%~1"') do @echo %%A,%%B,%%C >> "%~n1.csv"
Works with spaces in input filename if you use quotes
C:\Batch>type "ABC NOA1172xvy.txt" Wind forecast from NOAA RAP Lat Lon Time 45.322 -75.888 2000Z QNH hPa QFE hPa 1017.5 1002.2 Alt Dir Spd Temp ftAGL deg kts degC 0 102 4 8 1000 134 9 7 2000 213 23 12 3000 241 28 13 4000 254 30 12 5000 263 33 12 6000 264 37 12 7000 260 40 11 8000 256 43 8 9000 253 46 6 10000 252 47 4 11000 253 48 2
C:\Batch\>process3.bat "ABC NOA1172xvy.txt" C:\Batch\>type "ABC NOA1172xvy.csv" ;Altitude(feet), Direction(degrees), Speed(knots) 0,102,4 1000,134,9 2000,213,23 3000,241,28 4000,254,30 5000,263,33 6000,264,37 7000,260,40 8000,256,43 9000,253,46 10000,252,47 11000,253,48 hahau read my mind - tx. I was just playing AROUND with it to do exatly that. Hm becausefile name is
winds_45.28,-75.91_1200Z.txt
it will only put in ;Altitude(feet), Direction(degrees), Speed(knots)
into the file. If I rename the file it works.. Whats the issue?Use quotes for filenames with spaces or other troublesome characters like dots or COMMAS. In fact you can always use quotes to be safe.
No quotes around input file name:
C:\Batch>process3.bat winds_45.28,-75.91_1200Z.txt The system cannot find the file specified.
A file called "winds45.csv" (shortened name!) was created with just the first line, like you showed.
Using quotes around input file name:
C:\Batch\>process3.bat "winds_45.28,-75.91_1200Z.txt" C:\Batch>
This time, the batch completed. Show output file:
C:\Batch>type "winds_45.28,-75.91_1200Z.csv" ;Altitude(feet), Direction(degrees), Speed(knots) 0,102,4 1000,134,9 2000,213,23 3000,241,28 4000,254,30 5000,263,33 6000,264,37 7000,260,40 8000,256,43 9000,253,46 10000,252,47 11000,253,48
Quote from: Salmon Trout on October 09, 2018, 09:39:26 AM A file called "winds45.csv" (shortened name!) was created with just the first line, like you showed. In fact, the one-line file with the shortened name was called winds_45.csv with an UNDERSCORE like you'd EXPECT.
|