鉴于
文件1

uniquename1:somethinguseless:somethinguseless:uniquekey1
uniquename2:somethinguseless:somethinguseless:uniquekey2
uniquename3:somethinguseless:somethinguseless:uniquekey3

文件2
uniquekey1:hello
uniquekey2:apple
uniquekey3:hello

我想做一个基本上可以做到这一点的方法
$ command uniquename2
apple
$ command uniquename1
hello
$ command uniquename3
hello

因此,给定一个来自file1的uniquename,它将使用来自file2的密钥来获取它的链接。如果找不到uniquename就什么都不做。
我的尝试
$ grep -i 'uniquename1' | (not sure how to slice the line with regex "*:*:*") | grep $thisline file2

最佳答案

cut实用程序是为此类数据设计的:

#!/bin/bash
KEY=$(grep -i -e "^$1:" file1.txt | cut -d ':' -f 4)
grep -e "^$KEY" file2.txt | cut -d ':' -f 2

关于linux - Linux/Shell切片大文本文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56712353/

10-16 16:19