简体   繁体   English

在bash文件的每一行中添加一个数字

[英]Add a number to each line of a file in bash

I have some files with some lines in Linux like: 我在Linux中有一些带有某些行的文件,例如:

2013/08/16,name1,,5000,8761,09:00,09:30
2013/08/16,name1,,5000,9763,10:00,10:30
2013/08/16,name1,,5000,8866,11:00,11:30
2013/08/16,name1,,5000,5768,12:00,12:30
2013/08/16,name1,,5000,11764,13:00,13:30
2013/08/16,name2,,5000,2765,14:00,14:30
2013/08/16,name2,,5000,4765,15:00,15:30
2013/08/16,name2,,5000,6765,16:00,16:30
2013/08/16,name2,,5000,12765,17:00,17:30
2013/08/16,name2,,5000,25665,18:00,18:30
2013/08/16,name2,,5000,45765,09:00,10:30
2013/08/17,name1,,5000,33765,10:00,11:30
2013/08/17,name1,,5000,1765,11:00,12:30
2013/08/17,name1,,5000,34765,12:00,13:30
2013/08/17,name1,,5000,12765,13:00,14:30
2013/08/17,name2,,5000,1765,14:00,15:30
2013/08/17,name2,,5000,3765,15:00,16:30
2013/08/17,name2,,5000,7765,16:00,17:30

My column separator is " , " and in the third column (currently ,, ), I need the entry number within the same day. 我列分隔符为“ ”并且在第三列(目前, ,, ),我需要在同一天内的条目号。 For example, with date 2013/08/16 I have 11 lines and with date 2013/08/17 7 lines, so I need add the numbers for example: 例如,日期为2013/08/16的我有11行,日期为2013/08/17的我有7行,因此我需要添加数字,例如:

2013/08/16,name1,1,5000,8761,09:00,09:30
2013/08/16,name1,2,5000,9763,10:00,10:30
2013/08/16,name1,3,5000,8866,11:00,11:30
2013/08/16,name1,4,5000,5768,12:00,12:30
2013/08/16,name1,5,5000,11764,13:00,13:30
2013/08/16,name2,6,5000,2765,14:00,14:30
2013/08/16,name2,7,5000,4765,15:00,15:30
2013/08/16,name2,8,5000,6765,16:00,16:30
2013/08/16,name2,9,5000,12765,17:00,17:30
2013/08/16,name2,10,5000,25665,18:00,18:30
2013/08/16,name2,11,5000,45765,09:00,10:30
2013/08/17,name1,1,5000,33765,10:00,11:30
2013/08/17,name1,2,5000,1765,11:00,12:30
2013/08/17,name1,3,5000,34765,12:00,13:30
2013/08/17,name1,4,5000,12765,13:00,14:30
2013/08/17,name2,5,5000,1765,14:00,15:30
2013/08/17,name2,6,5000,3765,15:00,16:30
2013/08/17,name2,7,5000,7765,16:00,17:30

I need do it in bash. 我需要用bash来做。 How can I do it? 我该怎么做?

This one's good too: 这也很好:

awk -F, 'sub(/,,/, ","++a[$1]",")1' file

Output: 输出:

2013/08/16,name1,1,5000,8761,09:00,09:30
2013/08/16,name1,2,5000,9763,10:00,10:30
2013/08/16,name1,3,5000,8866,11:00,11:30
2013/08/16,name1,4,5000,5768,12:00,12:30
2013/08/16,name1,5,5000,11764,13:00,13:30
2013/08/16,name2,6,5000,2765,14:00,14:30
2013/08/16,name2,7,5000,4765,15:00,15:30
2013/08/16,name2,8,5000,6765,16:00,16:30
2013/08/16,name2,9,5000,12765,17:00,17:30
2013/08/16,name2,10,5000,25665,18:00,18:30
2013/08/16,name2,11,5000,45765,09:00,10:30
2013/08/17,name1,1,5000,33765,10:00,11:30
2013/08/17,name1,2,5000,1765,11:00,12:30
2013/08/17,name1,3,5000,34765,12:00,13:30
2013/08/17,name1,4,5000,12765,13:00,14:30
2013/08/17,name2,5,5000,1765,14:00,15:30
2013/08/17,name2,6,5000,3765,15:00,16:30
2013/08/17,name2,7,5000,7765,16:00,17:30

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将变量中的字符串添加到BASH文件中的每一行 - How to add a string from variables to each line in a file in BASH 在 Bash 中,如何在文件的每一行之后添加一个字符串? - In Bash, how do I add a string after each line in a file? 如何在 ext 文件 BASH 的每一行中添加一个数字 - how can I add a number to every line in a ext file BASH 在bash shell中的文件中每行打印3个字符 - Printing 3 character of each line in a file in bash shell Bash:对于文件中的每一行,设置变量 - Bash: For Each Line in a File, Set variable Bash-查找并替换目录中文件中的一行 - Bash - Find and replace a line in a file in number of directories Bash:如何在一行上匹配特定数字时为一行添加前缀 - Bash: How to add a prefix to a line when a specific number is matched on a line 如何使用Bash将字符串添加到文件中的每一行 - How to add a string to every line in a file with Bash Bash:如何缩短日志文件的长行,同时从每行的开头和结尾保持固定数量的字符? - Bash: How to shorten long lines of a log file whilst keeping a fixed number of characters from beginning and end of each line? 将文本文件每一行中某个字符的出现次数与一个数字进行比较,并根据条件添加更多该字符 - Compare the occurrences of a character in each line of a text file to a number, and add more of that character according to a condition
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM