首页 > 解决方案 > How to search a string containing with $ and ; in linux

问题描述

I ran a ldapsearch command and the output is redirected to a file(overwriting the same file every time the command is run) I have to search 3 strings, In which have 2 strings have $ and ; in it.

Contents in the file example.txt

<some lines above>
changenumber;demo$host-example_good1$fine-example_good2
changenumber;echo$fine-example_good2$host-example_good1
changenumber;echo$host-example_good1$fine-example_good2
changenumber;demo$fine-example_good2$host-example_good1
<some lines below>
<end of file>

Tried below commands

awk -F";|$" '/echo$host-example_good1$fine-example_good2'/ example.txt

awk -F"[$;]" '/demo$host-example_good1$fine-example_good2'/ example.txt

Output: nothing is displayed

Expected output

changenumber;demo$host-example_good1$fine-example_good2
changenumber;echo$host-example_good1$fine-example_good2

标签: unixawk

解决方案


$在正则表达式中有特殊含义,需要转义。

awk -F'[;$]' '/demo\$host-example_good1\$fine-example_good2/' example.txt

转到regular-expressions.info阅读有关正则表达式的教程。


推荐阅读