当新行位于数据中时,对txt文件中的行数进行计数 [英] Count number of line in txt file when new line is inside data

查看:30
本文介绍了当新行位于数据中时,对txt文件中的行数进行计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个txt文件,其中包含以下数据

I have one txt file which has below data

Name    mobile  url message text
test11  1234567890  www.google.com  "Data Test New
Date:27/02/2020
Items: 1
Total: 3
Regards
ABC DATa
Ph:091 : 123456789"
test12  1234567891  www.google.com  "Data Test New one
Date:17/02/2020
Items: 26
Total: 5
Regards
user test
Ph:091 : 433333333"

现在您可以看到我的最后一列数据具有换行符.所以当我使用下面的命令

Now you can see my last column data has new line character. so when I use below command

awk 'END{print NR}' file.txt

它给我的长度是15但实际上行长是3.请建议使用相同的命令

it is giving my length is 15 but actually line length is 3 . Please suggest command for the same

编辑部分:根据给出的答案,如果输入文件末尾没有换行符,以下脚本将不起作用

Edited Part: As per the answer given the below script is not working if there's no newline at the end of input file

awk -v RS='"[^"]*"' '{gsub(/\n/, " ", RT); ORS=RT} END{print NR "\n"}' test.txt 

我的文件可能也有3-4百万条记录.因此,将文件转换为UNIX格式将需要时间,这不是我的偏爱.因此,请提出在两种情况下均应适用的最佳解决方案

Also my file may have 3-4 Million of records . So converting file to unix format will take time and that is not my preference. So Please suggest some optimum solution which should work in both case

head 5.csv | cat -A  
Above command is giving me the output

名称移动网址消息文本^ M $

Name mobile url message text^M$

推荐答案

使用 gnu-awk ,您可以使用自定义的 RS :

Using gnu-awk you can do this using a custom RS:

awk -v RS='"[^"]*"' '{gsub(/(\r?\n){2,}/, "\n"); n+=gsub(/\n/, "&")}
END {print n}' <(sed '$s/$//' file)

15001

这里:

  • -v RS ='[[^"] *'':使用此正则表达式作为输入记录分隔符.匹配双引号的字符串
  • n + = gsub(/\ n/,&"):虚拟用自身替换 \ n 并计算 \ n 在变量 n
  • END {print n} :最后打印 n
  • sed'$ s/$//'文件:最后一行添加换行符(以防丢失)
  • -v RS='"[^"]*"': Uses this regex as input record separator. Which matches a double quoted string
  • n+=gsub(/\n/, "&"): Dummy replace \n with itself and counts \n in variable n
  • END {print n}: Prints n in the end
  • sed '$s/$//' file: For last line adds a newline (in case it is missing)

代码演示

这篇关于当新行位于数据中时,对txt文件中的行数进行计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆