Linux 中shell 实现给同一列中的不同类别从头编号

 

001、

复制代码
[root@pc1 test02]# ls
a.txt
[root@pc1 test02]# cat a.txt                                         ## 准备一个测试数据
aa
aa
aa
bb
bb
cc
cc
cc
cc
dd
dd
dd
[root@pc1 test02]# awk '{ay[$1]++; print $0ay[$1]}' a.txt           ## 依据 aa、bb、cc、dd分别从头编号
aa1
aa2
aa3
bb1
bb2
cc1
cc2
cc3
cc4
dd1
dd2
dd3
复制代码

 。

 

002、python实现

复制代码
[root@pc1 test02]# ls
a.txt  test.py
[root@pc1 test02]# cat a.txt                 ## 测试数据
aa
aa
aa
bb
bb
cc
cc
cc
cc
dd
dd
dd
[root@pc1 test02]# cat test.py               ## python程序
#!/usr/bin python
# -*- coding: utf-8 -*-

import sys
import os

in_put = open("a.txt", "r")
out_put = open("result.txt", "w")

dict1 = {}

for i in in_put:
        i = i.strip()
        if i not in dict1:
                dict1[i] = 1
        else:
                dict1[i] += 1
        print(i + str(dict1[i]), file = out_put)

in_put.close()
out_put.close()
[root@pc1 test02]# python test.py                ## 执行程序
[root@pc1 test02]# ls
a.txt  result.txt  test.py
[root@pc1 test02]# cat result.txt                ## 运算结果
aa1
aa2
aa3
bb1
bb2
cc1
cc2
cc3
cc4
dd1
dd2
dd3
复制代码

 

 

 。

 

posted @   小鲨鱼2018  阅读(29)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2023-01-15 几种常见的接口类型
2023-01-15 鼠标右键刷新桌面很慢,其他正常的处理
2022-01-15 linux 中生成两个变量任意的两两组合
2022-01-15 linux 中awk命令实现统计列的频数
2022-01-15 linux shell中for循环结构
2022-01-15 linux 系统中 获取环境变量、 获取环境变量+自定义变量
点击右上角即可分享
微信分享提示