简体   繁体   English

如何使用 awk 打印除特定字段之外的所有字段?

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

I have a file of 100000 rows separated by ^M.我有一个由 ^M 分隔的 100000 行文件。 Following are the first 4 rows.以下是前 4 行。 I want to remove 35=[etc] from every even row in the file of 100K rows我想从 100K 行文件中的每个偶数行中删除 35=[etc]

{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:这可以在 vim 中使用g命令实现:

: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). g命令搜索与正则表达式35=[0-9]*, *匹配的每一行,然后执行它后面的命令(在本例中为if语句)。
  2. Check if the cursor position is even with if getcurpos()[1] % 2 == 0 .检查光标位置是否与if getcurpos()[1] % 2 == 0
  3. If it is even, replace with an empty string s///g .如果是偶数,则替换为空字符串s///g

More on the power of the g command here: https://vim.fandom.com/wiki/Power_of_g更多关于g命令的力量在这里: https : //vim.fandom.com/wiki/Power_of_g

Not sure if 62=[digits] or if 11=[digits] are reliable row counters.不确定 62=[digits] 或 11=[digits] 是否是可靠的行计数器。 But if they are, maybe consider something like this in Vim?但如果是,也许可以在 Vim 中考虑这样的事情?

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

This filters on the 11=[digits] for even numbers这对偶数的 11=[digits] 进行过滤

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

Filters on the 62=[digits] for even numbers在 62=[digits] 上过滤偶数

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

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