1

The input text in test.txt file:

{"col1":"250000","col2":"8089389","col4":"09876545","col3":"121","col5":"123456789"}
{"col1":"210000","col3":"112","col2":"8089389","col4":"09876545","col5":"123456789"}
{"col1":"120000","col2":"8089389","col3":"123","col4":"09876545","col5":"123456789"}
{"col1":"170000","col2":"8089389","col4":"09876545","col5":"123456789","col3":"123"}
{"col1":"190000","col2":"8089389","col4":"09876545","col5":"123456789,"col3":"124""}
{"col3":"176","col1":"220000","col2":"8089389","col4":"09876545","col5":"123456789"}

The command line and result that i tried:

$ awk -F"," '{for(i=1;i<=NF;i++){ if($i ~ /col1/){print $i} };for (x=1;x<=NF;x++){if($x ~ /col3/){print $x}}}' test.txt
{"col1":"250000"
"col3":"121"
{"col1":"210000"
"col3":"112"
{"col1":"120000"
"col3":"123"
{"col1":"170000"
"col3":"123"
{"col1":"190000"
"col3":"124"
{"col1":"220000"
"col3":"176"

The expected result that i would like to get:

col1:250000,col3:121
col1:210000,col3:112
col1:120000,col3:123
col1:170000,col3:123
col1:190000,col3:124
col1:220000,col3:176

4 Answers 4

2

Seems like you are parsing a json file. You can use jq,

$ jq --raw-output '"col1:" + .col1 + ",col3:" + .col3' file.json
col1:250000,col3:121
col1:210000,col3:112
col1:120000,col3:123
col1:170000,col3:123
col1:190000,col3:124
col1:220000,col3:176

For more info: jq manual

Sign up to request clarification or add additional context in comments.

2 Comments

Unfortunately there is not jq on my server and i have not the permission to install it. Any idea with awk ?
@RafaelNguyen, No need to install it. You can get pre-compiled binary here
1

try:

awk  '{gsub(/\{|\"|\}|\;/,"");match($0,/col1[^,]*/);VAL1=substr($0,RSTART,RLENGTH)",";match($0,/col3[^,]*/);VAL2=substr($0,RSTART,RLENGTH);if(VAL1 && VAL2){print VAL1 VAL2}}'  Input_file

I am globally substituting the characters {}"; in the line and then looking for a match for col1 and col3 strings in each line and if both the col1 and col3 strings are present then printing them.

EDIT: Adding a non-one liner form of solution too now.

awk  '{
        gsub(/\{|\"|\}|\;/,"");
        match($0,/col1[^,]*/);
        VAL1=substr($0,RSTART,RLENGTH)",";
        match($0,/col3[^,]*/);
        VAL2=substr($0,RSTART,RLENGTH);
        if(VAL1 && VAL2){
                                print VAL1 VAL2
                        }
      }
     '   Input_file

Comments

1

if missing json tools, here is an awk hack

$ awk -F'[:,]' -v OFS=, -v cols='col1,col3' '
       {n=split(cols,c); 
        gsub(/[{}"]/,""); 
        for(i=1;i<NF;i+=2) a[$i]=$(i+1); 
        for(i=1;i<=n;i++) printf "%s%s", (c[i]":"a[c[i]]), (i==n?ORS:OFS)}' file

col1:250000,col3:121
col1:210000,col3:112
col1:120000,col3:123
col1:170000,col3:123
col1:190000,col3:124
col1:220000,col3:176

Comments

0

Whenever you're manipulating data that has name->value mappings, it's best to first create an associative array to store that mapping (n2v[] below) and then you can just print whatever values you want by looking them up in the array by their name.

$ cat tst.awk
BEGIN { RS="}"; FS="\""; OFS="," }
{
    for (i=2; i<=NF; i+=4) {
        n2v[$i] = $(i+2)
    }
    print p("col1"), p("col3")
}
function p(n) { return (n ":" n2v[n]) }

$ awk -f tst.awk file
col1:250000,col3:121
col1:210000,col3:112
col1:120000,col3:123
col1:170000,col3:123
col1:190000,col3:123
col1:220000,col3:176
col1:220000,col3:176

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.