简体   繁体   中英

grep command line, linux

i'm trying to isolate all the server names (ie sv012-cali) from within an html file "Servernick":"sv012-cali" in the line code below I think its the quotation mark that's throwing it off

cat smtest.html | tr '"' '\\n' | grep "^Servernick":"" | cut -d '"' -f 2

snippet of the html data file "Relation":0},{"ID":415804","Servernick":"sv012-cali","Level":"3"

You need to escape the quotes. In bash use \\ to escape characters:

grep "^Servernick\":\""

or alternatively put your double quotes within single quotes:

grep '^servernick":"'

处理包含双引号的字符串时,请使用单引号:

grep '^Servernick":"'

Updated after OP provided sample data -

# cat test.data                                                  
"Relation":0},{"ID":415804","Servernick":"sv012-cali","Level":"3"
"Relation":0},{"ID":415804","Servernick":"sv012-balh","Level":"3"
# cat test.data | tr "," "\n" | grep Servernick | cut -d '"' -f 4
sv012-cali
sv012-balh

Original reply -

Is this what you need?

# echo \"Servernick\":\"sv012-cali\"  > test.data  
# cat test.data                                  
"Servernick":"sv012-cali"
# cat test.data  | tr '"' '\n'                                          

Servernick
:
sv012-cali

# cat test.data  | tr '"' '\n' | egrep -v "Servernick|:|^$"
sv012-cali

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