My Linux file contains of over 4000 lines of code similar to this:
[INSERT INTO DATAMART_QUEUE_DETAILS(EVENT_TIME,QUEUE,EVENT,TRACKNUM,QUEUE_TYPE,ASSOCIATED_ROUTING_SCRIPT,ANI,DNIS,CHANNEL_TYPE) VALUES ('2021.01.01 15:31:57','Electrolux', 'abandoned', '1609507904.86839', 'ROUTING_SCRIPT', 'Electrolux', '01008367900', '886123', 'CALL')]
[INSERT INTO DATAMART_QUEUE_DETAILS(EVENT_TIME,QUEUE,EVENT,TRACKNUM,QUEUE_TYPE,ASSOCIATED_ROUTING_SCRIPT,ANI,DNIS,CHANNEL_TYPE) VALUES ('2021.01.01 15:31:59','Electrolux-Inst', 'queued', '1609507878.86832', 'VIRTUAL', 'Electrolux', '01552050703', '886123', 'CALL')]
[INSERT INTO DATAMART_QUEUE_DETAILS(EVENT_TIME,QUEUE,EVENT,TRACKNUM,QUEUE_TYPE,ASSOCIATED_ROUTING_SCRIPT,ANI,DNIS,CHANNEL_TYPE) VALUES ('2021.01.01 15:31:59','Electrolux', 'distributed', '1609507878.86832', 'ROUTING_SCRIPT', 'Electrolux', '01552050703', '886123', 'CALL')]
[INSERT INTO DATAMART_QUEUE_DETAILS(EVENT_TIME,QUEUE,EVENT,TRACKNUM,QUEUE_TYPE,ASSOCIATED_ROUTING_SCRIPT,ANI,DNIS,CHANNEL_TYPE) VALUES ('2021.01.01 15:31:59','Electrolux-Inst', 'distributed', '1609507878.86832', 'VIRTUAL', 'Electrolux', '01552050703', '886123', 'CALL')]
I need linux command(s) to delete the square brackets [ ] from the file so that it looks something like this:
INSERT INTO DATAMART_QUEUE_DETAILS(EVENT_TIME,QUEUE,EVENT,TRACKNUM,QUEUE_TYPE,ASSOCIATED_ROUTING_SCRIPT,ANI,DNIS,CHANNEL_TYPE) VALUES ('2021.01.01 15:31:57','Electrolux', 'abandoned', '1609507904.86839', 'ROUTING_SCRIPT', 'Electrolux', '01008367900', '886123', 'CALL')
INSERT INTO DATAMART_QUEUE_DETAILS(EVENT_TIME,QUEUE,EVENT,TRACKNUM,QUEUE_TYPE,ASSOCIATED_ROUTING_SCRIPT,ANI,DNIS,CHANNEL_TYPE) VALUES ('2021.01.01 15:31:59','Electrolux-Inst', 'queued', '1609507878.86832', 'VIRTUAL', 'Electrolux', '01552050703', '886123', 'CALL')
INSERT INTO DATAMART_QUEUE_DETAILS(EVENT_TIME,QUEUE,EVENT,TRACKNUM,QUEUE_TYPE,ASSOCIATED_ROUTING_SCRIPT,ANI,DNIS,CHANNEL_TYPE) VALUES ('2021.01.01 15:31:59','Electrolux', 'distributed', '1609507878.86832', 'ROUTING_SCRIPT', 'Electrolux', '01552050703', '886123', 'CALL')
INSERT INTO DATAMART_QUEUE_DETAILS(EVENT_TIME,QUEUE,EVENT,TRACKNUM,QUEUE_TYPE,ASSOCIATED_ROUTING_SCRIPT,ANI,DNIS,CHANNEL_TYPE) VALUES ('2021.01.01 15:31:59','Electrolux-Inst', 'distributed', '1609507878.86832', 'VIRTUAL', 'Electrolux', '01552050703', '886123', 'CALL')
Finally, I want to insert these lines directly to MySQL query.
2
Answers
A cheap and cheerful way is
Edit: I should add that this won’t have the desired result if [ or ] occurs in the data.
Even though this question already has an answer, I’d like to share my own solution here:
cat infile | sed 's/^[(.*)]$/1/' > outfile
Explanation:
s
: substitute commands/regexp/replacement/flags
^
: start of string[
: The literal character[
(.*)
: A group that captures everything]
: The literal character]
$
: end of string1
: text in the first capturing group ((.*)
)EDIT: in fact, this solution works even if there are ‘[‘ or ‘]’ inside the input