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
CodePudding user response:
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
CodePudding user response:
Just grep for a pipe in front of the line, pipe on the end of line, or pipe with nothing in between.
if grep -q '^[[:blank:]]*|\||[[:blank:]]*$\||[[:blank:]]*|' file.txt; then
echo "as poper data"
else
echo "No proper data"
fi
CodePudding user response:
An easier to understand awk
:
awk -F '|' '
{
for (i=1; i<=NF; i)
if ($i ~ /^[[:blank:]]*$/) {
print "Line", NR, ": No proper data"
next
}
print "Line", NR, ": as proper data"
}' file
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