'How to check any pipe separated data is empty
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 1:[1]
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
Solution 2:[2]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | anubhava |
| Solution 2 | KamilCuk |
