简体   繁体   中英

Translating a sed one-liner into awk

I am parsing files containing lines of "key=value" pairs. An example could be this:

Normal line
Another normal line
[PREFIX] 1=Something 5=SomethingElse 26=42
Normal line again

I'd like to leave all lines not containing key=value pairs as they are, while transforming all lines containing key=value pairs as follows:

Normal line
Another normal line
[PREFIX]
  AAA=Something
  EEE=SomethingElse
  ZZZ=42
Normal line again

Assume I have a valid dictionary for the translation.

What I do at the moment is passing the input to sed, where I turn spaces into newlines for the lines that match '^\\[' .

The output is then piped into this awk script:

BEGIN {
    dict[1] = "AAA"
    dict[5] = "EEE"
    dict[26] = "ZZZ"

    FS="="
}   
{
    if (match($0, "[0-9]+=.+")) {
        key = ""
        if ($1 in dict) {
            key = dict[$1]
        }
        printf("%7s = %s\n", key, $2)
    }   
    else {
        print
        next
    }   
}   

The overall command line then becomes:

cat input | sed '/^\(\[.*\)/s/ /\n/g' | awk -f script.awk

My question is: is there any way I can include the sed operation in the middle so to get rid of that additional step?

$ cat tst.awk
BEGIN {
    split("1 AAA 5 EEE 26 ZZZ",tmp)
    for (i=1; i in tmp; i+=2) {
        dict[tmp[i]] = tmp[i+1]
    }
    FS="[ =]"
    OFS="="
}
$1 == "[PREFIX]" {
    print $1
    for (i=2; i<NF; i+=2) {
        print "  " ($i in dict ? dict[$i] : $i), $(i+1)
    }
    next
}
{ print }

$ awk -f tst.awk file
Normal line
Another normal line
[PREFIX]
  AAA=Something
  EEE=SomethingElse
  ZZZ=42
Normal line again

In fact I could not force awk to read the file twice; one for sed command, one for your algo, so I had to modify your algo.

BEGIN {
    dict[1] = "AAA"
    dict[5] = "EEE"
    dict[26] = "ZZZ"

#    FS="="
}   
$0 !~/[0-9]+=.+/ { print }
/[0-9]+=.+/ {
   nb = split($0,arr1);
   for (i=1; i<=nb; i++ in arr1)  {
      nbb = split(arr1[i], keyVal, "=");
      if ( (nbb==2) && (keyVal[1] in dict) ) {
         printf("%7s = %s\n", dict[keyVal[1]], keyVal[2])
      } 
      else
         print arr1[i];
   }
}   

When you have to convert a lot, you can first migrate your dict file into a sed script file. When your dicht file has a fixed format, you can convert it on the fly.

Suppose your dict file looks like

1=AAA
5=EEE
26=ZZZ

And your input file is

Normal line
Another normal line
[PREFIX] 1=Something 5=SomethingElse 26=42
Normal line again

You want to do something like

cat input | sed '/^\[/ s/ /\n/g' | sed 's/^1=/  AAA=/'
# Or eliminating the extra step with cat
sed '/^\[/ s/ /\n/g' input | sed 's/^1=/  AAA=/'

So your next step is converting your dict file into sed commands:

sed 's#\([^=]*\)=\(.*\)#s/^\1=/   \2=/#' dictfile

Now you can combine these with

sed '/^\[/ s/ /\n/g' input | sed -f <(
   sed 's#\([^=]*\)=\(.*\)#s/^\1=/   \2=/#' dictfile
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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