Operating System - HP-UX
1748134 Members
3515 Online
108758 Solutions
New Discussion юеВ

Re: How to align the space between column in output of the script.

 
SOLVED
Go to solution
Sunny123_1
Esteemed Contributor

Re: How to align the space between column in output of the script.

Hi Senthil

If you got proper solution please go ahead and closed this thread

Regards
Sunny
Hein van den Heuvel
Honored Contributor

Re: How to align the space between column in output of the script.

Senthil>> But some entries are repeating, i want to retain only one entry for each. so how to truncat the other entry.

The details provided show that this is caused by duplicate entries in /etc/passwd. The solution (IMHO) is to fix that instead of using some workaround in your script. Fix the core problem for all!


I must admit to a weakness in my suggested solution approach. As written it does not report trying to enter a username entry when one exists. It would have worked without alerting you to the problem with /etc/password.

Viktor>> Hein, nice coding! But can we call this
"du -sk * | sort -rn" thing inside of awk? ;)

Thank you!. I try. As Dennis says, yeah you could, but why. I preferred to keep is outside as it seemed to me that it woudl be the most variable part. It allows you to drive the same report with different data sources. Maybe sorted differently, maybe cobbled up together from multiple intermediate files.

fwiw,

Hein.


James R. Ferguson
Acclaimed Contributor

Re: How to align the space between column in output of the script.

Hi:

Dennis suggested:

# givenname=$(grep "^$uname:" /etc/passwd |
awk -F: 'NR == 1 { gsub(",", "", $5); print $5 }')

...but I so dislike 'grep' piped to 'awk' --- as it uses an extra process (the 'grep') when 'awk' can do it all! Hence:

# givenname=$(awk -F: -v uname=${uname} '$1~uname {gsub(",","",$5);print $5}' /etc/passwd)

...which enables exact matches and eliminates the need for a 'tail -1' or testing for one record in the 'awk' too!

By the way, I also dislike using 'uname' as a variable name since it is the name of a command.

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: How to align the space between column in output of the script.

>JRF: but I so dislike 'grep' piped to 'awk'

Oops, spent so much time removing cut, sed and head, forgot about grep.

>eliminates the need for a 'tail -1' or testing for one record in the 'awk' too!

Why do you say that? If /etc/passwd has two identical records, your script will find both.
Easily fixed by:
givenname=$(awk -F: -v uname=${uname} '$1 ~ uname {gsub(",","",$5); print $5; exit}' /etc/passwd)
James R. Ferguson
Acclaimed Contributor

Re: How to align the space between column in output of the script.

Hi (again):

> Dennis: If /etc/passwd has two identical records, your script will find both.

Ah, now I see what you were originally saying. Senthil's 'passwd' file actually has identical account name records. My comment was focused on the fact that we would match exactly on account name; not that there might be multiple instances. I agree that an 'exit' in the 'awk' after a match is the most performance satisfactory solution. Using a piped 'tail' merely adds another process.

> Senthil, I would 'vipw' your 'passwd' file and _eliminate_ the duplicate accounts. Unfortunately running 'pwck' will not uncover them.

> Dennis > JRF: but I so dislike 'grep' piped to 'awk'. Oops, spent so much time removing cut, sed and head, forgot about grep.

That's OK, you found a similar one that I had missed for the same reason in another thread :-}}

Regards!

...JRF...
senthil_kumar_2
Regular Advisor

Re: How to align the space between column in output of the script.

Hi All,

My problem is solved.

So i am closing this.