Unix, Separate-table

2019-09-11 15:53发布

问题:

How to put space between these two words without affecting other words.

As you can see, I have two columns here adjusted in decreasing order of age. I want to separate the word NameAge so that each of them be in a cell and its corresponding column. How can I solve this problem?

回答1:

The command below insert space between lowercase letter and uppercase letter.

 sed '1s/\([a-z]\)\([A-Z]\)/\1 \2/g' orig.txt > split.txt

orig.txt:

NameAge
Bob      90
Sarah    76
Russel   49
Kate     21

split.txt

Name Age
Bob      90
Sarah    76
Russel   49
Kate     21

EDIT: 1 in the sed expression to affect only header, per suggested by @jm666

EDIT2:

sed '1s/-/ /g' orig.txt > split.txt

Turned out that orig.txt looks like this:

Name-PersonNumber-Age
Bob      1 90
Sarah    2 76
Russel   3 49
Kate     4 21

split.txt

Name PersonNumber Age
Bob      1 90
Sarah    2 76
Russel   3 49
Kate     4 21


标签: shell unix