简体   繁体   English

Linux Awk帮助代码

[英]Linux Awk help on code

I need to print the contents of a file, and give a title to each column, leaving enough space to be readable, and then I need to output this into a new file. 我需要打印文件的内容,并为每列提供标题,留下足够的空间以便可读,然后我需要将其输出到新文件中。 I followed this tutorial for a good while but I've gotten stuck. 我跟着这个教程好了一会儿但是我已经卡住了。

http://www.thegeekstuff.com/2010/01/awk-introduction-tutorial-7-awk-print-examples http://www.thegeekstuff.com/2010/01/awk-introduction-tutorial-7-awk-print-examples

This is the example code they use, which would give me exactly what I need to do with mine. 这是他们使用的示例代码,这将完全给我我需要做的事情。 But it will not work when I adjust it. 但是当我调整它时,它将无法工作。

$ awk 'BEGIN {print "Name\tDesignation\tDepartment\tSalary";}
{print $2,"\t",$3,"\t",$4,"\t",$NF;}
END{print "Report Generated\n--------------";
}' employee.txt

This is mine, as unlike the example, I want the whole document printed and don't really want this "report generated" nonsense under it. 这是我的,与示例不同,我希望打印整个文档,并且不希望这个“报告生成”无意义。 I tried adding {print;}' to the end after end, and made sure to start a new line and... nothing. 我尝试在结束后添加{print;}',并确保开始一个新行并且......没有。

$ awk 'BEGIN {Print "Firstname\tLastname\tPoints";} END > awktest.txt > done

Where have I gone wrong? 我哪里出错了? It keeps giving me the response Source line 2. 它不断给我回应源代码行2。

To remove the footline, just drop out anything starting from END till the closing ': 要删除边线,只需删除从END到结束的任何内容:

awk 'BEGIN {print "Name\tDesignation\tDepartment\tSalary";} {print $2,"\t",$3,"\t",$4,"\t",$NF;}' employee.txt

In your second example, you left out the closing ', and I suspect you put one more ">" than needed: 在你的第二个例子中,你忽略了结束',我怀疑你再多放一个“>”而不是需要:

awk 'BEGIN {print "Firstname\tLastname\tPoints";}' awktest.txt > done

The latter example will however silently ignore everything read from "awktest.txt". 然而,后一个例子将默默地忽略从“awktest.txt”读取的所有内容。

It seems you have missed out the actual printing of the columns and a source file, also I read that you don't need any END actions... 看来你错过了列的实际打印和源文件,我也读到你不需要任何END动作......

awk 'BEGIN {Print "Firstname\tLastname\tPoints";} END > awktest.txt > done`

Should be... 应该...

awk 'BEGIN {Print "Firstname\tLastname\tPoints";}{print $1,"\t",$2,"\t",$3;}' source_file.txt > awktest.txt`

Just remember to change the $1,$2,$3 to what columns on the source file you need. 只需记住将$ 1,$ 2,$ 3更改为您需要的源文件上的哪些列。

FYI. 仅供参考。 I'm no expert, just reading the tuts :) 我不是专家,只是阅读tuts :)

It looks like what you need is just to insert a header line, which can easily be done with sed (as well as awk ) or with cat 看起来你需要的只是插入一个标题行,可以用sed (以及awk )或者用cat轻松完成

$ sed '1iFirstname\tLastname\tPoints' file > output.file

or 要么

$ awk 'BEGIN{print "Firstname\tLastname\tPoints"} 1' file > output.file

or 要么

$ cat <(echo -e "Firstname\tLastname\tPoints") file > output.file

The awk print function is named print , not Print . awk打印功能名为print ,而不是Print idk why all the solutions are including ,"\\t", in their print statements. idk为什么所有的解决方案都包括在他们的打印声明中,"\\t", You don't want that - you want to set -v OFS='\\t' at the start of the script and then just use , between fields. 你不希望出现这种情况-你想设置-v OFS='\\t'在脚本的开始,然后只用,场之间。 All you want is: 你想要的只是:

awk -v OFS='\t' '
BEGIN {print "Name", "Designation", "Department", "Salary"}
{print $2, $3, $4, $NF}
}' employee.txt

assuming those are the correct field numbers you want to print from your data. 假设您要从数据中打印正确的字段编号。 Sample input/output in your question would be extremely useful to help us answer it. 您问题中的示例输入/输出对于帮助我们回答它非常有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM