srimal
October 21, 2009, 5:37am
1
hi all,
I have this file [myfile.txt] with some user data.
example:
$cat myfile.txt
FName|LName|Gender|Company|Branch|Bday|Salary|Age
aaaa|bbbb|male|cccc|dddd|19900814|15000|20|
eeee|asdg|male|gggg|ksgu|19911216|||
aara|bdbm|male|kkkk|acke|19931018||23|
asad|kfjg|male|kkkc|gkgg|19921213|14000|24|
aera|bprb|male|cccc|pppp||15000|20|
.
.
. // and so on
So what I want to do is to take out (to a file) the missing fields as following format:
<FName> <LName> <Company> Missing Field/s:<> <>
example output:
eeee asdg gggg Missing Field/s: Salary Age
aara bdbm kkkk Missing Field/s: Salary
CAN ANYONE HELP ME PLEASE?
grep \|\| file > new_file
srimal
October 21, 2009, 7:44am
3
hi all,
I found the answer.Go through the link
And thank you danmero for U'r co-operation.
Another one:
awk -F"|" '
BEGIN{h="FName|LName|Gender|Company|Branch|Bday|Salary|Age";split(h,a,"|")}
{
s=$1" "$2" "$4" Missing Field/s: "
for(i=1;i<NF;i++) {
if (!$i) {s=s" "a;f=1}
}
}
f{print s;f=0}' file
This is the output:
$ cat file
FName|LName|Gender|Company|Branch|Bday|Salary|Age
aaaa|bbbb|male|cccc|dddd|19900814|15000|20|
eeee|asdg|male|gggg|ksgu|19911216|||
aara|bdbm|male|kkkk|acke|19931018||23|
asad|kfjg|male|kkkc|gkgg|19921213|14000|24|
aera|bprb|male|cccc|pppp||15000|20|
$
$ awk -F"|" '
BEGIN{h="FName|LName|Gender|Company|Branch|Bday|Salary|Age";split(h,a,"|")}
{
s=$1" "$2" "$4" Missing Field/s: "
for(i=1;i<NF;i++) {
if (!$i) {s=s" "a;f=1}
}
}
f{print s;f=0}' file
eeee asdg gggg Missing Field/s: Salary Age
aara bdbm kkkk Missing Field/s: Salary
aera bprb cccc Missing Field/s: Bday
$
while(<DATA>){
if($. == 1){
my @tmp=split("[|]",$_);
for(my $i=0;$i<=$#tmp;$i++){
$hash{$i}=$tmp[$i];
}
}
else{
if(/[|][|]+/){
my @tmp=split("[|]",$_);
print $tmp[0]," ",$tmp[1]," ",$tmp[3]," Missing Field/s:";
for(my $i=0;$i<=$#tmp;$i++){
print $hash{$i}," " if $tmp[$i] eq "";
}
print "\n";
}
}
}
__DATA__
FName|LName|Gender|Company|Branch|Bday|Salary|Age|
aaaa|bbbb|male|cccc|dddd|19900814|15000|20|
eeee|asdg|male|gggg|ksgu|19911216|||
aara|bdbm|male|kkkk|acke|19931018||23|
asad|kfjg|male|kkkc|gkgg|19921213|14000|24|
aera|bprb|male|cccc|pppp||15000|20|