Batch Programming

1

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

Source file:

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

Target File:

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

Add the repetitions and put in front of the item after a comma. I use to balance items in store, thank you to anyone who can help. I was able to do the batch to include ", 1" after each bar code 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
)

But I want to improve it, to decrease the size of lines that is generated by all bar codes, making a count of the same codes and putting the total in front of the code, but I do not know where to start.

    
asked by anonymous 12.07.2018 / 12:37

2 answers

0

The best solution in this case !!

source.txt and destination.txt code in the name of your real files!

@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
    
17.07.2018 / 00:33
-1

What you can do is scan the file line by line and where the lines are the same, 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 small script in bash (.sh) that can help you.

I know it's not the language you requested, but I particularly find bash more practical.

I hope you have helped.

    
12.07.2018 / 20:56