简体   繁体   English

需要使用awk匹配在文件中查找字符串

[英]Need to find string in file using awk match

Ii have javascript file which contains following 我有一个JavaScript文件,其中包含以下内容

    ..............
    templateUrl: 'views/product.html'
    url: '/categories',
    templateUrl: 'views/categories.html'
    url: '/report',
    templateUrl: 'views/report.html'
    url: '/publisher',
    templateUrl: 'views/publisher.html'
    url: '/manageoutlet',
    templateUrl: 'views/outlet.html'
    url: '/order',
    templateUrl: 'views/order.html'
    url: '/mobileapp',
    templateUrl: 'views/mobileapp.html'
    url: '/cartitem/:id',
    .....
    .....
    'url': 'http://fhdjhjd/fkdf',
    ........
    ........

I need to get the line 'url': 'http://fhdjhjd/fkdf', from the file using awk , for that am using the following code 我需要使用awk从文件中获取'url': 'http://fhdjhjd/fkdf', ,因为那是使用以下代码

sudo awk '{
    if(match($0,/'url'\s*:\s*'.*'/)){
        pattern = substr($0,RSTART,RLENGTH);
        printf "%s\n",pattern;
    }
}' "app.js"

but the output is 但输出是

url: '/categories',
url: '/report',
url: '/publisher',
url: '/manageoutlet',
url: '/order',
url: '/mobileapp',
url: '/cartitem/:id',

I don't know why it's happen . 我不知道为什么会这样。 I need to get the line 我需要接电话

'url': 'http://fhdjhjd/fkdf',

Using \\047 to match "'" 使用\\ 047匹配“'”

sudo awk '{
    if(match($0,/\047url\047\s*:\s*'.*'/)){
        pattern = substr($0,RSTART,RLENGTH);
        printf "%s\n",pattern;
    }
}' "app.js"

尝试

awk -F":" '$2~/fhdjhjd/{ print }' file

Try this: 尝试这个:

awk '($0~/'\''url'\'':/) {print}' File

In your case, it will be: 在您的情况下,它将是:

sudo awk '($0~/'\''url'\'':/) {print}' "app.js"

You should escape the single quotes ' using '\\'' 您应将单引号'换成'\\''

the regex must be 正则表达式必须是

'\''url'\''\s*:\s*'.*'

The code can be 该代码可以是

sudo awk '{
    if(match($0,/'\''url'\''\s*:\s*'.*'/)){
        pattern = substr($0,RSTART,RLENGTH);
        printf "%s\n",pattern;
    }
}' "app.js"

But I would use a simpler awk code as 但是我会使用更简单的awk代码作为

$ awk ' /'\''url'\''\s*:\s*'.*'/ ' "app.js"
    'url': 'http://fhdjhjd/fkdf',

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

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