convert file with multiple lines into array
Question:
How can I convert a file like:
ap-southeast-1
ap-southeast-2
ca-central-1
into:
"ap-southeast-1","ap-southeast-2","ca-central-1"
What I tried so far:
sed -z 's/n/,/g;s/,$/n/' filename
Above gives:
ap-southeast-1,ap-southeast-2,ca-central-1
Tries xls transpose function but that also no luck: https://support.microsoft.com/en-us/office/transpose-function-ed039415-ed8a-4a81-93e9-4b6dfac76027
Answers:
Extending OP’s current sed
:
$ sed -z 's/n/,/g;s/^/"/;s/,$/"n/;s/,/","/g' filename
"ap-southeast-1","ap-southeast-2","ca-central-1"
One awk
idea:
$ awk '{out=out sep $0; sep="",""} END {print """ out """}' filename
"ap-southeast-1","ap-southeast-2","ca-central-1"
data = []
with open('original-file.txt', 'r') as o:
for line in o:
data.append(f'"{line.strip()}"')
with open('new-file.txt', 'w') as n:
n.write(','.join(data))
$ cat original-file.txt
example-country-1
example-country-2
example-country-3
$ python3 script.py && cat new-file.txt
"example-country-1","example-country-2","example-country-3"
You included the python tag so I’ll answer with python code.
Try this:
new = []
with open("/path/to/your/file") as f:
for line in f:
new.append(f"{line.strip()}")
with open("/path/to/file/to/write", "w") as f: #this can be the same file, but be sure you have write access
f.write('"' + '","'.join(new) + '"')
This can be written better but it works for a one-off script.
Edit – welp by the time I’ve written my answer someone already answered with a similar answer.
Using sed
$ sed -Ez 's/([[:alnum:]-]*)n/"1",/g;s/,$/n/' input_file
"ap-southeast-1","ap-southeast-2","ca-central-1"
How can I convert a file like:
ap-southeast-1
ap-southeast-2
ca-central-1
into:
"ap-southeast-1","ap-southeast-2","ca-central-1"
What I tried so far:
sed -z 's/n/,/g;s/,$/n/' filename
Above gives:
ap-southeast-1,ap-southeast-2,ca-central-1
Tries xls transpose function but that also no luck: https://support.microsoft.com/en-us/office/transpose-function-ed039415-ed8a-4a81-93e9-4b6dfac76027
Extending OP’s current sed
:
$ sed -z 's/n/,/g;s/^/"/;s/,$/"n/;s/,/","/g' filename
"ap-southeast-1","ap-southeast-2","ca-central-1"
One awk
idea:
$ awk '{out=out sep $0; sep="",""} END {print """ out """}' filename
"ap-southeast-1","ap-southeast-2","ca-central-1"
data = []
with open('original-file.txt', 'r') as o:
for line in o:
data.append(f'"{line.strip()}"')
with open('new-file.txt', 'w') as n:
n.write(','.join(data))
$ cat original-file.txt
example-country-1
example-country-2
example-country-3
$ python3 script.py && cat new-file.txt
"example-country-1","example-country-2","example-country-3"
You included the python tag so I’ll answer with python code.
Try this:
new = []
with open("/path/to/your/file") as f:
for line in f:
new.append(f"{line.strip()}")
with open("/path/to/file/to/write", "w") as f: #this can be the same file, but be sure you have write access
f.write('"' + '","'.join(new) + '"')
This can be written better but it works for a one-off script.
Edit – welp by the time I’ve written my answer someone already answered with a similar answer.
Using sed
$ sed -Ez 's/([[:alnum:]-]*)n/"1",/g;s/,$/n/' input_file
"ap-southeast-1","ap-southeast-2","ca-central-1"