[awk] printing value of a variable assignment from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [awk] printing value of a variable assignment from a file
# 1  
Old 05-14-2015
[awk] printing value of a variable assignment from a file

Heyas

Me try to print only the value of a (specific) variable assignment from a file.

What i get (1):
Code:
:) tui $ bin/tui-conf-get ~/.tui_rc TUI_THEME

dot-blue
""
"$TUI_DIR_INSTALL_ROOT/usr"
"$TUI_DIR_INSTALL_ROOT/etc/tui"
"$TUI_PREFIX/share/doc/tui"
"$TUI_PREFIX/share/tui"
"/home/sea/.config/tui"
"$TUI_DIR_USER/logs"
"$TUI_DIR_SYSTEM/themes"
"/home/sea/.cache"
"/home/sea/.local/bin"
"/home/sea/.local/man/man1"
"/home/sea/.cache/tui.tmp~"
"$TUI_DIR_USER/loadlist.conf"
"$TUI_DIR_USER/apps.conf"
"$TUI_DIR_USER/user.conf"
"$TUI_DIR_USER/settings.conf"
"$TUI_DIR_CONF/tui.conf"

What i want: (2)
Code:
✔ tui $ bin/tui-conf-get ~/.tui_rc TUI_THEME

dot-blue

Code i got:
Code:
CONFILE="$1"
VARNAME="$2"
awk	'  
		/^#/  ||
		/^\[/  ||
		/!VAR/ { next }
		{
			if (VAR = $1) {
				if ("-z" != $2) {
					print $2
				}
			}
	}' VAR="$VARNAME" FS="=" "$CONFFILE"


The RC file looks like:
Code:
#/home/sea/.tui_rc
# This file is not ment to be changed manualy!
# Any change of this file may result in an unusable TUI.
# Do changes at your own risk!
#
#	Theme
#	Options are: 	default, default-red, dot-blue, dot-red, \
#			floating, mono, witch-purple, witch-yellow
#	See /usr/share/tui/themes
#
 	TUI_THEME=dot-blue
#
#	Paths
#
[ -z "$TUI_DIR_INSTALL_ROOT" ] &&  \
 	TUI_DIR_INSTALL_ROOT=""
[ -z "$TUI_PREFIX" ] && \
 	TUI_PREFIX="$TUI_DIR_INSTALL_ROOT/usr"
[ -z "$TUI_DIR_CONF" ] && readonly \
 	...
#
#	Files
#
[ -z "$TUI_FILE_TEMP" ] && readonly \
 	TUI_FILE_TEMP="/home/sea/.cache/tui.tmp~"
[ -z "$TUI_FILE_CONF_LOADLIST" ] && readonly \
 	TUI_FILE_CONF_LOADLIST="$TUI_DIR_USER/loadlist.conf"
...

What am i missing?
Tia
# 2  
Old 05-14-2015
Perhaps this will help you:

Code:
$ printf "%s=%s\n" one two three four TUI_THEME dot-blue | awk -v VAR="TUI_THEME" -F= '
   /!VAR/ {next}
   { print $2}'
two
four
dot-blue

$ printf "%s=%s\n" one two three four TUI_THEME dot-blue | awk -v VAR="TUI_THEME" -F= '
   $1 != VAR {next}
   { print $2}'
dot-blue


So your choices here are:

Code:
/!VAR/    -> Input line contains the string "!VAR" anywhere
!/VAR/    -> Input line does not contain the string "VAR" anywhere
$0 !~ VAR -> Input line doesn't contain the contents of variable VAR anywhere
$1 != VAR -> First field is anything except the exact contents of variable VAR


Last edited by Chubler_XL; 05-14-2015 at 10:01 PM..
This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 05-15-2015
Since you are using an = sign as field separator, $1 will contain leading spaces, so it is not going to equal to the name, unless it it right at the beginning of the line. You could try
Code:
$1 ~ VAR "$"

rather than
Code:
$1==VAR

--
Try:
Code:
$ awk -F= -v VAR=TUI_THEME '$1 ~ "^[^#]*" VAR "$"{print $2}' ~/.tui_rc
dot-blue


Last edited by Scrutinizer; 05-15-2015 at 04:32 AM..
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk code to inspect variable before printing

i have a unique scenario id like help with. im currently running this command and it does what i want: printf '%s\n' "${RawContent}" | awk '/## Beginning Stages ##/,/## Ending Stages ##/' | awk '!/^#.*\!|^#\!|DefaultError/' Can this be shortened? I'm looking for something portable as... (8 Replies)
Discussion started by: SkySmart
8 Replies

2. Shell Programming and Scripting

Optimize multiple awk variable assignment

how can i optimize the following: TOTALRESULT="total1=4 total2=9 total3=89 TMEMORY=1999" TOTAL1=$(echo "${TOTALRESULT}" | egrep "total1=" | awk -F"=" '{print $NF}') TOTAL2=$(echo "${TOTALRESULT}" | egrep "total2=" | awk -F"=" '{print $NF}') TOTAL3=$(echo... (4 Replies)
Discussion started by: SkySmart
4 Replies

3. Shell Programming and Scripting

Variable assignment inside awk

Hi, Was hoping someone could help with the following: while read line; do pntadm -P $line | awk '{if (( $2 == 00 && $1 != 00 ) || ( $2 == 04 )) print $3,$5}'; done < /tmp/subnet_list Anyone know if it is possible to assign $3 and $5 to separate variables within the {} brackets? Thanks... (14 Replies)
Discussion started by: CiCa
14 Replies

4. Homework & Coursework Questions

problem with printing out variable in awk

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: couldn't print out stored variable in awk 2. Relevant commands, code, scripts, algorithms: i have in a... (5 Replies)
Discussion started by: ymc1g11
5 Replies

5. Shell Programming and Scripting

Printing a variable column using awk

Hi everyone, Ok here's the scenario. I have a control file like this. component1,file1,file2,file3,file4,file5 component2,file1,file2,file3,file4,file5I want to do a while loop here to read all files for each component. file_count=2 while ] do file_name=`cat list.txt | grep... (2 Replies)
Discussion started by: The Gamemaster
2 Replies

6. Shell Programming and Scripting

awk variable assignment-- inside braces or not?

So, in awk, I've always put my variable assignments inside of the curly braces, just like dad, and grandpa, and the 26 generations before them. But today I came upon an awk statement that had them outside the braces-- blasphemy! Seriously, though, is there any practical difference? I was... (3 Replies)
Discussion started by: treesloth
3 Replies

7. Shell Programming and Scripting

AWK Variable assignment issue Ksh script

Hi There, I am writing a ksh script which assigns variable values from file "A" and passes that variables to file "B". While passing the parameters an additional "$" sign is being assigned to awk -v option. Could any one help me with this please. #!/bin/ksh head -1... (3 Replies)
Discussion started by: Jeevanm
3 Replies

8. Shell Programming and Scripting

variable assignment using awk

Guys, Could you please help me out. I need two values in two variables using awk from the o/p of grep. example:- grep sdosanjh <filename> sdosanjh myhostname myfilename NOW WHAT I WANT IS :- sdosanjh should be in variable (say NAME) myhostname should be in variable (say... (8 Replies)
Discussion started by: sdosanjh
8 Replies

9. Shell Programming and Scripting

awk variable assignment in a file

Hi All, I have a little awk script which uses a variable (x): awk -v x=0 'NF != 6 { ++x } END { print "This batch had " x " errors out of ", NR" records"}' But when I've tried to put the command in a file I can't seem to declare the variable. I've managed to simplify the code so that I... (4 Replies)
Discussion started by: pondlife
4 Replies

10. UNIX for Dummies Questions & Answers

Variable assignment for file names.

I am trying to process error files in selected directories. I can count the files that are there and export the contents to a file for either emailing or printing. The next step is to move the files to a processed directory with the name changed to .fixed as the last extension. for file in... (2 Replies)
Discussion started by: jagannatha
2 Replies
Login or Register to Ask a Question