Make a Shell Script bash that extracts to a new file all names and nr of women whose number begins with "91"

0

Consider that there is a file "file1.txt" which has for example:

Name of the person, Sex, Zip Code, Mobile Number, Mobile Phone Mark ...

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

If anyone knows thank you.

    
asked by anonymous 25.11.2015 / 15:12

3 answers

1

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

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

Unplugging the command:

cat file1.txt | = Play the contents of the file to be treated in awk

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

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

At this point the program filtered all the rows and only those containing the word "Female" in column two and the column of the phone start with "93" remain.

print $ 1, $ 4} '> newfile.txt = Prints only the name and phone columns in a file.

    
02.12.2015 / 21:48
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 Name , home with Female , ignores the Mobile numbers that start with 93

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

We separated the row into columns delimited by ; and chose the first and fourth columns.

    
26.11.2015 / 17:41
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
    
08.05.2016 / 09:38