I am trying to insert a comma after the values on line 1, 4, 8, etc using sed,
sed '0-4 s/$/,/' in.txt > in2.txt
For some reason this isn’t working so I was wondering if anyone has any solutions doing this using awk, sed, or any other methods.
The error I am getting is
sed: 1: "0-4 s/$/,/": invalid command code -
Currently my data looks like this:
City
Address
Zip Code
County
and I was trying to format it like this
City,
Address
Zip Code
County
Much Appreciated.
0-4indeed is not well-formedsedsyntax. I would useawkfor this, but it is easy to do it with either.which substitutes one line and prints it, then prints the next three lines without substitution, then starts over from the beginning of the script; or
which does the substitution if the line number modulo 4 is 1, then prints unconditionally.
Sed’s addressing modes are sometimes a tad disappointing; there is no standard way to calculate line offsets, relative or in reference to e.g. the end of the file. Of course,
awkis more complex, but if you can only learn one or the other, definitely go forawk. (Or in this day and age, Python or Perl — a much better investment.)