如何在每行的开头添加数字?

时间:2016-04-14 17:35:58

标签: shell awk command-line sed

这就是我通常用来在每行的开头添加数字的内容:

create table customers (
    id int identity(1,1),
    distance int
)
go


insert into customers values(10)
insert into customers values(100)
insert into customers values(20)
insert into customers values(30)
insert into customers values(40)
insert into customers values(50)
insert into customers values(60)
go

create view AllCustomerDistances with SCHEMABINDING 
as
select custA.Id custA_Id, custC.Id custC_id, custA.distance - custC.distance [distanceBetweenAandC] --distance(custA.lat, custA.lon, custC.lon, custC.lon) [distanceBetweenAandC]
from 
    dbo.customers custA cross join
    dbo.customers custC
where
    custA.Id <> custC.Id

go

select *
from
(   select
        custA.Id [A],
        custB.Id [B],
        row_number() over (partition by custA_Id  order by distanceBetweenAandC desc) as Rownumber
    from
    AllCustomerDistances d inner join 
        customers custA on (d.custA_Id = custA.Id) inner join
        customers custb on (d.custC_Id = custb.Id)
) r
where
    Rownumber = 1

但是,我需要做的是在awk '{ print FNR " " $0 }' file 处开始编号。有没有办法从这样的特定数字开始而不必使用行号?

3 个答案:

答案 0 :(得分:3)

nl

有一个特殊命令
nl -v1000001 file

答案 1 :(得分:1)

您只需将1000001添加到FNR(或NR):

awk '{ print (1000001 + FNR), $0 }' file

答案 2 :(得分:1)

$ seq 5 | awk -v n=1000000 '{print ++n, $0}'
1000001 1
1000002 2
1000003 3
1000004 4
1000005 5

$ seq 5 | awk -v n=30 '{print ++n, $0}'
31 1
32 2
33 3
34 4
35 5