maihabunash
maihabunash

Reputation: 1702

cut words from csv file to text file only if the second field in csv is yes

I have the following csv file, file.csv:

1,yes,yes,customer1
2,no,yes,customer5
3,yes,yes,customer11
4,no,no,customer14
5,yes,no,customer15
6,yes,yes,customer21
7,no,yes,customer34
8,no,yes,customer89

I want to convert the csv file to the following format, new_file.txt

the first field and the four field should be taken from the csv line only if the second field in the csv is "yes"

Line Number 1 Customer customer1
Line Number 3 Customer customer11
Line Number 5 Customer customer15
Line Number 6 Customer customer21 

Please advice how to convert the csv to txt (new_file.txt) with awk , sed , or perl one liner , etc

Upvotes: 0

Views: 120

Answers (2)

mpapec
mpapec

Reputation: 50667

Perl one liner:

perl -F, -lanE 'say "Line number $F[0] customer $F[3]" if $F[1] eq "yes"' file
Line number 1 customer customer1
Line number 3 customer customer11
Line number 5 customer customer15
Line number 6 customer customer21

Switches:

  • -F: split() pattern for -a switch
  • -l: Enable line ending processing
  • -a: Splits the line on space and loads them in an array @F
  • -n: Creates a while(<>){...} loop for each “line” in your input file.
  • -E: Tells perl to execute the code on command line like -e, but enables all optional features

Upvotes: 2

fedorqui
fedorqui

Reputation: 290165

This makes it:

$ awk -F, '$2=="yes" {printf "Line number %d customer %s\n", $1, $4}' file
Line number 1 customer customer1
Line number 3 customer customer11
Line number 5 customer customer15
Line number 6 customer customer21

It sets the comma as field separator and checks if the 2nd field is exactly "yes". In that case, prints the desired output.

In case you want this checking to be not case sensitive, you have two options:

Use regex:

awk -F, '$2~/^[yY][eE][sS]$/'

Use IGNORECASE:

awk -F, 'BEGIN{IGNORECASE=1} $2=="yes"' a

All together:

awk -F, 'BEGIN{IGNORECASE=1} $2=="yes" {printf "Line number %d customer %s\n", $1, $4}' file

Test

$ cat a
1,yes,yes,customer1
2,no,yes,customer5
3,YES,yes,customer11
4,no,no,customer14
5,yeS,no,customer15
6,yEs,yes,customer21
7,no,yes,customer34
8,no,yes,customer89
$ awk -F, '$2~/^[yY][eE][sS]$/ {print $2}' a
yes
YES
yeS
yEs
$ awk -F, 'BEGIN{IGNORECASE=1} $2=="yes" {print $2}' a
yes
YES
yeS
yEs

See Gawk manual --> 3.6 Case Sensitivity in Matching for further info.

Upvotes: 3

Related Questions