Batch programming

Asked

Viewed 262 times

-2

I would like to make a batch that does the following count:

Read the source file:

7895537000011 
7895537000028 
7895537000035 
7895537000042 
7895537000059 
7895537000066
7895537000011 
7895537000011  
7895537000028 
7895537000028 
7895537000028
7895537000059 
7895537000059 

Write like this in a destination file:

7895537000011,3 
7895537000028,4 
7895537000035,1 
7895537000042,1 
7895537000059,3 
7895537000066,1

Add the repetitions and place in front of the item after a comma. I use to balance items in store. I already managed to make a batch to include ".1" after each barcode using this code:

setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in (Inventario_%loja%_%dia%-%mes%-%ano%_%hour%h%min%m%secs%s.inv) do (

echo %%a,1 >>C:\Inventario\Inventario_%loja%_%dia%-%mes%-%ano%_%hour%h%min%m%secs%s\Inventario_%loja%_%dia%-%mes%-%ano%_%hour%h%min%m%secs%s.log

)

I need to decrease the size of lines generated by all barcodes and implement a count of occurrences where barcodes repeat.

Thus:

bar code, occurrences

7895537000011,3

So that the total is in front of the code.

How to do this?

  • 1

    And was in doubt about what exactly? Did you make a mistake? Which one? If you didn’t, did you generate the unexpected output? What output did you generate? If you can’t even begin to do it, what do you know about Batch? Please edit the question by answering the relevant questions. As you are new in the community, I recommend you do the [tour].

  • Thanks for the tip, I edited.

3 answers

1

The best solution in this case !!

only mute origin.txt and txt destination. in the code for your real file name !

@echo off
setlocal enabledelayedexpansion

for /f "delims=" %%a in (origem.txt) do (
  if defined ##%%a (
    for /f "tokens=2 delims==" %%b in ('set ##%%a') do set /a tmp=%%b+1
    set "##%%a=!tmp!"
    ) else (
    set "##%%a=1"
    )
    )

(for /f "tokens=1,2 delims==" %%a in ('set ##') do (
    set "$l=%%a"
    echo !$l:#=!,%%b
    )
)>destino.txt

0

In powershell is very simple.

$Entrada = 'C:\entrada.txt'
$Saída = 'C:\saída.txt'

Get-Content -LiteralPath $Entrada |
    Group-Object |
    Select-Object -Property @{
        Label = 'Coluna'
        Expression = { $PSItem.Name + ',' + $PSItem.Count }
        } |
    Select-Object -ExpandProperty 'Coluna' |
    Set-Content -LiteralPath $Saída -Force

-2


What you can do is scan the file line by line and where the lines are equal, increment a contactor and add it at the end of the file.

#!/bin/bash

qnt_de_linhas=$(cat arquivo_origem | wc -l)
linhas_contadas=1
for (( i = 1; i <= qnt_de_linhas; i++ ))
do
    qnt_linhas_iguais=0
    linha=$(sort arquivo_origem | sed -n $i'p')
    for (( j = "$linhas_contadas"; j <= qnt_de_linhas; j++ ))
    do
        outra_linha=$(sort arquivo_origem | sed -n $j'p')
        if [ "$linha" == "$outra_linha" ]
        then
            let "qnt_linhas_iguais++"
        else
            continue
        fi
    done
    linhas_contadas=$(($linhas_contadas+$qnt_linhas_iguais))
    echo "$linha, $qnt_linhas_iguais" | grep -v ", 0" > arquivo_final
done

This is a little script in bash(.sh) that can help you.

I know it’s not the language you asked for, but particularly I find bash more practical.

I hope it helped.

  • I like it, but I need it in Batch, to run on CMD.

  • Redo the script with the right commands... I did in bash Porq I don’t know batch hahhah

  • Thanks. For the help anyway.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.