'how to add " to end of specific lines in linux
I have a file as below
"time":"2022-03-01T12:00:25.388Z","duration":0.8255,"error":0
"time":"2022-03-01T12:00:26.809Z","duration":0.29521,"error":0,"state":"pending
"time":"2022-03-01T12:00:29.053Z","duration":2.90688,"error":0,"options":"merge_request.create
"time":"2022-03-01T12:00:00.635Z","duration":0.46049,"error":0
"time":"2022-03-01T12:00:39.351Z","duration":0.76487,"error":0,"state":"failed
I want to add " at the end but only to lines ending as a string
I gave sed -i 's/$/",/' filename but it adds quotes to end of all the lines.
Desired output is
"time":"2022-03-01T12:00:25.388Z","duration":0.8255,"error":0
"time":"2022-03-01T12:00:26.809Z","duration":0.29521,"error":0,"state":"pending"
"time":"2022-301T12:00:29.053Z","duration":2.90688,"error":0,"push_options":"merge_request.create"
"time":"2022-03-01T12:00:00.635Z","duration":0.46049,"error":0
"time":"2022-03-01T12:00:39.351Z","duration":0.76487,"error":0,"state":"failed"
Is there a way to achieve this?
Solution 1:[1]
Suggesting an awk solution:
awk '/[[:alpha:]]$/{$0=$0"\""}1' input.txt
Solution 2:[2]
Using sed
$ sed 's/[a-z]$/&"/' input_file
"time":"2022-03-01T12:00:25.388Z","duration":0.8255,"error":0
"time":"2022-03-01T12:00:26.809Z","duration":0.29521,"error":0,"state":"pending"
"time":"2022-03-01T12:00:29.053Z","duration":2.90688,"error":0,"options":"merge_request.create"
"time":"2022-03-01T12:00:00.635Z","duration":0.46049,"error":0
"time":"2022-03-01T12:00:39.351Z","duration":0.76487,"error":0,"state":"failed"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dudi Boy |
| Solution 2 | HatLess |
