Copy one line at a time from the source file to the target files

Asked

Viewed 117 times

2

A new need arose accompanied by a good and new question that until then did not find anything similar on the internet - Copy a single line at a time to a different single 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 only need to get ten, so I limit the variable total=10

That will make the for traverse 10x determined by seq, interacting about i

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

Dai destination files are being created in ordinal numbers - 1 2 3 4 5 6 7 8 9 ...

Then we’d have to copy first line to first file, copy second line to second file and so on.

Concluding - make a copy of a name for a single file.

I know I’ll have to wear one increment and tals, but I don’t know how.

  • Barbadinha, I will write the script and then put it here for you. These the contents of these lines have space between the words? Give me an example of line.

4 answers

1

Using for gets 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

1


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

#!/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 the sed because I believe it is the best option for the action you want, because it takes all the content of the line, whether or not it has special characters. Contents of the file I used was based on the suggestion of Tom Melo but with spaces in 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

Upshot:

~# 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
...

1

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 awk:

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

Just adapt the script to save in the desired directory.

0

I made it with bow tie while, I leave here as a record for future consultation. 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 for testing:

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
  • You want to wear a loop Okay, but put a break,Exit in his if. Suppose your file has 1000 lines, yours loop will get the first 10, and the script will continue running until you make the 1000 lines.

  • 1

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

Browser other questions tagged

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