如何在sed命令中正确用双引号将变量引起来

FlexMcMurphy

我有一个.csv文件,其中包含两个“列”数据,如下所示:

test1.ts.meta,Before Sunrise (1995)
test2.ts.meta,A Beautiful Mind (2001)
test3.ts.meta,Departures (2008)
test4.ts.meta,Love & Other Drugs (2010)

我正在尝试使用此命令将每个.ts.meta文件中的第2行替换为对应的电影名称...

cat 1TBMovie2_dotTSdotMeta.txt | while IFS=, read file moviename; do sed "2 s/^.*$/$moviename/" "$file"; done

除电影名称中带有“&”号的电影名称外,它都可以正常工作。
例如,电影名称:Love&Other Drugs(2010)在这种情况下,.ts.meta文件的第二行获得以下电影名称:

Love Love Love & Other Drugs Other Drugs (2010) Other Drugs (2010)

同样,电影名称:Love&Mercy(2015)出现在.ts.meta文件中,例如:Love Love Love&Mercy Mercy(2015)Mercy(2015)

令人困惑的是...如果我打开名为Love&Mercy(2015)的电影的.ts.meta文件并手动删除第2行并保存,然后再次运行上面的命令,我将在第2行中得到此信息... Love Mercy(2015)在“ Love”和“ Mercy”之间有两个空格。

我想我需要像使用$ file变量一样将$ moviename变量括在双引号中吗?我猜sed读取的&字符具有特殊含义吗?

这是一些更多信息以澄清问题

我的csv文件(我实际上称它为:updatemeta.txt

test1.ts.meta,Carols from King's (2013)
test2.ts.meta,Before Sunrise (1995)
test3.ts.meta,Love & Other Drugs (2010)
test4.ts.meta,Departures (2008)

test1.ts.meta

1:0:19:1B1C:802:2:11A0000:0:0:0:
Carols from King's
The traditional Christmas carol concert from King's College Chapel, Cambridge. Stephen Cleobury conducts the famous chapel choir in carols old and new. [HD] [S]
1387969020

448066800
2913369072
f:0,c:00157c,c:01157e,c:02157f,c:03157c,c:050001
188
0

test2.ts.meta

1:0:1:189E:7FD:2:11A0000:0:0:0:
Before Sunrise
Romance starring Julie Delpy and Ethan Hawke. Two twentysomethings meet on a train and decide to spend a few hours together. Contains some strong language.  Also in HD. [1995] [AD,S]
1392418980

550744512
2637755808
f:0,c:0013ec,c:0113ed,c:0213ef,c:0313ec
188
0

test3.ts.meta

1:0:1:2404:7F9:2:11A0000:0:0:0:
Love & Other Drugs
(2010) Fact-based adult comedy. Jake Gyllenhaal stars as a successful Viagra salesman who falls for a woman with Parkinson's (Anne Hathaway). Strong language/sexual scenes.  [AD,S]
1472775840

712401799
2824257448
f:0,c:000931,c:010932,c:020934,c:030931
188
0

test4.ts.meta

1:0:1:2404:7F9:2:11A0000:0:0:0:
Departures
(2008) An Oscar-winning, whimsical look at the Japanese undertaking profession. Masahiro Motoki stars as a musician starting a new career preparing the dead for burial. Japanese/subs.
1400111580

863881200
3699150040
f:0,c:000931,c:010932,c:020934,c:030931
188
0

我将把.csv文件与许多.ts.meta文件放在同一目录中。目录中每个.ts.meta文件的.csv文件中都将有一行,并有一个对应的电影名称。

如何使用sed或awk或gawk创建一个脚本,该脚本循环遍历.csv文件中的每一行,并用.csv文件中指定的相应影片名称替换命名的.ts.meta文件中的第二行?

我尝试了以下解决方案中给出的示例,但不了解发生了什么!

谢谢,

柔性

埃德·莫顿

不要仅仅为了处理文本而编写shell循环,请查看为什么使用shell循环来处理文本考虑到的坏做法,并且当您要使用文字字符串时,请使用awk之类的工具,理解文字字符串,而不是像sed这样的工具。

您没有提供任何.ts.meta文件供我们测试,显然这是未经测试的,但是像这样的事情将使用GNU awk来完成-i inplace(假设您要修改原始文件)和ARGIND

awk -i inplace -F',' '
    NR == FNR {
        titles[ARGC] = $2
        ARGV[ARGC++] = $1
    }
    (NR != FNR) && (FNR == 2) {
        $0 = titles[ARGIND]
    }
    { print }
' 1TBMovie2_dotTSdotMeta.txt

如果您真的想尝试使用sed来做到这一点(不要!),那么请参见sed是否可以逃脱regex元字符,并注意它&不是唯一的字符不必担心,例如/\1,也将需要处理。

考虑到您新提供的示例输入:

$ head -n 50 update* *.meta
==> updatemeta.txt <==
test1.ts.meta,Carols from King's (2013)
test2.ts.meta,Before Sunrise (1995)
test3.ts.meta,Love & Other Drugs (2010)
test4.ts.meta,Departures (2008)

==> test1.ts.meta <==
1:0:19:1B1C:802:2:11A0000:0:0:0:
Carols from King's
The traditional Christmas carol concert from King's College Chapel, Cambridge. Stephen Cleobury conducts the famous chapel choir in carols old and new. [HD] [S]
1387969020

448066800
2913369072
f:0,c:00157c,c:01157e,c:02157f,c:03157c,c:050001
188
0

==> test2.ts.meta <==
1:0:1:189E:7FD:2:11A0000:0:0:0:
Before Sunrise
Romance starring Julie Delpy and Ethan Hawke. Two twentysomethings meet on a train and decide to spend a few hours together. Contains some strong language.  Also in HD. [1995] [AD,S]
1392418980

550744512
2637755808
f:0,c:0013ec,c:0113ed,c:0213ef,c:0313ec
188
0

==> test3.ts.meta <==
1:0:1:2404:7F9:2:11A0000:0:0:0:
Love & Other Drugs
(2010) Fact-based adult comedy. Jake Gyllenhaal stars as a successful Viagra salesman who falls for a woman with Parkinson's (Anne Hathaway). Strong language/sexual scenes.  [AD,S]
1472775840

712401799
2824257448
f:0,c:000931,c:010932,c:020934,c:030931
188
0

==> test4.ts.meta <==
1:0:1:2404:7F9:2:11A0000:0:0:0:
Departures
(2008) An Oscar-winning, whimsical look at the Japanese undertaking profession. Masahiro Motoki stars as a musician starting a new career preparing the dead for burial. Japanese/subs.
1400111580

863881200
3699150040
f:0,c:000931,c:010932,c:020934,c:030931
188
0

这是运行的awk脚本:

$ awk -i inplace -F',' '
    NR == FNR {
        titles[ARGC] = $2
        ARGV[ARGC++] = $1
    }
    (NR != FNR) && (FNR == 2) {
        $0 = titles[ARGIND]
    }
    { print }
' updatemeta.txt

这就是对您的文件所做的操作:

$ head -n 50 update* *.meta
==> updatemeta.txt <==
test1.ts.meta,Carols from King's (2013)
test2.ts.meta,Before Sunrise (1995)
test3.ts.meta,Love & Other Drugs (2010)
test4.ts.meta,Departures (2008)

==> test1.ts.meta <==
1:0:19:1B1C:802:2:11A0000:0:0:0:
Carols from King's (2013)
The traditional Christmas carol concert from King's College Chapel, Cambridge. Stephen Cleobury conducts the famous chapel choir in carols old and new. [HD] [S]
1387969020

448066800
2913369072
f:0,c:00157c,c:01157e,c:02157f,c:03157c,c:050001
188
0

==> test2.ts.meta <==
1:0:1:189E:7FD:2:11A0000:0:0:0:
Before Sunrise (1995)
Romance starring Julie Delpy and Ethan Hawke. Two twentysomethings meet on a train and decide to spend a few hours together. Contains some strong language.  Also in HD. [1995] [AD,S]
1392418980

550744512
2637755808
f:0,c:0013ec,c:0113ed,c:0213ef,c:0313ec
188
0

==> test3.ts.meta <==
1:0:1:2404:7F9:2:11A0000:0:0:0:
Love & Other Drugs (2010)
(2010) Fact-based adult comedy. Jake Gyllenhaal stars as a successful Viagra salesman who falls for a woman with Parkinson's (Anne Hathaway). Strong language/sexual scenes.  [AD,S]
1472775840

712401799
2824257448
f:0,c:000931,c:010932,c:020934,c:030931
188
0

==> test4.ts.meta <==
1:0:1:2404:7F9:2:11A0000:0:0:0:
Departures (2008)
(2008) An Oscar-winning, whimsical look at the Japanese undertaking profession. Masahiro Motoki stars as a musician starting a new career preparing the dead for burial. Japanese/subs.
1400111580

863881200
3699150040
f:0,c:000931,c:010932,c:020934,c:030931
188
0

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

sh如何将变量的值附加到文件中sh用双引号引起来的多行

exec用双引号引起来

用双引号引起来

如何用引号将变量引起来

用双引号引起来时忽略逗号

在Hibernate中将列名称用双引号引起来

在IntelliJ的代码编辑器中,将选定的文本用双引号引起来

用双引号将单元格值引起来:Excel VBA宏

为什么即使将URL用双引号引起来,wget仍然会失败?

如果用双引号引起来,awk上的逗号分隔字符串将忽略

如果仅非空字段用双引号引起来,我如何读取CSV文件?

',(用引号引起来)

如何更改变量的第三列,以便使用AWK或SED将其用单引号引起来

Python ::如何在列表中将值的双引号引起来

不要用双引号引起来

用引号引起来的命令替换与反斜杠转义

什么是sed comand来修复此文件,所以每行的最后一个值用双引号引起来

变量无法用单引号引起来

当第一个命令行参数用双引号引起来时,错误“ 1”“此时是意外的。

awk输入字段分隔符,并打印所有用双引号引起来的变量

以编程方式用引号将Python输入引起来

将Yaml数据用单引号引起来

将路径用引号引起来是最佳实践吗?

cat bash命令将制表符转换为逗号分隔并用双引号引起来

如何将必须用引号引起来的值传递给命令?

如果已经使用了单引号和双引号,该如何用引号引起来?

请给RegEx帮助-用引号引起来的字符串中包含双引号

用单引号而不是双引号引起来的C ++中的转义序列

如何在u-boot中用引号引起来?