# Extract file records based on some field conditions

**URL:** <https://community.unix.com/t/extract-file-records-based-on-some-field-conditions/278178>\
**Category:** Shell Programming and Scripting\
**Created:** [November 29, 2010, 11:44pm UTC](https://community.unix.com/t/extract-file-records-based-on-some-field-conditions/278178 "2010-11-29T23:44:56Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![mehimadri](https://community.unix.com/letter_avatar/mehimadri/32/5_5575768a8748004e209b776fc1b2916d.png) [@mehimadri](https://community.unix.com/u/mehimadri)\
**Post date:** [November 29, 2010, 11:44pm UTC](https://community.unix.com/t/extract-file-records-based-on-some-field-conditions/278178/1 "2010-11-29T23:44:56Z")

</div>

Hello Friends,

I have a file(InputFile.csv) with the following columns(the columns are pipe-delimited):  
ColA|ColB|ColC|ColD|ColE|ColF

Now for this file, I have to get those records which fulfil the following condition:  
If "ColB" is NOT NULL and "ColD" has values one of the following ('A','B','C') the show the records

If the aforementioned condition is satisfied then I need to have those records only in a separate file.

Please help me in this! Thanks in Advance!!

with warm regards

---

<div class="post-metadata">

**Author:** ![kevintse](https://community.unix.com/user_avatar/community.unix.com/kevintse/32/1921_2.png) [@kevintse](https://community.unix.com/u/kevintse)\
**Post date:** [November 30, 2010, 12:03am UTC](https://community.unix.com/t/extract-file-records-based-on-some-field-conditions/278178/2 "2010-11-30T00:03:52Z")

</div>

> [@](#):
>
> awk -F"|" ' $2!="" && $4 ~ /\[1\]$/ ' InputFile.csv \> outfile

* * *

1. ABC

---

<div class="post-metadata">

**Author:** ![mehimadri](https://community.unix.com/letter_avatar/mehimadri/32/5_5575768a8748004e209b776fc1b2916d.png) [@mehimadri](https://community.unix.com/u/mehimadri)\
**Post date:** [November 30, 2010, 1:14am UTC](https://community.unix.com/t/extract-file-records-based-on-some-field-conditions/278178/3 "2010-11-30T01:14:46Z")

</div>

Hello kevintse,

Sorry I failed to mentioned that the values I have to check are not 'A','B','C' but string values like 'Asia','Casablanca','Brando'

So the condition should be:  
If "ColB" is NOT NULL and "ColD" has values one of the following ('Asia','Casablanca','Brando') the show the records

Sorry for the goof-up.  
In this case how will teh awk command be..

Thanks in Advance!!

---

<div class="post-metadata">

**Author:** ![Chubler\_XL](https://community.unix.com/user_avatar/community.unix.com/chubler_xl/32/2077_2.png) [@Chubler\_XL](https://community.unix.com/u/Chubler_XL)\
**Post date:** [November 30, 2010, 1:34am UTC](https://community.unix.com/t/extract-file-records-based-on-some-field-conditions/278178/4 "2010-11-30T01:34:13Z")

</div>

```nohighlight
awk -F"|" 'BEGIN { B["Asia"]=B["Casablanca"]=B["Brando"]=1; } $2 && ($4 in B) ' InputFile.csv > outfile

```

If you have many more than 3 criteria (our you may change them) you should consider loading the criteria from another file:

```nohighlight
awk -F"|" 'NR==FNR { B[$1]=1; Next } $2 && ($4 in B) ' CritFile.txt InputFile.csv > outfile

```

---

<div class="post-metadata">

**Author:** ![mehimadri](https://community.unix.com/letter_avatar/mehimadri/32/5_5575768a8748004e209b776fc1b2916d.png) [@mehimadri](https://community.unix.com/u/mehimadri)\
**Post date:** [November 30, 2010, 2:16am UTC](https://community.unix.com/t/extract-file-records-based-on-some-field-conditions/278178/5 "2010-11-30T02:16:47Z")

</div>

Hi,

When I tried the command I received the following error:

$ cat InputFile.csv  
ColA|ColB|ColC|ColD|ColE|ColF  
1|A|B|Asia|E|F  
1||C|Asia|E|F  
2|B|C|Casablanca|E|F  
3|B|C|Brando|E|F

$ awk -F"|" 'BEGIN { B["Asia"]=B["Casablanca"]=B["Brando"]=1; } $2 && ($4 in B) ' InputFile.csv \> outfile  
awk: syntax error near line 1  
awk: bailing out near line 1

Please help me in this. Thanks in Advance!!

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [November 30, 2010, 2:46am UTC](https://community.unix.com/t/extract-file-records-based-on-some-field-conditions/278178/6 "2010-11-30T02:46:54Z")

</div>

```nohighlight
nawk -F\| '$2&&$4~/^(Brando|Casablanca|Asia)$/' InputFile.csv > outfile

```

---

<div class="post-metadata">

**Author:** ![ygemici](https://community.unix.com/letter_avatar/ygemici/32/5_5575768a8748004e209b776fc1b2916d.png) [@ygemici](https://community.unix.com/u/ygemici)\
**Post date:** [November 30, 2010, 5:27am UTC](https://community.unix.com/t/extract-file-records-based-on-some-field-conditions/278178/7 "2010-11-30T05:27:56Z")

</div>

```nohighlight
x=($(sed '=' infile | sed -n 'N;s/\n/ /;p'|sed -n 's/[^]|*[^|]*|*[^]|*\(Asia\|Casablanca\|Brando\).*/\1/p'|sed 's/[^0-9]\|//g;/^$/d') )
for i in ${x[@]} ; do sed -n "$i p" infile ; done
1|A|B|Asia|E|F
2|B|C|Casablanca|E|F
3|B|C|Brando|E|F

```

---

<div class="post-metadata">

**Author:** ![rdcwayx](https://community.unix.com/letter_avatar/rdcwayx/32/5_5575768a8748004e209b776fc1b2916d.png) [@rdcwayx](https://community.unix.com/u/rdcwayx)\
**Post date:** [November 30, 2010, 5:42am UTC](https://community.unix.com/t/extract-file-records-based-on-some-field-conditions/278178/8 "2010-11-30T05:42:46Z")

</div>

> [@scrutinizer](#):
>
> ```plaintext
> nawk -F\| '$2&&$4~/^(Brando|Casablanca|Asia)$/' InputFile.csv > outfile
> 
> ```

I knew you try to short the code, but there is a bug in it, if $2=0, it is not null 🙂

$ cat infile  
ColA|0|ColC|Asia|ColE|ColF

$ nawk -F\| '$2&&$4~/^(Brando|Casablanca|Asia)$/' infile

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [November 30, 2010, 5:44am UTC](https://community.unix.com/t/extract-file-records-based-on-some-field-conditions/278178/9 "2010-11-30T05:44:57Z")

</div>

Well spotted.. Thanks

```nohighlight
nawk -F\| '$2""&&$4~/^(Brando|Casablanca|Asia)$/' InputFile.csv > outfile

```

---

<div class="post-metadata">

**Author:** ![Chubler\_XL](https://community.unix.com/user_avatar/community.unix.com/chubler_xl/32/2077_2.png) [@Chubler\_XL](https://community.unix.com/u/Chubler_XL)\
**Post date:** [November 30, 2010, 12:22pm UTC](https://community.unix.com/t/extract-file-records-based-on-some-field-conditions/278178/10 "2010-11-30T12:22:44Z")

</div>

> [@rdcwayx](#):
>
> I knew you try to short the code, but there is a bug in it, if $2=0, it is not null 🙂

The original requirement was for 2nd field is non-null, lol
