简体   繁体   中英

Find a word in a file and print it in bash

I have a file in /home/{user}/client.log .

clinet.log contains a work lcid .

How can I find a word lcid in a file client.log and print it using bash.

use the following command: 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
  • s -- substitute
  • .* -- match anything
  • lcid -- literal match
  • . -- match any single character
  • : -- literal match
  • . -- match any one character
  • \\( -- begin capture group one
  • \\w* -- any amount of word characters
  • \\) -- end capture group one
  • . -- match any single character
  • \\1 -- replace with capture group one
  • p -- print it

Let me know if this helps.

Thanks all. I got this working by using the following command

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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