2

I have a file and I want to append the value in a unix variable at the end of each line using SED.

I have already achieved this through AWK but I would like to do it in SED

something like this. I have already tried the below command and it's not working.

sed -i 's/$/"$BATCH_RUN_DATE"/g' data.csv

Error

sed: -e expression #1, char 10: unknown option to `s'

GNU sed version 4.2.1

Regards, Aswinikumar

tripleee
  • 175,061
  • 34
  • 275
  • 318
user3792699
  • 339
  • 3
  • 7
  • 17
  • What is your problem? Have you tried the sed command you showed? What is its output? – Olaf Dietsche Oct 02 '16 at 07:30
  • @user3792699 : Please format code in question. In such cases, it is expected to give a sample input/output. `It's not working` doesn't explain anything at all.. – sjsam Oct 02 '16 at 07:36
  • 1
    Since, it is a date and sed complains about an unknown option, I guess, the date (`$BATCH_RUN_DATE`) contains slashes. This collides with sed's separators. In this case, you must use a different separator, like comma or exclamation mark, e.g. `sed -e "s!$! ${BATCH_RUN_DATE}!" data.csv` – Olaf Dietsche Oct 02 '16 at 08:33

2 Answers2

3

here is an example of how you could do it:

% cat subject.txt                     
Studid    StudName     Asp.Net   DBMS     Unix
   1       Ani         75        62       80
   2       George      90        95       82
   3       Jake        45        30       40
   4       Dennie      89        92       90
% 
% my_var="R2D2"                       
% 
% sed "s/$/${my_var}/" subject.txt 
Studid    StudName     Asp.Net   DBMS     UnixR2D2
   1       Ani         75        62       80R2D2
   2       George      90        95       82R2D2
   3       Jake        45        30       40R2D2
   4       Dennie      89        92       90R2D2

Explanation

The tricky thing about this sed expression, is why don't I need to reinsert the $ newline in the replace part of the substitution?.
I believe this is because when sed loads each line into the pattern space it removes the newline, then after all pattern space operations, it then re-attaches a newline to the contents of the pattern space then prints the pattern space, so I guess the newline is a kind of a "freebie" here.
Then you might ask the question, how does the first $ match if the pattern space has no newline in it ? -- if I had to guess, I might say , sed just knows what you mean when you use the $ meta character, but that is just a guess.

the_velour_fog
  • 2,094
  • 4
  • 17
  • 29
  • sed "s/$/${BATCH_RUN_DATE}/g" data.csv sed: -e expression #1, char 10: unknown option to `s' I have already this and it's not working. Got the above error message – user3792699 Oct 02 '16 at 07:43
1

You should put the script itself inside double quotes if you wish bash variable expansion to happen

sed -i "s/$/$BATCH_RUN_DATE/" data.csv

should do the job. This is the most easiest way to do it. Also, note you don't need the global flag as there is only one substitution per line.

Sidenotes

  • Check if your sed supports inplace edit option
  • Check if $BATCH_RUN_DATE itself is non-empty.
sjsam
  • 21,411
  • 5
  • 55
  • 102