简体   繁体   English

在文件中查找单词并用bash打印

[英]Find a word in a file and print it in bash

I have a file in /home/{user}/client.log .我在/home/{user}/client.log有一个文件。

clinet.log contains a work lcid . clinet.log包含一个工作lcid

How can I find a word lcid in a file client.log and print it using bash.如何在文件client.log找到单词lcid并使用 bash 打印它。

use the following command: grep "lcid" . -R使用以下命令: grep "lcid" . -R grep "lcid" . -R

Okay, with the additional information you provided this will do the trick for you:好的,使用您提供的其他信息,这将为您解决问题:

sed -n "s/.*lcid.:.\(\w*\)./\1/p" client.log
  • -n -- don't print -n -- 不打印
  • s -- substitute s——替代
  • .* -- match anything .* -- 匹配任何东西
  • lcid -- literal match lcid -- 文字匹配
  • . . -- match any single character -- 匹配任意单个字符
  • : -- literal match : -- 文字匹配
  • . . -- match any one character -- 匹配任意一个字符
  • \\( -- begin capture group one \\( -- 开始捕获第一组
  • \\w* -- any amount of word characters \\w* -- 任意数量的单词字符
  • \\) -- end capture group one \\) -- 结束捕获组一
  • . . -- match any single character -- 匹配任意单个字符
  • \\1 -- replace with capture group one \\1 -- 替换为捕获组一
  • p -- print it p——打印出来

Let me know if this helps.如果这有帮助,请告诉我。

Thanks all.谢谢大家。 I got this working by using the following command我通过使用以下命令得到了这个工作

grep 'lcid' client.log | grep 'lcid' client.log | cut -d'"' -f4剪切 -d'"' -f4

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

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