我有以下输入文件,需要找到哪个字段为空并显示键列和空值列名。
注意:未来可能会添加新字段。
INPUT.TXT
// Log logged in user's API requests
module.exports = (app) => {
app.remotes().before('**', async (ctx) => {
const { accessToken } = ctx.req;
// ignore unauthenticated requests
if (!accessToken) return;
const user = await accessToken.user.get();
console.log(`${user.email} => ${ctx.req.url}`);
});
};
ExpectedOutput.txt:
Keyfeild1|Over|Loan|cc|backup
200|12||0|
100||15|1|200
100|100|100|100|100
50||50||11
使用的命令:
200|Loan
200|backup
100|Over
50|Over
50|cc
达到产出:
{cat Input.txt | awk -F"|" '{for(i=1;i<=NF;i++) if($i=="") { print $1"|"i} }'}
答案 0 :(得分:1)
关注awk
可能对您有帮助。
awk -F"|" 'FNR==1{for(i=1;i<=NF;i++){a[i]=$i};next} {for(i=2;i<=NF;i++){if($i==""){print $1,a[i]}}}' OFS="|" Input_file
现在添加一种非单线形式的解决方案:
awk -F"|" '
FNR==1{
for(i=1;i<=NF;i++){
a[i]=$i};
next
}
{
for(i=2;i<=NF;i++){
if($i==""){
print $1,a[i]}}
}
' OFS="|" Input_file
输出如下。
200|Loan
200|backup
100|Over
50|Over
50|cc
答案 1 :(得分:1)
Awk
解决方案:
awk 'BEGIN{ FS=OFS="|" }
NR==1{ split($0, a); next }
{ for(i=2; i<=NF; i++) if ($i=="") print $1,a[i] }' file
输出:
200|Loan
200|backup
100|Over
50|Over
50|cc