I'm looking for a way to add a character at beginning and at the end of each line of a txt file. The file contains a list of http addresses and I'd like to add the character " without any space, at the beginning and at the end of each address.
I'm looking for a way to add a character at beginning and at the end of each line of a txt file. The file contains a list of http addresses and I'd like to add the character " without any space, at the beginning and at the end of each address.
Using
sed
:^
denotes the starting of line and$
ending of line.If you want to overwrite the file, use
-i
option.If python is not an option, you can use
sed
:Brief explaination:
-i
: in-place (edit files in place )s/^\(.*\)$/"\1"/
: this replaces all the content that matches the regular expression delimited by the first two slashes (in this case^\(.*\)$
matches each line and saves the content of the line in the\1
group, which will be used during the replacement) with the corresponding one between the last two slashes (in this case"\1"
adds the quotes at the beginning and at the end of the group saved before).You could use python for it.
Just copy paste this code in a file called
test.py
and run it usingpython3
Edit the first and fourthlast line with appropriate path to the txt file you want to edit.