Birbal
Birbal

Reputation: 353

Retrieving a variable name that starts with a specific string

I have a variable name that appears in multiple locations of a text file. This variable will always start with the same string but not always end with the same characters. For example, it can be var_name or var_name_TEXT.

I'm looking for a way to extract the first occurrence in the text file of this string starting with var_name and ending with , (but I don't want the comma in the output).

Example1: var_name, some_other_var, another_one, ....

Output: var_name

Example2: var_name_TEXT, some_other_var, another_one, ...

Output: var_name_TEXT

Upvotes: 0

Views: 99

Answers (4)

dawg
dawg

Reputation: 104032

To print the field only (i.e., var_name or var_name_TEXT only; not the line containing it) you could use awk:

awk -F, '{for (i=1;i<=NF;i++) if ($i~/^var_name/) print $i}' file

If you actually have spaces before or after the commas (as you show in your example) you can change to awk field separator:

awk -F"[, ]+" '{for (i=1;i<=NF;i++) if ($i~/^var_name/) print $i}' file

You can also use GNU grep with a word boundary assertion:

grep -o '\bvar_name[^,]*' file

Or GNU awk:

awk '/\<var_name/' file

If you want only one considered, add exit to awk or -m 1 to grep to exit after the first match.

Upvotes: 0

karakfa
karakfa

Reputation: 67527

grep -oPm1 '\bvar_name[^, ]*(?=,)' file | head -1

match and output only variables starting with var_name and ending with comma, do not include comma in the output, quit after the first line of match and pick the first match on that line (if there are more than one)

ps. you have to include space in the regex as well.

Upvotes: 1

Ed Morton
Ed Morton

Reputation: 204218

All you need is (GNU awk):

$ awk 'match($0,/\<var_name[^,]*/,a){print a[0]; exit}' file
var_name_TEXT

Upvotes: 0

Cyrus
Cyrus

Reputation: 88774

I suggest with GNU grep:

grep -o '\bvar_name[^,]*' file | head -n 1

Upvotes: 1

Related Questions