I have a file which has several lines.
I wish to update the file while making sure that the last visible character of each line of the file is a single quote '
. In case not, then we should add the single quote at the end of each line.
If the single quote is the last character in a line then leave it as is.
cat test.txt
fahsjhjkhkjhjhajkhjf
afasfsfsfffsfasf'
aaffa' sfff
wfafsfsaffs'
fsafsfs'afffafsasf
Expected output:
fahsjhjkhkjhjhajkhjf'
afasfsfsfffsfasf'
aaffa' sfff'
wfafsfsaffs'
fsafsfs'afffafsasf'
sed "\$s/[^']\$/&'/" file
. To do it on any line, just remove the\$
address condition. – steeldriver Mar 27 '22 at 18:26tac test.txt | sed '1,/[[:graph:]]/ { /[[:graph:]]/!d; }' | tac | sed "\$s/[^']/&'/"
and it does not work on the sample i provided. – Ashar Mar 27 '22 at 18:39tac test.txt | sed '1,/[[:graph:]]/ { /[[:graph:]]/!d; }' | tac | sed "\$s//'/"
but i get errorsed: -e expression #1, char 0: no previous regular expression
– Ashar Mar 27 '22 at 18:42tac
? Don't treat programming as a "cargo cult" but instead take some time to try and understand the solutions you're given. It makes things so much easier... – Chris Davies Mar 27 '22 at 20:46'
then include that case in your example as some potential solutions would fail given that input. What should the output be if an input line ends with 2'
s, e.g. would input offoo''
outputfoo''
orfoo'
? – Ed Morton Mar 28 '22 at 00:27