bash 如何在文件中特定字段的单词字符之间插入空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12746906/
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 03:28:30  来源:igfitidea点击:

How to insert whitespace between characters of words in a specific field in a file

bashawkprocessing-efficiency

提问by saloua

I have a file containing 100000 lines like this

我有一个包含 100000 行这样的文件

1 0110100010010101
2 1000010010111001
3 1000011001111000
10 1011110000111110
123 0001000000100001

I would like to know how can I display efficiently just the second field by adding whitespaces between characters.

我想知道如何通过在字符之间添加空格来有效地仅显示第二个字段。

0 1 1 0 1 0 0 0 1 0 0 1 0 1 0 1
1 0 0 0 0 1 0 0 1 0 1 1 1 0 0 1
1 0 0 0 0 1 1 0 0 1 1 1 1 0 0 0
1 0 1 1 1 1 0 0 0 0 1 1 1 1 1 0
0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 1

One solution would be to get the second column with awk and then add the whitespaces using sed. But as the file is too long I would like to avoid using pipes. Then I'm wondering if I can do that by just using awk.

一种解决方案是使用 awk 获取第二列,然后使用 sed 添加空格。但由于文件太长,我想避免使用管道。然后我想知道我是否可以通过使用 awk 来做到这一点。

Thanks in advance

提前致谢

回答by Kent

is this ok?

这个可以吗?

awk '{gsub(/./,"& ",);print }' yourFile

example

例子

kent$  echo "1 0110100010010101
2 1000010010111001
3 1000011001111000"|awk '{gsub(/./,"& ",);print }'
0 1 1 0 1 0 0 0 1 0 0 1 0 1 0 1 
1 0 0 0 0 1 0 0 1 0 1 1 1 0 0 1 
1 0 0 0 0 1 1 0 0 1 1 1 1 0 0 0

update

更新

more than 2 digits in 1st column won't work? I didn't get it:

第一列中超过 2 位数字不起作用?我没明白:

kent$  echo "133 0110100010010101
233 1000010010111001
333 1000011001111000"|awk '{gsub(/./,"& ",);print }'
0 1 1 0 1 0 0 0 1 0 0 1 0 1 0 1 
1 0 0 0 0 1 0 0 1 0 1 1 1 0 0 1 
1 0 0 0 0 1 1 0 0 1 1 1 1 0 0 0 


gsub(/./,"& ", )

1 /./  match any single character
2 "& " & here means the matched string, in this case, each character
3    column 2

so it means, replace each character in 2nd column into the character itself + " ".

回答by Birei

One way using only awk:

一种方法只使用awk

awk '{ gsub( /./, "& ",  ); print ; }' infile

That yields:

这产生:

0 1 1 0 1 0 0 0 1 0 0 1 0 1 0 1 
1 0 0 0 0 1 0 0 1 0 1 1 1 0 0 1 
1 0 0 0 0 1 1 0 0 1 1 1 1 0 0 0


EDIT: Kentand I gave the same implementation, so, for this answer to be a bit more useful, I will add the sedone:

编辑肯特和我给出了相同的实现,因此,为了让这个答案更有用,我将添加sed一个:

sed -e 's/^[^ ]* *//; s/./& /g' infile

回答by Janito Vaqueiro Ferreira Filho

Just adding a sed alternative:

只需添加一个 sed 替代方案:

sed -e 's/^.* *//;s/./& /g;s/ $//' file

Three comands:

三个命令:

  1. Remove the characters and spaces on the start of the line
  2. Replace everycharacter with itself followed by a space
  3. (Optional) Remove the trailing space at the end of the line
  1. 删除行首的字符和空格
  2. 将每个字符替换为自身后跟一个空格
  3. (可选)删除行尾的尾随空格

回答by choroba

sedsolution.

sed解决方案。

sed 's/.* //;s/\(.\)/ /g'

It adds an extra space at the end of each line. Add ;s/ $//to the expression to remove it.

它在每行的末尾添加了一个额外的空格。添加;s/ $//到表达式以将其删除。

回答by potong

This might work for you (GNU sed):

这可能对你有用(GNU sed):

sed 's/^\S*\s*//;s/\B/ /g' /file