user1536782
user1536782

Reputation: 97

Shell programming --Extract values of 2 key words

Input file (HTTP request log file):

GET /dynamic_branding_playlist.fmil?domain=915oGLbNZhb&pluginVersion=3.2.7_2.6&pubchannel=usa&sdk_ver=2.4.6.3&width=680&height=290&embeddedIn=http%3A%2F%2Fviewster.com%2Fsplash%2FOscar-Videos-1.aspx%3Futm_source%3Dadon_272024_113535_24905_24905%26utm_medium%3Dcpc%26utm_campaign%3DUSYME%26adv %3D573900%26req%3D5006e9ce1ca8b26347b88a7.1.825&sdk_url=http%3A%2F%2Fdivaag.vo.llnwd.net%2Fo42%2Fhtt p_only%2Fviewster_com%2Fv25%2Fyume%2F&viewport=42

Out put file:

domain sdk_version

915oGLbNZhb 2.4.6.3

Thousands of logs similar to the example above, so I need to find a way to extract the value of domain&sdk_version. And the positions of domain and sdk_version are not fixed. sometimes appear in the 2 field, sometimes apprear in the last field (if split by &).

Could anyone help me in this problem (using sed command)? Thanks so much in advance

Upvotes: 1

Views: 200

Answers (3)

potong
potong

Reputation: 58391

This might work for you (GNU sed):

sed 's/.*\<\(domain\)=\([^&]*\).*\<\(sdk_ver\)=\([^&]*\).*/\1 \3sion\n\2 \4/p;d' file

Upvotes: 0

Steve
Steve

Reputation: 54392

Using awk:

BEGIN {
    FS = "[&?]"
    printf "domain\tsdk_version\n"
}

{
    for (i = 1; i <= NF; i++) {
        split ($i, array, "=")
        if (array[1] == "domain") {
            printf array[2]
        }
        if (array[1] == "sdk_ver") {
            printf "\t%s", array[2]
        }
    }
    printf "\n"
}

Or as a one-liner:

awk -F "[&?]" 'BEGIN { printf "domain\tsdk_version\n" } { for (i = 1; i <= NF; i++) { split ($i, array, "="); if (array[1] == "domain") printf array[2]; if (array[1] == "sdk_ver") printf "\t%s", array[2]; } printf "\n"; }' file.txt

Results:

domain  sdk_version
915oGLbNZhb 2.4.6.3

Upvotes: 0

perreal
perreal

Reputation: 97948

Using sed:

sed -n 's/.*domain=\([^&]*\).*sdk_ver=\([^&]*\).*/\1 \2/p' input_file

Upvotes: 1

Related Questions