bash 使用 Shell 从字符串中间删除第 n 个字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10217016/
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
Remove nth character from middle of string using Shell
提问by bkcberry
I've been searching google for ever, and I cannot find an example of how to do this. I also do not grasp the concept of how to construct a regular expression for SED, so I was hoping someone could explain this to me.
我一直在搜索谷歌,但找不到如何执行此操作的示例。我也不明白如何为 SED 构建正则表达式的概念,所以我希望有人能向我解释这一点。
I'm running a bash script against a file full of lines of text that look like this: 2222,H,73.82,04,07,2012
我正在针对一个充满文本行的文件运行 bash 脚本,如下所示:2222,H,73.82,04,07,2012
and I need to make them all look like this: 2222,H,73.82,04072012
我需要让它们看起来像这样:2222,H,73.82,04072012
I need to remove the last two commas, which are the 16th and 19th characters in the line. Can someone tell me how to do that? I was going to use colrm, which is blessedly simple, but i can't seem to get that installed in CYGWIN. Please and thank you!
我需要删除最后两个逗号,即行中的第 16 个和第 19 个字符。有人能告诉我怎么做吗?我打算使用 colrm,这很简单,但我似乎无法在 CYGWIN 中安装它。谢谢,麻烦您了!
回答by Johnsyweb
I'd use awkfor this:
我会用awk这个:
awk -F',' -v OFS=',' '{ print , , , }' inputfile
This takes a CSV file and prints the first, second and third fields, each followed by the output field separator (",") and then the fourth, fifth and sixth fields concatenated.
这需要一个 CSV 文件并打印第一个、第二个和第三个字段,每个字段后跟输出字段分隔符 ( ","),然后连接第四、第五和第六个字段。
Personally I find this easier to read and maintain than regular expression-based solutions in sedand it will cope well if any of your columns get wider (or narrower!).
就我个人而言,我发现这比基于正则表达式的解决方案更易于阅读和维护,sed并且如果您的任何列变宽(或更窄!),它将很好地应对。
回答by vyegorov
This will work on any string and will remove only the last 2 commas:
这适用于任何字符串,并且只会删除最后 2 个逗号:
sed -e 's/\(.*\),\([^,]*\),\([^,]*\)$//' infile.txt
Note that in my sed variant I have to escape parenthesis, YMMV.
请注意,在我的 sed 变体中,我必须转义括号 YMMV。
回答by Kenavoz
It should work :
它应该工作:
sed -e 's~,~~4g' file.txt
remove 4th and next commas
删除第 4 个和下一个逗号
回答by ghoti
I also do not grasp the concept of how to construct a regular expression for SED, so I was hoping someone could explain this to me.
我也不明白如何为 SED 构建正则表达式的概念,所以我希望有人能向我解释这一点。
The basic notation that people are telling you here is: s/PATTERN/REPLACEMENT/
人们在这里告诉你的基本符号是: s/PATTERN/REPLACEMENT/
Your PATTERN is a regular expression, which may contain parts that are in brackets. Those parts can then be referred to in the REPLACEMENT part of the command. For example:
您的 PATTERN 是一个正则表达式,它可能包含括号中的部分。然后可以在命令的 REPLACEMENT 部分中引用这些部分。例如:
> echo "aabbcc" | sed 's/\(..\)\(..\)\(..\)//'
bbccaa
Note that in the version of sed I'm using defaults to the "basic" RE dialect, where the brackets in expressions need to be escaped. You can do the same thing in the "extended" dialect:
请注意,在 sed 版本中,我使用“基本”RE 方言的默认值,其中表达式中的括号需要转义。你可以在“扩展”方言中做同样的事情:
> echo "aabbcc" | sed -E 's/(..)(..)(..)//'
bbccaa
(In GNU sed (which you'd find in Linux), you can get the same results with the -roptions instead of -E. I'm using OS X.)
(在 GNU sed(您可以在 Linux 中找到)中,您可以使用-r选项而不是获得相同的结果-E。我使用的是 OS X。)
I should say that for your task, I would definitely follow Johnsyweb's advice and use awk instead of sed. Much easier to understand. :)
我应该说,对于您的任务,我肯定会遵循 Johnsyweb 的建议并使用 awk 而不是 sed。更容易理解。:)
回答by user unknown
echo "2222,H,73.82,04,07,2012" | sed -r 's/(.{15}).(..).//'
Take 15 chars, drop one, take 2, drop one.
取 15 个字符,放下一个,取 2 个,放下一个。
回答by Bohemian
sed -e 's/(..),(..),(....)$//' myfile.txt

