Sunday, September 4, 2022

[SOLVED] How to check any pipe separated data is empty

Issue

I am trying to implement this but not getting

I have below data in file

A|B|C|D
AX | GH | H
FG |HJJK | 
  | GHJ | 
AG |

I need to check like line should have data in between pipe

Expected output

Line 1 : as proper data 
Line 2 : as proper data 
Line 3 : No proper data 
Line 4 : No proper data 
Line 5 : No proper data 

I am trying to frame code for it

IFS=''
    while -r data
    do 
       var1=`echo "$data" | awk -F'|' '{print $1}'
       var2=`echo "$data" | awk -F'|' '{print $2}'
       var3=`echo "$data" | awk -F'|' '{print $3}'
       if [ ! -z $var1  ]  && [! -z $var2] && [! -z $var3]
       then
            echo "as poper data"
       else
            echo "No proper data"
       fi  
    done < file.txt

How to write a generic code it should work for any file of data if it as pipe separated data .

Draw back of my code and if their are 3 columns I need to create 3 var variables


Solution

A|B|C|D
AX | GH | H
FG |HJJK | 
  | GHJ | 
AG |

———————————————————————

{m,g}awk '$-_=sprintf("Line %5.f : %s proper data",
                       NR, (!_)< NF ? "No" : "as")' \
                       FS='(^|[|])[ \t]*[|]|[|][ \t]*$'

———————————————————————

Line     1 : as proper data
Line     2 : as proper data
Line     3 : No proper data
Line     4 : No proper data
Line     5 : No proper data

————————————————————

VERSION WITH EXIT CODE

{m,g}awk '!_<NF { exit !_ } !_' FS='(^|[|])[ \t]*[|]|[|][ \t]*$'

OUTPUT

A|B|C|D
AX | GH | H

EXIT CODE

echo "$?"

1


Answered By - RARE Kpop Manifesto
Answer Checked By - Gilberto Lyons (WPSolving Admin)