如何在文件的第一行中找到总逗号?

时间:2016-03-06 11:42:41

标签: windows batch-file scripting

我想创建一个批处理文件来查找文本文件第一行中的逗号总数。

示例文本文件 的 input.txt中

12345,Bhavik
12323,Bhavik,Sanghvi

输出

1

我试图在网上冲浪,但无法找到解决方案,请帮忙

5 个答案:

答案 0 :(得分:1)

这是此问题的另一个简单解决方案。

@echo off
setlocal enabledelayedexpansion
set LF=^


::Above 2 blank lines are critical - do not remove
for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"
set /p var=<input.txt
echo "%var:,="!cr!!lf!"..***..%">temp.file
find /c "..***.." <temp.file
del temp.file

答案 1 :(得分:0)

如果您不是受到糟糕的Windows BATCH工具的阻碍,而是从Windows here的Unix工具安装awk,那么您可以这样做:

awk -F, 'NR==1{print NF-1;exit}' input.txt

那说...... “运行awk并使用逗号作为分隔符来分割字段。在第1行,打印此行上的字段数减1,然后退出。对文件执行此操作input.txt“。

gawk只是awk的一个略有不同的版本,如果你在Unix Utils包中得到那个版本。您可能需要将双引号替换为单引号,以适应Windows缺乏的能力。

答案 2 :(得分:0)

@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q35826440.txt"
:: first method
SET /a count=0
FOR /f "usebackqdelims=" %%a IN ("%filename1%") DO SET "line=%%a"&GOTO got1
:got1
SET "line=%line:"=%"
IF NOT DEFINED line ECHO method 1: %count% found&GOTO method2
IF "%line:~-1%"=="," SET /a count+=1
SET "line=%line:~0,-1%"
GOTO got1
:: second method
:method2
SET /a count=-1
FOR /f "usebackqdelims=" %%a IN ("%filename1%") DO SET "line=%%a"&GOTO got2
:got2
SET "line=%line:"=%"
SET "line=%line:;=%"
SET "line=%line: =%"
SET "line=%line:,=x,x%"
FOR %%a IN (%line%) DO SET /a count+=1
ECHO method 2: %count% found

GOTO :EOF 

您需要更改sourcedir的设置以适合您的具体情况。

我使用了一个名为q35826440.txt的文件,其中包含我的测试数据。

两种方法 - 都将第一行读取到line,然后删除所有"个字符。

第一个然后机械循环,检查最后一个字符是否为逗号,计算是否为,并删除最后一个字符,直到找到的字符串为空。

第二个替换所有; Space 字符(为了更好的衡量, Tab 也可以删除),然后用x,x替换逗号

结果是剩下的唯一分隔符是逗号,并且在列表中将有1个项目比逗号更多。

因此,在-1处启动计数器,并为列表中找到的每个元素递增。

答案 3 :(得分:0)

下一个解决方案(类似于Magoo的第二种方法)似乎甚至会在输入文件中处理˙cmd˙ and .bat poisonous characters

@ECHO OFF
SETLOCAL EnableExtensions DisableDelayedExpansion
set "infile=D:\bat\SO\files\35826440input.txt"   change to suit your circumstances
set /A "commacount=-1"
for /F "usebackq delims=" %%G in ("%infile%") do (
  set "line=%%G"
  call :parseline
  if /I not "%~1"=="/all" goto :continue
) 
:continue
echo script continues here
ENDLOCAL
exit /B

:parseline
  rem treat unbalanced doublequote in next line
  set "lineToParse=%line:"=§%"
  set "lineToParse=%lineToParse:,=","%"
  set /A "commacount=-1"
  for %%g in ("%lineToParse%") do (
    set /A "commacount+=1"
    rem echo %line%, !commacount!, %%g    
  )
  echo %commacount% "%line%"
goto :eof

输出(带输入文件列表):

==> D:\bat\SO\35826440.bat
1 "12345,Bhavik"
script continues here

==> D:\bat\SO\35826440.bat /all
1 "12345,Bhavik"
2 "12323,Bhavik,Sanghvi"
3 "12323,Bhavik,Sanghvi,three"
0 "zero"
1 ",1 leading"
2 ",,2 leading"
1 "trailing,"
2 "2 trailing,,"
2 "2 middle,,mid"
4 "!OS!,!,!!,!!!,exclamations"
4 "%OS%,%,%%%,%%,percents"
8 "&,|,>,<,",",;,=,miscelaneous"
0 "unbalanced"doublequote"
script continues here

==> type D:\bat\SO\files\35826440input.txt
12345,Bhavik
12323,Bhavik,Sanghvi
12323,Bhavik,Sanghvi,three
zero
,1 leading
,,2 leading
trailing,
2 trailing,,
2 middle,,mid
!OS!,!,!!,!!!,exclamations
%OS%,%,%%%,%%,percents
&,|,>,<,",",;,=,miscelaneous
unbalanced"doublequote

==>

答案 4 :(得分:0)

@echo off
setlocal EnableDelayedExpansion

rem Read the first line
set /P "line=" < input.txt

rem Store it in a text file
> before.txt echo !line!

rem Store the line without commas in a second file
> after.txt echo !line:,=!

rem Get the difference in sizes between both files
set "diff="
for %%a in (before.txt after.txt) do (
   if not defined diff (
      set "diff=%%~Za"
   ) else (
      set /A "diff-=%%~Za"
   )
)
del before.txt after.txt

echo %diff%