bash 通过 sed 使用 unix 变量将数据附加到每行的末尾

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39814806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 15:15:00  来源:igfitidea点击:

Append data to end of each line using unix variable through sed

bashshellunixsed

提问by user3792699

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

我有一个文件,我想使用 SED 在每行末尾附加一个 unix 变量中的值。

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

我已经通过 AWK 实现了这一点,但我想在 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

GNU sed 版本 4.2.1

Note: If you don't know the answer that's fine but don't mark it as duplicate. The one that you provided the reference is different from this question.

注意:如果您不知道答案也可以,但不要将其标记为重复。您提供的参考与此问题不同。

Regards, Aswinikumar

问候, 阿斯维尼库马尔

回答by the_velour_fog

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 sedexpression, is why don't I need to reinsert the $newline in the replace part of the substitution?.
I believe this is because when sedloads 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 , sedjust knows what you mean when you use the $meta character, but that is just a guess.

这个sed表达式的棘手之处在于,为什么我不需要$在替换的替换部分重新插入换行符?.
我相信这是因为当sed将每一行加载到模式空间时它会删除换行符,然后在所有模式空间操作之后,它会重新将换行符附加到模式空间的内容然后打印模式空间,所以我猜换行符在这里是一种“免费赠品”。
那么你可能会问,$如果模式空间中没有换行符,第一个如何匹配?-- 如果我不得不猜测,我可能会说,sed当您使用$元字符时,只知道您的意思,但这只是猜测。

回答by sjsam

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

如果您希望 bash 变量扩展发生,您应该将脚本本身放在双引号内

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 sedsupports inplace edit option
  • Check if $BATCH_RUN_DATEitself is non-empty.
  • 检查您是否sed支持就地编辑选项
  • 检查$BATCH_RUN_DATE自身是否非空。