简体   繁体   中英

Manipulate text Files with bash script

Create an ex2 script that using the ./it/colaboradores/users.txt file will create a "XXX" file for each employee in the ./it/colaboradores/LDAP/ folder, with "XXX" as its initial. Within each file it should contain the following text:

First line example:

$ cat TES
Nome: TesteTeste
Email: teste.teste@xxx.pt
Numero de telemovel: 934318232
Skype: teste.cvs

This is my code so far:

#!/bin/bash

input="$HOME/folha1/it/colaboradores/users.txt"
out="$HOME/folha1/it/colaboradores/LDAP/teste.txt"

while read line; do

        Acr="$(awk -F ';' '{print $1}' <<<"$line")"
        Nome="$(awk -F ';' '{print $2}' <<<"$line")"
        Email="$(awk -F ';' '{print $3}' <<<"$line")"
        Numero="$(awk -F ';' '{print $4}' <<<"$line")"
        Skype="$(awk -F ';' '{print $5}' <<<"$line")"

        cat  <<-!>> "$out"

       -------------------------------------------

        Acr: $Acr
        Nome: $Nome
        Email: $Email
        Numero: $Nr
        Skype: $Skype
        !

done < $input

In my code i test if i can manipulate , and my output for this code are :

-------------------------------------------

#Acr: TES
Nome: TesteTeste
Email: teste.teste@xxx.pt
Numero de telemovel: 934318232
Skype: teste.cvs

       -------------------------------------------

#Acr: XXX
Nome: PauloTeste
Email: paulo.teste@teste.pt
NR: 323322323
Skype: paulo.teste

       -------------------------------------------

And my dificult it's to generate one file for each person.. and when i start my script i put example:

./ex02 
cat TES 

the output is:

Nome: TesteTeste
Email: teste.teste@xxx.pt
Numero de telemovel: 934318232
Skype: teste.cvs

but in one file with the file name are the firts 3 letter.txt. (e TES.txt/XXA.txt)

i know it's with maybe a cicle for.. but i dont know really how to procede.

my original txt it's something like that:

XXA;Teste;Teste.silva@Teste.pt;32323232;Teste.cs
XXB;Example;tes.te@Teste.pt;323232323;Teste.alx
XXC;Example;teste.te@Teste.pt;2323232;Teste.me2
XXD;ExamplesXamples;Joao.teste@Teste.pt;3232323;Teste
XXE;ExamplesXam;Pedro.teste@Teste.pt;32323232;Teste.rodrigues

what i want to do with the script is create a file for each person like that

file tes.txt

#Acr: TES
Nome: TesteTeste
Email: teste.teste@xxx.pt
Numero de telemovel: 934318232
Skype: teste.cvs



file xxx.txt 

#Acr: XXX
Nome: PauloTeste
Email: paulo.teste@teste.pt
NR: 323322323
Skype: paulo.teste

Try:

input="$HOME/folha1/it/colaboradores/users.txt"
out="$HOME/folha1/it/colaboradores/LDAP/"

while IFS=';' read -r Act Nome Email Numero Skype; do
     cat  << EOF >> "$out"/"$Nome".txt
Acr: $Acr
Nome: $Nome
Email: $Email
Numero: $Numero
Skype: $Skype
EOF
done < "$input"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM