如何找到与部分字符串匹配的内容,然后使用awk从参考文件中删除该字符串?

如何解决如何找到与部分字符串匹配的内容,然后使用awk从参考文件中删除该字符串?

我有一个一直在试图解决的问题,但是还没有弄清楚该怎么做。我有一个参考文件,其中包含条形码中清单中的所有设备。

参考文件:

PTR10001,PRINTER,SN A
PTR10002,SN B
PTR10003,SN C 
MON10001,MONITOR,SN A
MON10002,SN B
MON10003,SN C
CPU10001,COMPUTER,SN A
CPU10002,SN B
CPU10003,SN C

我想做的是制作一个文件,我只需要在上面写上我需要的缩写即可。 文件2如下所示:

PTR
CPU
MON
MON

所需的输出将是一个文件,该文件可以告诉我需要从货架上拿走条形码的哪些物品。
所需的输出文件:

PTR10001
CPU10001
MON10001
MON10002

从输出中可以看到,由于我不能拥有2个相同的条形码,因此我需要它浏览参考文件并找到第一个匹配项。将数字复制到输出文件后,我想从参考文件中删除该数字,以免重复该数字。

我尝试了awk的多次迭代,但无法获得所需的输出。
我得到的最接近的是以下代码:

awk -F'/' '{ key = substr($1,1,3) } NR==FNR {id[key]=$1; next} key in id { $1=id[key] } { print }' $file1 $file2 > $file3

我正在用ksh编写此文件,并希望使用awk,因为我认为这将是解决该问题的最佳方法。 感谢您的帮助。

解决方法

请您尝试按照GNU awk中的示例进行跟踪,编写和测试。

awk '
FNR==NR{
  iniVal[$0]++
  next
}
{
  counter=substr($0,1,3)
}
counter in iniVal{
  if(++currVal[counter]<=iniVal[counter]){
     print $1
     if(currVal[counter]==iniVal[counter]){ delete iniVal[$0] }
  }
}
' Input_file2  FS="," Input_file1

说明: 添加以上详细说明。

awk '                                           ##Starting awk program from here.
FNR==NR{                                        ##Checking condition if FNR==NR which is true when Input_file2 is being read.
  iniVal[$0]++                                  ##Creating array iniVal with index of current line with increment of 1 each time it comes here.
  next                                          ##next will skip all further statements from here.
}
{
  counter=substr($0,3)                        ##Creating counter variable which has 1st 3 characters of Input_file1 here.
}
counter in iniVal{                              ##Checking if counter is present in iniVal then do following.
  if(++currVal[counter]<=iniVal[counter]){      ##Checking if currValarray with index of counter value is lesser than or equal to iniVal then do following.
     print $1                                   ##Printing 1st field of current line here.
     if(currVal[counter]==iniVal[counter]){     ##Checking if currVal value is equal to iniVal with index of counter here.
       delete iniVal[$0]                        ##If above condition is TRUE then deleting iniVal here.
     }
  }
}
' Input_file2  FS="," Input_file1               ##Mentioning Input_file names here.
,

第一个解决方案:

根据您的详细说明,我认为顺序无关紧要,因为您想知道该如何下架。因此,您可以做相反的事情,首先阅读file2,对物品进行计数,然后到架子上拿起它们。

awk -F,'FNR==NR{c[$0]++; next} c[substr($1,3)]-->0{print $1}' file2 file1

输出:

PTR10001
MON10001
MON10002
CPU10001

第二个解决方案:

您的awk非常接近您想要的内容,但是您需要数组中的第二个维度,并且不覆盖现有ID。我们将使用伪2维数组(BTW GNU awk具有真实的二维数组)来完成该操作,在其中存储PTR10001,PTR10002,PTR10003之类的ID,并使用split检索它们,然后删除从架子上也可以。

> cat tst.awk
BEGIN { FS="," }

NR==FNR {
    key=substr($1,3)
    ids[key] = (ids[key]? ids[key] "," $1: $1) #append new id.
    next
}

$0 in ids {
    split(ids[$0],tmp,",")
    print(tmp[1])
    ids[$0]=substr(ids[$0],length(tmp[1])+2) #remove from shelf
}

输出

awk -f tst.awk file1 file2
PTR10001
CPU10001
MON10001
MON10002

在这里,我们保留file2的顺序,因为这是基于您尝试过的想法。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-