简体   繁体   中英

How to use awk to print all fields excluding a specific field?

I have a file of 100000 rows separated by ^M. Following are the first 4 rows. I want to remove 35=[etc] from every even row in the file of 100K rows

{1=0200, 62=01000000000000000000000000000000000000000000000000000000000000001000001000001, 63=1000000000000000000000000002, 38=000001, 11=000001, 12=125216, 13=0213, 15=0213, 18=6011, 19=840, 2=4511230100604088, 22=9000, 25=00, 3=010000, 32=12345678901, 35=2302127162,  37=500001000001,  4=000000000300, 41=123ac130, 42=USA STORE000001, 43=SAFEWAY STORE #970       SAN MATEO    US, 49=0840, 52=7B7AD380360E5C66,  59=0600094044, 60=123456789000, 7=1103145214}^M
{1=0200, 62=01000000000000000000000000000000000000000000000000000000000000001000001000002, 63=1000000000000000000000000002, 38=000002, 11=000002, 12=125216, 13=0213, 15=0213, 18=6011, 19=840, 2=4511230100604286, 22=9000, 25=00, 3=010000, 32=12345678901, 35=2302127328,  37=500001000002,  4=000000000300, 41=123ac130, 42=USA STORE000001, 43=SAFEWAY STORE #970       SAN MATEO    US, 49=0840, 52=F55C5F27C8A91F31,  59=0600094044, 60=123456789000, 7=1103145214}^M
{1=0200, 62=01000000000000000000000000000000000000000000000000000000000000001000001000003, 63=1000000000000000000000000002, 38=000003, 11=000003, 12=125216, 13=0213, 15=0213, 18=6011, 19=840, 2=4511230100604997, 22=9000, 25=00, 3=010000, 32=12345678901, 35=2302127295,  37=500001000003,  4=000000000300, 41=123ac130, 42=USA STORE000001, 43=SAFEWAY STORE #970       SAN MATEO    US, 49=0840, 52=CC3803D589D05384,  59=0600094044, 60=123456789000, 7=1103145214}^M
{1=0200, 62=01000000000000000000000000000000000000000000000000000000000000001000001000004, 63=1000000000000000000000000002, 38=000004, 11=000004, 12=125216, 13=0213, 15=0213, 18=6011, 19=840, 2=4511230100605333, 22=9000, 25=00, 3=010000, 32=12345678901, 35=2302127558,  37=500001000004,  4=000000000300, 41=123ac130, 42=USA STORE000001, 43=SAFEWAY STORE #970       SAN MATEO    US, 49=0840, 52=D9FB6238A83D8FDD,  59=0600094044, 60=123456789000, 7=1103145214}^M

The output should be like

{1=0200, 62=01000000000000000000000000000000000000000000000000000000000000001000001000001, 63=1000000000000000000000000002, 38=000001, 11=000001, 12=125216, 13=0213, 15=0213, 18=6011, 19=840, 2=4511230100604088, 22=9000, 25=00, 3=010000, 32=12345678901, 35=2302127162,  37=500001000001,  4=000000000300, 41=123ac130, 42=USA STORE000001, 43=SAFEWAY STORE #970       SAN MATEO    US, 49=0840, 52=7B7AD380360E5C66,  59=0600094044, 60=123456789000, 7=1103145214}^M
{1=0200, 62=01000000000000000000000000000000000000000000000000000000000000001000001000002, 63=1000000000000000000000000002, 38=000002, 11=000002, 12=125216, 13=0213, 15=0213, 18=6011, 19=840, 2=4511230100604286, 22=9000, 25=00, 3=010000, 32=12345678901, 37=500001000002,  4=000000000300, 41=123ac130, 42=USA STORE000001, 43=SAFEWAY STORE #970       SAN MATEO    US, 49=0840, 52=F55C5F27C8A91F31,  59=0600094044, 60=123456789000, 7=1103145214}^M
{1=0200, 62=01000000000000000000000000000000000000000000000000000000000000001000001000003, 63=1000000000000000000000000002, 38=000003, 11=000003, 12=125216, 13=0213, 15=0213, 18=6011, 19=840, 2=4511230100604997, 22=9000, 25=00, 3=010000, 32=12345678901, 35=2302127295,  37=500001000003,  4=000000000300, 41=123ac130, 42=USA STORE000001, 43=SAFEWAY STORE #970       SAN MATEO    US, 49=0840, 52=CC3803D589D05384,  59=0600094044, 60=123456789000, 7=1103145214}^M
{1=0200, 62=01000000000000000000000000000000000000000000000000000000000000001000001000004, 63=1000000000000000000000000002, 38=000004, 11=000004, 12=125216, 13=0213, 15=0213, 18=6011, 19=840, 2=4511230100605333, 22=9000, 25=00, 3=010000, 32=12345678901, 37=500001000004,  4=000000000300, 41=123ac130, 42=USA STORE000001, 43=SAFEWAY STORE #970       SAN MATEO    US, 49=0840, 52=D9FB6238A83D8FDD,  59=0600094044, 60=123456789000, 7=1103145214}^M

This is possible using the g command in vim:

:g/35=[0-9]*, */ if getcurpos()[1] % 2 == 0 | s///g | endif

Basically what we do is:

  1. The g command searches for every line that has a match for the regex 35=[0-9]*, * and then executes the commands following it (in this case the if statement).
  2. Check if the cursor position is even with if getcurpos()[1] % 2 == 0 .
  3. If it is even, replace with an empty string s///g .

More on the power of the g command here: https://vim.fandom.com/wiki/Power_of_g

Not sure if 62=[digits] or if 11=[digits] are reliable row counters. But if they are, maybe consider something like this in Vim?

g/\v11\=\d+[0248],/s/\v, 35\=\d{10}//

This filters on the 11=[digits] for even numbers

g/\v62\=\d+[0248],/s/\v, 35\=\d{10}//

Filters on the 62=[digits] for even numbers

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