简体   繁体   English

如何替换参数的值

[英]How to replace the values of a Param

How can I replace the values of parameters step by step.如何逐步替换参数值。

What I mean is,我的意思是,

For Example- Url is https://example.com/?p=first&q=second&r=third例如- Url 是https://example.com/?p=first&q=second&r=third

First I want to add '123' on p param https://example.com/?p=123&q=second&r=third首先,我想在 p 参数https://example.com/?p=123&q=second&r=third上添加“123”

Then again with same URL but different parameter, such as q param https://example.com/?p=first&q=123&r=third然后再次使用相同的 URL 但参数不同,例如 q param https://example.com/?p=first&q=123&r=third

Again with same URL but different parameter,再次使用相同的 URL 但参数不同,

https://example.com/?p=first&q=second&r=123

What I tried:我尝试了什么:

while read line; do
first_part=`echo $line | cut -d'=' -f1`                                          second_part=`echo $line | cut -d'=' -f2`

    echo "${first_part}=123${second_part}"

    echo "${first_part}${second_part}=123"

done < urls.txt

The problem described is a good application for AWK's capabilities.所描述的问题是 AWK 功能的一个很好的应用。 The demo script includes samples for both URLs and a mapping functions file for global transformation of URLs.演示脚本包括 URL 示例和用于 URL 全局转换的映射函数文件

This approach allows for parameters to "free float", not dependent on matching at a specific sequential position in the URL string.这种方法允许参数“自由浮动”,而不依赖于匹配 URL 字符串中的特定顺序 position。

This approach also allows for parameters to be strings of any length.这种方法还允许参数是任意长度的字符串。

#!/bin/bash

#QUESTION:  https://stackoverflow.com/questions/75124190/how-to-replace-the-values-of-a-param

cat >URL.list <<"EnDoFiNpUt"
https://example.com/?p=first&q=second&r=third
https://example.com/?r=zinger
https://example.com/?r=bonkers&q=junk&p=wacko
https://example.com/?p=flyer
EnDoFiNpUt

cat >mapfile.txt <<"EnDoFiNpUt"
q=SECOND
r=THIRD
p=FIRST
EnDoFiNpUt

awk -v datFile="mapfile.txt" 'BEGIN{
    ## Initial loading of the mapping file into array for comparison
    split( "", transforms ) ;
    indexT=0 ;
    while( getline < datFile ){
        indexT++ ;
        transforms[indexT]=$0 ;
    } ;
}
{
    ### Split off beginning of URL from parameters
    qPos=index( $0, "?" ) ;
    beg=substr( $0, 1, qPos ) ; 

    ### Load URL elements into array for comparison
    rem=substr( $0, qPos+1 ) ;
    n=split( rem, parts, "&" ) ;

    ### Match and Map transforms elements with URL parts
    for( k=1 ; k<= indexT ; k++ ){
        dPos=index( transforms[k], "=" ) ;
        fieldPref=substr( transforms[k], 1, dPos ) ;
        for( i=1 ; i<=n ; i++ ){
            if( parts[i] ~ fieldPref ){
                parts[i]=transforms[k] ;
            } ;
        } ;
    } ;

    ### Print transformed URL
    printf("%s%s", beg, parts[1] ) ;
    for( i=2 ; i<=n ; i++ ){
        printf("&%s", parts[i] ) ;
    } ;
    print "" ;
}' URL.list

The output looks like this: output 看起来像这样:

https://example.com/?p=FIRST&q=SECOND&r=THIRD
https://example.com/?r=THIRD
https://example.com/?r=THIRD&q=SECOND&p=FIRST
https://example.com/?p=FIRST

HTML params are, by spec, orderless, so you can simply place p= 's new value at the tail instead of original position:根据规范,HTML 参数是无序的,因此您可以简单地将p=的新值放在尾部而不是原始 position:

echo 'https://example.com/?p=first&q=second&r=third' | 
 mawk NF=NF FS='p=[^&]*[&]?' OFS= ORS='&p=123\n'
     1  https://example.com/?q=second&r=third&p=123

same for q= . q=相同。

if you're modifying r= instead, then set both FS and OFS to "=" , and do it it like a vanilla value update for $NF如果您要修改r= ,则将FSOFS都设置为"=" ,然后像$NF的香草值更新一样进行操作

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

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