Understanding awk 'expansion'


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Understanding awk 'expansion'
# 1  
Old 10-30-2015
Understanding awk 'expansion'

Heyas

Recently i wanted to help someone with an awk script, but the end-script didnt work as expected.

He wanted, if HOME was empty, to get the HOME of the current USER from /etc/passwd.
At first i tried hardcoded with root:
Code:
awk -F: '/^root/ {print $6}' /etc/passwd

As that worked, i've added a variable, for the changeable 'current user'.
Code:
awk -v U=$(whoami) -F: '/^U/ {print $6}' /etc/passwd

But this didnt want to work, so in the end i had to:
Code:
cmd="awk -F:  '/^$(whoami)/ {print \$6}' /etc/passwd"
eval $cmd

Why is my 2nd code not working? (just returns empty)
Any (better) ideas?

Thank you
# 2  
Old 10-30-2015
As /.../ is a regex constant, it would try to match the literal string "U" at the begin-of-line. Try $0 ~ "^"U.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 10-30-2015
Code:
awk -v U=$(whoami) -F: '"/^" U "/" {print $6}' /etc/passwd

This User Gave Thanks to cjcox For This Post:
# 4  
Old 10-30-2015
Hello sea,

Following may also be an option on same.
Code:
awk -v U=$(whoami) -F: '($1 == U) {print $6}' /etc/passwd

Actually in my server directories are having pretty similar names as users have so I have done with by directly comparing the 1st column itself.


Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 10-30-2015
Quote:
Originally Posted by cjcox
Code:
awk -v U=$(whoami) -F: '"/^" U "/" {print $6}' /etc/passwd

Hi, I do not think this will work. That evaluates to:
Code:
awk -F: '"/^username/"{print $6}' /etc/passwd

Which means if the string "/^username/" is true (i.e. non-empty) then print $6

So this always evaluates to true and it will print field 6 on every line..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash variable expansion in awk script

Hello, I need to split a file into two of different locations by re-direction in awk. cat infle aaa 1 3 bbb 2 4 aaa 3 3 bbb 4 4 aaa 5 3 bbb 6 4 cat /storage/tmp/group_a.gtf aaa 1 3 aaa 3 3 aaa 5 3 cat /storage/tmp/group_b.gtf bbb 2 4 bbb ... (2 Replies)
Discussion started by: yifangt
2 Replies

2. Shell Programming and Scripting

Shell variable expansion in awk

I want to split one file input.tab into two separate ones, odd lines to input_reads1.txt, even lines to input_reads2.txt for a serial of files with similar name pattern. Also I want to "match" input/output file names to keep consistency of file name: CSEL_02.0_input.tab CSEL_03.4_input.tab... (2 Replies)
Discussion started by: yifangt
2 Replies

3. Shell Programming and Scripting

awk : Need Help in Understanding a command

Hello I am working on a Change request and Stuck at a point. The below awk command is used in the function. float_test ( ) { echo | awk 'END { exit ( !( '"$1"')); }' } I understand that awk 'END' is used to add one line at the end and exit is used to end the script with an error... (4 Replies)
Discussion started by: rahul2662
4 Replies

4. UNIX for Dummies Questions & Answers

Understanding awk

I found this on an awk site and would like to know what it does: /CARS/{x="";next} {if(x)print x;x=$0} END{if(x)print x}' Does it mean if it finds the word cars it skips that line and then prints the next one? (4 Replies)
Discussion started by: newbie2010
4 Replies

5. Shell Programming and Scripting

Help in Understanding awk if ( F ) syntax -

Hi Experts, I was looking at the below link, for finding words next to it, And unable to understand this syntax: Can any one please explain , what is meaning of this code: if ( F ) s = s ? s OFS $i : $i from:... (4 Replies)
Discussion started by: rveri
4 Replies

6. Shell Programming and Scripting

Help in understanding awk expression

Hi, Could somebody help me in understanding the following awk expression: awk -v n="POINT" '/%/{print $0 "\n" n ;next}1' < file name Thanks, Arun (6 Replies)
Discussion started by: arun_maffy
6 Replies

7. UNIX for Dummies Questions & Answers

Understanding / Modifying AWK command

Hey all, So I have an AWK command here awk '{if(FNR==NR) {arr++;next} if($0 in arr) { arr--; if (arr == 0) delete arr;next}{print $0 >"list2output.csv"}} END {for(i in arr){print i >"list1output.csv"}}' list1 list2 (refer to image for a more readable format) This code was submitted... (1 Reply)
Discussion started by: Aussiemick
1 Replies

8. UNIX for Advanced & Expert Users

understanding awk in this script

i am analyzing a query written by another developer ,need to understand part of script am looking at a code ..and it converts comma files to pipe delimited and also takes away quotes from any columns, source field format: 2510,"Debbie",NewYork changes to target: 2510|Debbie|NewYork ... (1 Reply)
Discussion started by: coolrock
1 Replies

9. Shell Programming and Scripting

Need to print the expansion of the found string (the expansion is beween two delimiters '-' , '||'

Hi , could anyone help me out with this problem. sample.txt has this content : u001- this is used for project1 || u002- this is used for p2|| not to be printed u003- this is used for project3 || u004- this is used for p4 || u005- this is used for project5 || u006- this is used for p6... (9 Replies)
Discussion started by: Balaji PK
9 Replies

10. Shell Programming and Scripting

Understanding Awk and Cat

Hi Guys, I was recently come across some code to hopefully learn a little bit about putting Shell commands into PHP application to run on a Linux server. However, I don't understand the command AT ALL... and was wondering if anyone can interpret it: cat userIDs.dat | awk '{s=s+1; if... (1 Reply)
Discussion started by: jordRiot
1 Replies
Login or Register to Ask a Question