简体   繁体   English

从文件中的特定字段不以某些内容开头的行中打印

[英]Printing lines from a file where a specific field does not start with something

I want to print all the lines where 3rd field (fields separated by : ) DO NOT start with # (to signify that 3rd field is a comment). 我要打印第三行(以:分隔的字段)的所有行,不要以#开头(以表示第三行是注释)。 Please note that there may be space(s) between : and #. 请注意,:和#之间可能有空格。

Example Input: 输入示例:

A:B:#hdfghdfg A:B:#hdfghdfg

A:B: #dfdfdfg A:B:#dfdfdfg

A:B:C A:B:C

Desired output: 所需的输出:

A:B:C A:B:C

I tried: 我试过了:

awk -F : '$3 ~ /^#/ { print }' run_out5 > run_out6 awk -F:'$ 3〜/ ^#/ {print}'run_out5> run_out6

but it is not working 但它不起作用

Thanks, 谢谢,

Jagrati Jagrati

The regex could be a tiny bit cleaner: 正则表达式可能会更干净一点:

awk -F: '$3 !~ /^ ?#/ { print }'

It's often better to expect repeated whitespace (space or tab) rather than a single space character, which can look identical in printed output. 通常最好期待重复的空格(空格或制表符),而不是单个空格字符,后者在打印输出中看起来相同。

awk -F: '$3 !~ /^[[:space:]]*#/ { print }'

Use !~ to select lines that do not match the regexp. 使用!~选择与正则表达式不匹配的行。
Adjust the regexp so that it will match fields with leading spaces. 调整正则表达式,使其与前导空格匹配。

awk -F : '$3 !~ /^ *#/ {print}'

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

相关问题 从匹配特定字段的文件中打印行 - Printing lines from a file where a specific field is matched 从文件夹中的所有文档中打印特定行 - Printing specific lines from all documents in a folder 如何在 Linux/Unix 终端中开始和停止打印带有特定文本的行 - How to Start and Stop printing lines with Specific Text in Linux/Unix Terminal 在bash / linux中,尝试从文件中满足特定条件的文件中删除以特定字符串开头的行 - Trying to delete lines beginning with a specific string from files where the file meets a target condition, in bash/linux gdb从哪里获取代码行? - From where does gdb take the code lines? 从文件中过滤具有给定前缀的特定行 - Filter specific lines with a given prefix from a file 从Bash中具有特定模式的文件中获取行 - Get lines from a file with a specific pattern in Bash 如何仅恢复特定文件中的特定行? - How do I revert only specific lines from a specific file? 如何在执行cgi时使apache跳过特定的stdout行或停止从打印到stdout的共享库 - How to make apache skip specific stdout lines when executing cgi or stop shared library from printing to stdout 用于在文件中打印行数和列数的代码。 为什么它在Windows Mingw gcc环境下却不能在Linux上工作? - Code for printing Number of Lines and Columns in a File. Why does it work on Windows Mingw gcc environment but not on Linux?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM