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

3
  • What is your problem? Have you tried the sed command you showed? What is its output? Commented Oct 2, 2016 at 7: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.. Commented Oct 2, 2016 at 7: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 Commented Oct 2, 2016 at 8:33

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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
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.

10 Comments

I have already tried that it's not working
You must quote the first $.
@user3792699 : What version of sed are you using? Some may not support the -i option. In that case you need to do sed "s/$/$BATCH_RUN_DATE/" data.csv > tempfile && mv temp data.csv
This is a very common answer. Please flag as a duplicate instead of answering, so that we collect all good anwers in a single place. The Stack Overflow bash tag wiki contains a collection of common questions for this tag. Thanks in advance!
@sjsam My mistake, I thought $/ will be interpreted as a variable.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.