Hi All,
We have a file in the following format:
0.010000 $ ITI 11 LV2 $ 40456211 $
0.135000 $ ITI 11 LV1 $ 40512211 $
1.215600 $ ITI 11 ITI3 $ 41406211 $
24/05/2014 14:05:02
0.030000 $ ITI 11 LV2 $ 40456211 $
0.115000 $ ITI 11 LV1 $ 40512211 $
1.265600 $ ITI 11 ITI3 $ 41406211 $
24/05/2014 14:10:02
0.000000 $ ITI 11 LV2 $ 40456211 $
0.145000 $ ITI 11 LV1 $ 40512211 $
1.215600 $ ITI 11 ITI3 $ 41406211 $
24/05/2014 14:15:02
We want to read the time given in some rows and prepend them as first column in each row preceding it. We will also remove empty/blank rows. So it will look something like this.
24/05/2014 14:05:02 $ 0.010000 $ ITI 11 LV2 $ 40456211 $
24/05/2014 14:05:02 $ 0.135000 $ ITI 11 LV1 $ 40512211 $
24/05/2014 14:05:02 $ 1.215600 $ ITI 11 ITI3 $ 41406211 $
24/05/2014 14:10:02 $ 0.030000 $ ITI 11 LV2 $ 40456211 $
24/05/2014 14:10:02 $ 0.115000 $ ITI 11 LV1 $ 40512211 $
24/05/2014 14:10:02 $ 1.265600 $ ITI 11 ITI3 $ 41406211 $
24/05/2014 14:15:02 $ 0.000000 $ ITI 11 LV2 $ 40456211 $
24/05/2014 14:15:02 $ 0.145000 $ ITI 11 LV1 $ 40512211 $
24/05/2014 14:15:02 $ 1.215600 $ ITI 11 ITI3 $ 41406211 $
Thanks for your help.
$ awk 'NF==2{$1=$1;gsub(/\*/,$0 c,s);print s;s="";next}NF{$1=$1;s = s ? s ORS "*"$0 : "*"$0 }' c=' $ ' file
24/05/2014 14:05:02 $ 0.010000 $ ITI 11 LV2 $ 40456211 $
24/05/2014 14:05:02 $ 0.135000 $ ITI 11 LV1 $ 40512211 $
24/05/2014 14:05:02 $ 1.215600 $ ITI 11 ITI3 $ 41406211 $
24/05/2014 14:10:02 $ 0.030000 $ ITI 11 LV2 $ 40456211 $
24/05/2014 14:10:02 $ 0.115000 $ ITI 11 LV1 $ 40512211 $
24/05/2014 14:10:02 $ 1.265600 $ ITI 11 ITI3 $ 41406211 $
24/05/2014 14:15:02 $ 0.000000 $ ITI 11 LV2 $ 40456211 $
24/05/2014 14:15:02 $ 0.145000 $ ITI 11 LV1 $ 40512211 $
24/05/2014 14:15:02 $ 1.215600 $ ITI 11 ITI3 $ 41406211 $
This keeps your original formatting
$ awk 'NF==2{gsub(/\*/,$0 c,s);print s;s="";next}NF{s = s ? s ORS "*"$0 : "*"$0 }' c=' $ ' file
---------- Post updated at 05:45 PM ---------- Previous update was at 05:40 PM ----------
OR
$ awk 'NF==2{d=$0;next}NF{print d,"$",$0}' <(tac file) | tac
protocomm:
could you explain that:
gsub(/\*/,$0 c,s)
who's "s" and "c" ???
Hi protocomm,
Variable s
saves every line $0
where NF
is greater than 0 NF{..
, with ORS
,so blank line are skipped here.
I used *
, inorder to replace it with coming date and time where number of fields equal to 2 NF==2
.
Once number of fields NF
equal to 2, substitute $0
along with variable c for all *
in variable s
. Global subsitution using gsub
function.
Just see difference when variable c
is set to ' $ '
$ awk 'NF==2{$1=$1;gsub(/\*/,$0 c,s);print s;s="";next}NF{$1=$1;s = s ? s ORS "*"$0 : "*"$0 }' c=' $ ' file
24/05/2014 14:05:02 $ 0.010000 $ ITI 11 LV2 $ 40456211 $
24/05/2014 14:05:02 $ 0.135000 $ ITI 11 LV1 $ 40512211 $
....
Here variable c is empty
$ awk 'NF==2{$1=$1;gsub(/\*/,$0 c,s);print s;s="";next}NF{$1=$1;s = s ? s ORS "*"$0 : "*"$0 }' c='' file
24/05/2014 14:05:020.010000 $ ITI 11 LV2 $ 40456211 $
24/05/2014 14:05:020.135000 $ ITI 11 LV1 $ 40512211 $
....
2 Likes