Skip to content
Advertisement

Linux/Shell slicing a large text file

Given

File1

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

File2

uniquekey1:hello
uniquekey2:apple
uniquekey3:hello

I wish to make a method that basically does this

$ command uniquename2
apple
$ command uniquename1
hello
$ command uniquename3
hello

So given a uniquename from file1 it will use its key from file2 to get what it’s link to. If it doesn’t find uniquename do nothing.

MY ATTEMPT

$ grep -i 'uniquename1' | (not sure how to slice the line with regex "*:*:*") | grep $thisline file2

Advertisement

Answer

The cut utility was designed for this type of data:

#!/bin/bash
KEY=$(grep -i -e "^$1:" file1.txt | cut -d ':' -f 4)
grep -e "^$KEY" file2.txt | cut -d ':' -f 2
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement