Make one copy at a time from the source file, to the destination files

2

A new need arose accompanied by a good new question that until then I did not find anything similar on the internet - Copy a single line at a time, for a single different file .

Example for each new line to be copied:

total=10

linha='cat Arquivo_de_origem'

for i in 'seq $total'
    echo "$linha" > /home/$USER/$i
done

The source file contains more than 10 lines, but I just need to get ten, so I give the limit on the variable total=10

This will cause for to go 10x determined by seq , interacting with i

The echo followed by the variable linha establish line-by-line

asked by anonymous 02.02.2018 / 13:36

4 answers

1

Diego, sorry for the delay. but I was offline these days. Follow my response.

#!/bin/bash
# Limite que será usado no for
limit=10
# Diretório de destino dos arquivos
dir_dest="/home/mkt/tmp/"
# Arquivo de origem
arq_orig="/home/mkt/tmp/tst"
# Conteúdo do arquivo
arq=$(cat $arq_orig)
for (( i = 0; i < $limit; i++ )); do
    # Parametro de linha que será usado com o sed
    linha=$[i + 1]
    # Finalmente criando o arquivo tendo seu conteúdo cada
    # cada linha do arquivo de origem
    echo "$arq" | sed -n "$linha"p > $dir_dest/$i.txt
done

I used sed because I believe it is the best option for the action you want, because it takes all the contents of the line, regardless of whether or not you have special characters. The content of the file I used was based on the suggestion of Tom Melo but with spaces on certain lines.

~#cat tst 
Rogério : Amélia
Cleiton:Bruna
Mauro : Carla
Diego:Denise
Maicon : Emiliane
Delvair:Fátima
José:Graça
Marcos:Helena
Neto : Irene
Tiago:Júlia

Result:

~# ls -1
0.txt
1.txt
2.txt
3.txt
4.txt
5.txt
6.txt
7.txt
8.txt
9.txt

Contents of each file created by the script:

~# cat 0.txt
Rogério : Amélia
~# cat 1.txt
Cleiton:Bruna
~# cat 2.txt
Mauro : Carla
...
    
06.02.2018 / 14:28
1

Using for looks like this:

#!/bin/sh
NUM=0
MAX=10
# Caso tenha Nome e Sobrenome por exemplo: Ana Paula
IFS=$'\n'
for LINHA in $(cat lista-casais.txt)
do
    if [ $NUM -lt $MAX ]; then
        M=$(echo $LINHA | cut -d ':' -f1)
        F=$(echo $LINHA | cut -d ':' -f2)
        echo -ne "Masculino:$M\nFeminino:$F" > /home/$USER/noivos/$NUM
        NUM=$((NUM+1))
    fi
done

Reference

02.02.2018 / 15:04
1

Starting from the point that the file has the following format:

Rogério:Amélia
Cleiton:Bruna
Mauro:Carla
Diego:Denise
Maicon:Emiliane
Delvair:Fátima
José:Graça
Marcos:Helena
Neto:Irene
Tiago:Júlia

We can use the head + awk :

head -n 10 casais.txt  | awk -F ":" '{print "Masculino:"$1"\nFeminino:"$2"" > ""NR".txt"}'

Or a simplified version with only awk :

awk -F ":" '{if(NR<=10) print "Masculino:"$1"\nFeminino:"$2"" > ""NR".txt"}' casais.txt

Just adapt the script to save to the desired directory.

    
02.02.2018 / 17:30
0

I made the loop while , I leave here as a record for future reference. See:

bourne shell

#!/bin/sh

num=0

cat lista-casais.txt | while read LINHA
do

  M=$(echo $LINHA | cut -d ':' -f1)
  F=$(echo $LINHA | cut -d ':' -f2)

if [ $num -lt 10 ]
then
    echo -ne "Masculino:$M\nFeminino:$F" > /home/$USER/noivos/$num
    num=$[num + 1]
fi

  let num=$[num + 1]

done &>/dev/null

Small sample list to test:

Rogério:Amélia
Cleiton:Bruna
Mauro:Carla
Diego:Denise
Maicon:Emiliane
Delvair:Fátima
José:Graça
Marcos:Helena
Neto:Irene
Tiago:Júlia
    
02.02.2018 / 14:48