开发者

grep command line, linux

开发者 https://www.devze.com 2023-02-07 20:44 出处:网络
i\'m trying to isolate all the server names (i.e 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

i'm trying to isolate all the server names (i.e 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开发者_StackOverflow中文版 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":"'


Use single quote when dealing with strings containing double quotes :

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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号