将数据数字在shell脚本中分成两列。

huangapple go评论41阅读模式
英文:

split data numbers into two columns in shell script

问题

我有从1到1000的数字保存在文件($y)中。

for i in {1..1000}
do
echo "$i" >> $y
done

我想要将数字在文件中分成两列(用户输入文件名表示"$y")。

1    501
.    .
.    .
500  1000 

就像这样。

我尝试过 split -l500 $ycolumn -t 命令,但是没有得到预期的结果。

英文:

I have numbers from 1 to 1000 in file ($y).

for i in {1..1000}
do
echo "$i" >> $y
done

I want to split numbers into two column in the file(user enters the filename that indicates "$y").

1    501
.    .
.    .
500  1000 

like this.

I tried split -l500 $y and column -t command but couldn't get expected result.

答案1

得分: 0

我会做的事情:

    printf '%d\t%d\n' {1..100} 
    1    2
    3    4
    5    6
    [...]

然后:

    printf '%d\t%d\n' {1..100} | while read i; do
            echo "$i" >> "$y"
        done
英文:

What I would do:

printf '%d\t%d\n' {1..100} 
1    2
3    4
5    6
[...]

Then:

printf '%d\t%d\n' {1..100} | while read i; do
        echo "$i" >> "$y"
    done

huangapple
  • 本文由 发表于 2023年3月15日 18:31:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75743474.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定