Home > database >  How do I parse for a dynamic variable based on constant variables in shell?
How do I parse for a dynamic variable based on constant variables in shell?

Time:04-15

I'm trying to parse two different text files and populate 2 configuration files based on the information I find within the text files.

Here's what I am using:

grep -w -o 50000 GranMap.txt.

Response: 50000, which is good, but I want some of the information that follows that value.

The format of the information within the text files is as follows:

50001|Instrument|ATMS,

where 50001 is the constant, "Instrument" makes the file human readable, and "ATMS" is what changes between files (and is the information I want). In essence, I want to search the file for 50001 to find what is it equal to. In this case 50001 = ATMS.

How can I search for 50001, skip over Instrument, and grab the value that is in the location of ATMS? Maybe there is a more powerful way to do this than using grep?

CodePudding user response:

is quite good for this kind of "field oriented" text

awk -F '|' '$1 == 50001 {print $3}' GranMap.txt

The id value can be passed into awk like this:

constant=50001
awk -F '|' -v id="$constant" '$1 == id {print $3}' GranMap.txt
  • Related