Make a shell script bash that extracts to a new file all names and nr of women whose number starts with "91"

Asked

Viewed 280 times

0

Consider that there is a "file1.txt" file that you have for example:

Name of the person;Sex;Postal code;Cell phone number;Brand of the Cell Phone ...

How do I write a shell script bash that extracts to a new file all the names and phone numbers of all female people whose mobile phone number starts with "93".

If anyone knows, thank you.

  • You can add examples of the lines in this file?

3 answers

1

Another way and that you can adapt in other cases, would be using the awk.

cat ficheiro1.txt|awk -F ";" '{if (($2 == "Feminino") && ($4 ~ /^93/)) print $1,$4}' > novoficheiro.txt

Unlocking the remote control:

cat filer 1.txt | = Play the contents of the file to be handled in awk

awk -F ";" = Separates the columns of your file into fields ($1,$2,$3...) using as delimiter the ;

'{if (($2 == "Female") && ($4 ~ "93")) = Creates a condition where the second column ($2) will be positive if you have the word "Female" and if the fourth column ($4) starts with "93".

At that moment the program filtered all the lines and only the ones that contain the word "Feminine" in column two and that the telephone column starts with "93".

print $1,$4}' > novoficheiro.txt = Print only columns of name and phone in a file.

  • 2

    Cool (+1) already a quick simpilifcaçao: awk -F";" '$2 == "Feminino" && $4 ~ /^93/ { print $1,$4}' file.txt > out.txt

  • 1

    Good! I didn’t even know about this simplification of If no awk.

0

Only awk v1:

$ awk 'BEGIN{FS=";"}{if($2=="Feminino" && index($4,"93")==1) print $1,$4}' ficheiro1.txt

Only awk v2:

$ awk -F";" '$2=="Feminino" && $4~/^93/ {print $1,$4}' ficheiro1.txt

Using grep and cut:

$ grep '^[^;]*;Feminino;[^;]*;93' ficheiro1.txt | cut -d ';' -f 1,4

0

cat ficheiro1.txt | grep '^[^;]*;Feminino;[^;]*;93' | cut -d ';' -f 1,4 > saida.txt

Breaking:

grep '^[^;]*;Feminino;[^;]*;93'

It is a regular expression that ignores the Person’s name, house with Feminine, ignores the Postal Code and finally marries with Mobile phone numbers that begin with 93

cut -d ';' -f 1,4 > saida.txt

We separate the row into columns delimited by ; and choose the first and fourth column.

Browser other questions tagged

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