I have many data files that look something like this:
,8/9/2015
Timezone,-6
,
Serial No.,19000000395CCE41
Location:,LS_trap_9u
High temperature limit (�C),20.12
Low temperature limit (�C),0.05
Date - Time,Temperature (�C)
5/28/2015 6:00,20
5/28/2015 8:00,22.6
5/28/2015 10:00,27.1
5/28/2015 12:00,26.1
5/28/2015 14:00,27.1
5/28/2015 16:00,26.1
5/28/2015 18:00,24.6
5/28/2015 20:00,23.6
5/28/2015 22:00,22.6
5/29/2015 0:00,22.1
I parse these files with this script:
awk -vFS=, -vOFS=, \
'{gsub("\"","")}
FNR==4{s=$2}
FNR==5{l=$2}
FNR>8{gsub(" ",OFS);print l,s,FILENAME,$0}' \
*.csv > formatted_log.csv
printf "\nDone\n"
I want to extract the last character from the 'loc' string (in this case "u") and append it to the another column.
The final file should look something like this:
LS_trap_9c,3.6E+15,trap9c_3600000039654841_150809.csv,u,5/28/2015,5:59,20.1
LS_trap_9c,3.6E+15,trap9c_3600000039654841_150809.csv,u,5/28/2015,7:59,27.6
LS_trap_9c,3.6E+15,trap9c_3600000039654841_150809.csv,u,5/28/2015,9:59,30.1
LS_trap_9c,3.6E+15,trap9c_3600000039654841_150809.csv,u,5/28/2015,11:59,29.6
LS_trap_9c,3.6E+15,trap9c_3600000039654841_150809.csv,u,5/28/2015,13:59,29.6
LS_trap_9c,3.6E+15,trap9c_3600000039654841_150809.csv,u,5/28/2015,15:59,28.1
LS_trap_9c,3.6E+15,trap9c_3600000039654841_150809.csv,u,5/28/2015,17:59,26.1
LS_trap_9c,3.6E+15,trap9c_3600000039654841_150809.csv,u,5/28/2015,19:59,23.6
My attempt thus far looks like this:
awk -vFS=, -vOFS=, \
'{gsub("\"","")}
FNR==4{ser=$2}
FNR==5{loc=$2}
FNR>8{gsub(" ",OFS);print loc,ser,FILENAME,${loc:(-1)},$0}' \
*.csv > formatted_log.csv
I get the following errors:
awk: cmd. line:4: FNR>8{gsub(" ",OFS);print loc,ser,FILENAME,${loc:(-1)},$0}
awk: cmd. line:4: ^ syntax error
awk: cmd. line:4: FNR>8{gsub(" ",OFS);print loc,ser,FILENAME,${loc:(-1)},$0}
awk: cmd. line:4: ^ syntax error
awk: cmd. line:4: FNR>8{gsub(" ",OFS);print loc,ser,FILENAME,${loc:(-1)},$0}
awk: cmd. line:4: ^ syntax error
Changing the script to this:
awk -vFS=, -vOFS=, \
awk -vFS=, -vOFS=, \
'{gsub("\"","")}
FNR==4{ser=$2}
FNR==5{loc=$2}
my_loc="${loc:(-1)}"
FNR>8{gsub(" ",OFS);print loc,ser,FILENAME,my_loc,$0}' \
*.csv > formatted_log.CSV
printf "\nDone1\n"
awk -vFS=, -vOFS=, \
'{gsub("\"","")}
FNR==4{ser=$2}
FNR==5{loc=$2}
my_loc="${loc:(-1)}"
FNR>8{gsub(" ",OFS);print loc,ser,FILENAME,my_loc,$0}' \
*.csv > formatted_log.CSV
printf "\nDone1\n"
adds unwanted extra lines to the formattted_log.csv file. Which look something like this:
LS_trap_9c,3.6E+15,trap9c_3600000039654841_150809.csv,5/28/2015,5:59,20.1
5/28/2015 7:59,27.6
LS_trap_9c,3.6E+15,trap9c_3600000039654841_150809.csv,5/28/2015,7:59,27.6
5/28/2015 9:59,30.1
LS_trap_9c,3.6E+15,trap9c_3600000039654841_150809.csv,5/28/2015,9:59,30.1
5/28/2015 11:59,29.6
LS_trap_9c,3.6E+15,trap9c_3600000039654841_150809.csv,5/28/2015,11:59,29.6
5/28/2015 13:59,29.6
LS_trap_9c,3.6E+15,trap9c_3600000039654841_150809.csv,5/28/2015,13:59,29.6
5/28/2015 15:59,28.1
How can I extract final character of a specific string from within an awk?
'/^Location:/{code=substr($0,length($0))}...'