Reputation: 315
I am using openvms but have access to versions of aWk and /or sed on this platform. Wondered if anyone can help with a text file processing job.
My file looks like
START-OF-DATA
Stock ID|XYZ
START-TIME 11:30
END_TIME 12:30
11:31|BID|12.5|ASK|12.7
11:34|BID|12.6|ASK|12.7
END-OF-DATA
START-OF-DATA
Stock ID|ABC
START-TIME 11:30
END_TIME 12:30
11:40|BID|.245|ASK|.248
11:34|BID|.246|ASK|.249
END-OF-DATA
Basically I want to pre-pend the BID/ASK
data records with the Stock ID
so the above file should look like
START-OF-DATA
Stock ID|XYZ
START-TIME 11:30
END_TIME 12:30
XYZ|11:31|BID|12.5|ASK|12.7
XYZ|11:34|BID|12.6|ASK|12.7
END-OF-DATA
START-OF-DATA
Stock ID|ABC
START-TIME 11:30
END_TIME 12:30
ABC|11:40|BID|.245|ASK|.248
ABC|11:34|BID|.246|ASK|.249
END-OF-DATA
Can any one help ?
Upvotes: 1
Views: 70
Reputation: 58381
This might work for you (GNU sed):
sed -r '/Stock ID/h;/BID|ASK/{G;s/(.*)\n.*\|(.*)/\2|\1/}' file
Save the Stock ID
in the hold space and prepend it to records containing BID
or ASK
.
Upvotes: 0
Reputation: 45243
Using awk
awk '/Stock ID/{s=$2}/BID|ASK/{$0=s FS $0}1' FS=\| file
START-OF-DATA
Stock ID|XYZ
START-TIME 11:30
END_TIME 12:30
XYZ|11:31|BID|12.5|ASK|12.7
XYZ|11:34|BID|12.6|ASK|12.7
END-OF-DATA
START-OF-DATA
Stock ID|ABC
START-TIME 11:30
END_TIME 12:30
ABC|11:40|BID|.245|ASK|.248
ABC|11:34|BID|.246|ASK|.249
END-OF-DATA
Upvotes: 0
Reputation: 207425
Like this:
awk -F'|' 'BEGIN{OFS="|"} /^Stock/{S=$2} /BID|ASK/{print S,$0}' file
Explanation (with thanks to Olivier Dulac)
It updates "S" variable each time it encounters a line stating with "Stock", and then prepends S to lines CONTAINING "BID" or "ASK" (using | as a separator for reading and for outputting).
Upvotes: 2
Reputation: 195039
try this:
awk -F'|' 'NF==2{pre=$2}NF>2{$0=pre FS $0}7' file
it works for the given example.
Upvotes: 0