Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
207 views
in Technique[技术] by (71.8m points)

command line - How to split values from one cell to multiple cells using bash

I have a file which looks like this:

1|2|3|4
1|2|3|4
1|2|3|4
1   2   3   4
1   2   3   4
1   2   3   4

For a easy understanding we can say that while some values are divides into different cells, there are other values that are stored in a single cell and separated by tabs (although I'm not really sure). What I want to do is to split these values into different cells so my output file should look like this:

1|2|3|4
1|2|3|4
1|2|3|4
1|2|3|4
1|2|3|4
1|2|3|4

I would really appreciate your help!

question from:https://stackoverflow.com/questions/65928921/how-to-split-values-from-one-cell-to-multiple-cells-using-bash

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could also use tr:

tr -s '[:blank:]' '|'

It translates each character using to sets. According to the manual page:

  • -s (or --squeeze-repeats): replace each sequence of a repeated character that is listed in the last specified SET, with a single occurrence of that character (i.e. used to remove duplicates of the character to replace)
  • [:blank:]: all horizontal whitespaces (use instead if you're sure they are all tabs)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...