Windows环境下sed的正确方法

发布于 2025-01-07 07:43:55 字数 2911 浏览 0 评论 0原文

我有以下 sed ,它工作完美,但我不喜欢这个流程,因为它每行都输出结果。代码的第二部分代表我试图巩固的内容,但输出不正确。有人可以帮忙吗?第二部分中的这段代码将我的结果抛出 wack -i -e ":a;N;$!ba;s/\n/ /g" 它在顶部代码中工作正常,但我无法添加它在第二个代码中没有写出结果集。我只是想正确地写这个。

sed -i "1,275d" ConedScrape_494202320000008.txt
sed -i -r "s/>/>~/g; s/</~</g;" ConedScrape_494202320000008.txt
sed -i -r "s/~~//g" ConedScrape_494202320000008.txt
sed -i -e :a -e "s/<[^>]*>//g;/</N;//ba" ConedScrape_494202320000008.txt
sed -i "s/\s/\n/g" ConedScrape_494202320000008.txt
sed -i -n -e ":a" -e "$ s/\n/ /gp;N;b a" ConedScrape_494202320000008.txt
sed -i -e "s/^[ \t]*//" ConedScrape_494202320000008.txt
sed -i -e "s/\r//" ConedScrape_494202320000008.txt
sed -i -e "s/&nbsp;*/,/g; s/--*/--,/g" ConedScrape_494202320000008.txt
sed -i -e "s/   //g; s/If you have any problems or questions please contact us at     [email protected]//g;" ConedScrape_494202320000008.txt
sed -i -r "s/,//g; s/~~,/,/g; s/~~/,/g;" ConedScrape_494202320000008.txt
sed -i -r "s/, /,/g; s/,~/,/g;" ConedScrape_494202320000008.txt
sed -i -r "s/,,/,/g; s/~  ,//g;" ConedScrape_494202320000008.txt
sed -i -r "s/,,/,/g" ConedScrape_494202320000008.txt
sed -i -r "s/,, , , ,If you have any problems or questions please contact us at ,[email protected],,//g" ConedScrape_494202320000008.txt
sed -i -r "s/,,/,/g; s/, ,//g; s/,Tension Code,Stratum Variable,ICAP,PFJ ICAP,Residential %%,LBMP Zone//g; s/,Trip Number,Service Class,Previous Account Number,Min Monthly Demand,TOD Code,Profile,Tax,Muni//g; s/,From Date,To Date,Use,Demand,Bill Amt//g;" ConedScrape_494202320000008.txt
sed -i -r "s/, //g;" ConedScrape_494202320000008.txt

代码的第二部分。

@echo off
sed -i -e "1,275d" ^
-r -e "s/>/>~/g; s/</~</g;" ^
-e "s/~~//g" ^
-e :a -e "s/<[^>]*>//g;/</N;//ba" ^
-e "s/\s/\n/g" ConedScrape_494202320000008.txt
sed -i -e ":a;N;$!ba;s/\n/ /g" ConedScrape_494202320000008.txt
sed -i -e "s/^[ \t]*//" ^
-e "s/\r//" ^
-e "s/&nbsp;*/,/g; s/--*/--,/g" ^
-e "s/,//g; s/~~,/,/g; s/~~/,/g;" ^
-e "s/\n/ /g;" ^
-e "/^$/d" ^
-e "s/^\([^,]*,\)\{2\}//" ^
-e "s/,Tension Code,Stratum Variable,ICAP,PFJ ICAP,Residential %%,LBMP Zone//g" ^
-e "s/,Trip Number,Service Class,Previous Account Number,Min Monthly Demand,TOD   Code,Profile,Tax,Muni//g" ^
-e "s/,From Date,To Date,Use,Demand,Bill Amt//g;" ^
-e "s/, /,/g;" ^
-e "s/ ,/,/g;" ^
-e "s/,,//g;" ^
-e "s/, , ,If you have any problems or questions please contact us  at,[email protected]//g;" ConedScrape_494202320000008.txt

I have the following sed which works perfect but I do not like the flow because it outputs the result every line. The second portion of code represents what I am trying to consolidate but the output is not correct. Can anyone assist? This piece of code in the second portion is throwing my result out of wack -i -e ":a;N;$!ba;s/\n/ /g" it works fine in the top code but i can not add it in the second code witout writing out the result set. I am just trying to write this correctly.

sed -i "1,275d" ConedScrape_494202320000008.txt
sed -i -r "s/>/>~/g; s/</~</g;" ConedScrape_494202320000008.txt
sed -i -r "s/~~//g" ConedScrape_494202320000008.txt
sed -i -e :a -e "s/<[^>]*>//g;/</N;//ba" ConedScrape_494202320000008.txt
sed -i "s/\s/\n/g" ConedScrape_494202320000008.txt
sed -i -n -e ":a" -e "$ s/\n/ /gp;N;b a" ConedScrape_494202320000008.txt
sed -i -e "s/^[ \t]*//" ConedScrape_494202320000008.txt
sed -i -e "s/\r//" ConedScrape_494202320000008.txt
sed -i -e "s/ */,/g; s/--*/--,/g" ConedScrape_494202320000008.txt
sed -i -e "s/   //g; s/If you have any problems or questions please contact us at     [email protected]//g;" ConedScrape_494202320000008.txt
sed -i -r "s/,//g; s/~~,/,/g; s/~~/,/g;" ConedScrape_494202320000008.txt
sed -i -r "s/, /,/g; s/,~/,/g;" ConedScrape_494202320000008.txt
sed -i -r "s/,,/,/g; s/~  ,//g;" ConedScrape_494202320000008.txt
sed -i -r "s/,,/,/g" ConedScrape_494202320000008.txt
sed -i -r "s/,, , , ,If you have any problems or questions please contact us at ,[email protected],,//g" ConedScrape_494202320000008.txt
sed -i -r "s/,,/,/g; s/, ,//g; s/,Tension Code,Stratum Variable,ICAP,PFJ ICAP,Residential %%,LBMP Zone//g; s/,Trip Number,Service Class,Previous Account Number,Min Monthly Demand,TOD Code,Profile,Tax,Muni//g; s/,From Date,To Date,Use,Demand,Bill Amt//g;" ConedScrape_494202320000008.txt
sed -i -r "s/, //g;" ConedScrape_494202320000008.txt

second Portion of code.

@echo off
sed -i -e "1,275d" ^
-r -e "s/>/>~/g; s/</~</g;" ^
-e "s/~~//g" ^
-e :a -e "s/<[^>]*>//g;/</N;//ba" ^
-e "s/\s/\n/g" ConedScrape_494202320000008.txt
sed -i -e ":a;N;$!ba;s/\n/ /g" ConedScrape_494202320000008.txt
sed -i -e "s/^[ \t]*//" ^
-e "s/\r//" ^
-e "s/ */,/g; s/--*/--,/g" ^
-e "s/,//g; s/~~,/,/g; s/~~/,/g;" ^
-e "s/\n/ /g;" ^
-e "/^$/d" ^
-e "s/^\([^,]*,\)\{2\}//" ^
-e "s/,Tension Code,Stratum Variable,ICAP,PFJ ICAP,Residential %%,LBMP Zone//g" ^
-e "s/,Trip Number,Service Class,Previous Account Number,Min Monthly Demand,TOD   Code,Profile,Tax,Muni//g" ^
-e "s/,From Date,To Date,Use,Demand,Bill Amt//g;" ^
-e "s/, /,/g;" ^
-e "s/ ,/,/g;" ^
-e "s/,,//g;" ^
-e "s/, , ,If you have any problems or questions please contact us  at,[email protected]//g;" ConedScrape_494202320000008.txt

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

韶华倾负 2025-01-14 07:43:55

如果您有大量 sed 命令,可以将它们放入一个文件中并使用:

$ cat script.sed
s/foo/bar/g
s/xxx/yyy/g

$ sed -i -f script.sed data.txt

If you have a large number of sed commands, you can put them into a file and use:

$ cat script.sed
s/foo/bar/g
s/xxx/yyy/g

$ sed -i -f script.sed data.txt
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文