- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: How to align the space between column in outpu...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-27-2009 11:22 AM
тАО05-27-2009 11:22 AM
My script file:
#! /usr/bin/sh
cd /emd/home
echo "SPACE \t \t USERID \t \t USERNAME \t \t LINKED DIRECTORY \n"
du -sk * | sort -rn | while read line
do
space=`echo $line | awk {'print $1'}`
uname=`echo $line | awk {'print $2'}`
givenname=`grep ^"$uname:" /etc/passwd|cut -d: -f5`
linkdir=`ll -nd /emd/home/$uname | awk {'print $11'}`
#homedir=$(echo /emd/home/$uname)
echo "$space \t \t $uname \t \t $givenname \t \t $linkdir"
done
The output is: (Pls find the attachment)
SPACE USERID USERNAME LINKED DIRECTORY
7468504 remoquil Soliman Remoquillo,,,
5806088 pzhysl Jason Meyers,,,
3069352 7en0fm Mark Herman
0584 oddie Michael Oddie,,,
3336 bin
3288 hzrwh7 Michell Westra,,,
3080 xz5v8v Seban, Elizabeth,,
5176 deano Dean Linko,,,
4608 czhk53 Vijay Reddy,,,
3072 zz7msk Mike Miller,,,
2880 erickson Karl Erickson,,,
1648 test7 Testin ID
7680 security
7296 andersos Steven Anderson,,,
6152 3ezaxv Colin Copley
5936 noel Doug Noel,,,
4512 src
4304 xz9wmt Rajkumar Molugu,,,
0 vz1fmk Ish Patel,,,
0 tzrrzp Roopesh Shroff,,, /emd/ansys2/tzrrzp
0 tpw /emd/tpw
0 tombers /emd/ansys1/tombers
0 svihla Gary Svihla,,, /emd/ansys2/svihla
0 stdlib Standards Coord CDMS,,, /users/stdlib
0 schueler /emd/ansys2/schueler
0 rz17by Maximo Test,,,
0 ne7jll Lindsay Parsons,,,
0 ley7ux Sridhar Chunduri
0 kzx4yz Greg Langlois,,,
0 kmoravec Keith Moravec,,, /emd/ansys1/kmoravec
0 jyu Jin Yu,,, /emd/ansys1/jyu
0 jcarr John Carr,,, /emd/ansys2/jcarr
0 fkankuc Fanny Kanku,,,
0 duve /emd/ansys1/duve
0 cp
0 chobot Tony Chobot,,, /emd/ansys1/chobot
Pls find the attachment for the current align ment of the above output.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-27-2009 11:31 AM
тАО05-27-2009 11:31 AM
Re: How to align the space between column in output of the script.
If you want consistent columnar alignment, use 'printf' with fixed widths and not tab ('\t') characters in an 'echo' statement.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-27-2009 11:59 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-27-2009 12:22 PM
тАО05-27-2009 12:22 PM
Re: How to align the space between column in output of the script.
I use awk's "printf" function for that (that's pretty much the same a the shell's one suggested by James).
I took your script and rewrited:
#! /usr/bin/sh
(
cd /emd/home
echo "SPACE USERID USERNAME LINKED_DIRECTORY"
du -sk * | sort -rn | while read space uname
do
# space=`echo $line | awk {'print $1'}`
# uname=`echo $line | awk {'print $2'}`
givenname=`grep ^"$uname:" /etc/passwd|cut -d: -f5`
linkdir=`ll -nd /emd/home/$uname | awk {'print $11'}`
echo "$space $uname $givenname $linkdir"
done
)| awk '{printf "%10s %-10s %-15s %s\n", $1, $2, $3, $4}'
Changes: the "read" command can split the input line (no need for the extras "awk" to extract uname y space)
The "echo" lines prints the data with just a single space, the final awk would do all de formating. All echo's go to the same awk as they're within a subshell (bettween parenthesis). All subshell output goes to the final pipe where awk read id.
With this schema is simple to adust the script for varying lenghts in some field, to left o right justify. And, as title and body are processed by the same line, they're always be coherent.
Hope it helps. On my system worked pretty well (see attatch)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-27-2009 12:24 PM
тАО05-27-2009 12:24 PM
Re: How to align the space between column in output of the script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-27-2009 12:58 PM
тАО05-27-2009 12:58 PM
Re: How to align the space between column in output of the script.
"The output is: (Pls find the attachment)"
Good and appreciated attempt, but it is not the actual output. Is appears to be a cut & paste from a screen shot with the TAB characters which are critital to this problem gratuitously, and erroneously, transformed to spaces. To do this properly, redirect to a file and transfer as 'txt'. (Not .DOC not .RTF !).
Anyway... If you are doing all this awking and grepping, then why pretend to write a shell script. Just write a full awk program ?!
Get in there once, make it do the job, and get out.
You see, I'm a performance guy. So I cringe when I see "grep uname passwd | cut -d: -f5"
in a loop. For every record processed we read a whole file, and spawn two processes. Read it once, and remember what you need to!
:-)
The output format ought to the same for the header and a details right? A single awk program can nicely accomplish that.
So where is this rambling leading to?
Check out the program 'test.awk' below.
It reads /etc/passwd in the "BEGIN" section and fills an array with username -> given-name associations. That section also defines the output format and prints the header.
In the main loop, we look up the username, lookup your directory link, and print the output. Easy?
Run as:
du -sk * | sort -rn | awk -f test.awk > test.txt
Results in test.txt:
SPACE USERID USERNAME LINKED
----- ------ -------- ------
71864 hein Hein van den Heuvel /home/hein/aap
2352 test /home/hein/noot
Enjoy !
Hein.
BEGIN { FS = ":";
while (getline < "/etc/passwd") u[$1] = $5;
FS = " ";
fmt = "%12s %-12s %-20s %-30s\n"
printf fmt, "SPACE", "USERID", "USERNAME", "LINKED", "DIRECTORY";
printf fmt, "-----", "------", "--------", "------", "---------";
}
{ space = $1;
uname = $2;
given = u[uname];
"ll -nd /emd/home/" uname | getline;
link = $11;
printf fmt, space, uname, given, link;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-27-2009 01:28 PM
тАО05-27-2009 01:28 PM
Re: How to align the space between column in output of the script.
...and this thread would appear to be a continuation of your earlier one:
http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1341889
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-27-2009 08:49 PM
тАО05-27-2009 08:49 PM
Re: How to align the space between column in output of the script.
Instead of this below line or your script
echo "$space \t \t $uname \t \t $givenname \t \t $linkdir"
use this line into your script
printf "%-15s %20s %20s %20s" $space $uname $givenname $linkdir
Suraj
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-28-2009 04:55 AM
тАО05-28-2009 04:55 AM
Re: How to align the space between column in output of the script.
I modified the script as per your suggestion. now i am able to get the output aligned.
Modified script is:
#! /usr/bin/sh
cd /emd/home
printf "%-10s %-15s %-35s %-25s\n" "SPACE" "USERID" "USERNAME" "LINKED_DIRECTORY"
printf "%-10s %-15s %-35s %-25s\n" "-----" "------" "--------" "----------------"
du -sk * | sort -rn | while read space uname
do
#space=`echo $line | awk {'print $1'}`
#uname=`echo $line | awk {'print $2'}`
givenname=`grep ^"$uname:" /etc/passwd|cut -d: -f5`
linkdir=`ll -nd /emd/home/$uname | awk {'print $11'}`
printf "%-10s %-15s %-35s %-25s\n" "$space" "$uname" "$givenname" "$linkdir"
done
My output is:
SPACE USERID USERNAME LINKED_DIRECTORY
----- ------ -------- ----------------
7468504 remoquil Soliman Remoquillo,,,
5806312 pzhysl Jason Meyers,,,
3069352 7en0fm Mark Herman
2033224 wabtec Wabtec sharedaccount,,,
1966224 paez
1961688 7e8e9u Steven Dettloff,,,
1888472 mentordata
1810456 infodba Iman Administrator,,,
1650224 tek
1536368 reydl2 Matthew A Sawtell,,,
269904 common_box
0 zzw2cc Cherl Nordmann,,,
0 zehjrf
0 vzh53p Walter Klaric,,,
0 vz1fmk Ish Patel,,,
0 tzrrzp Roopesh Shroff,,, /emd/ansys2/tzrrzp
0 tpw /emd/tpw
0 tombers /emd/ansys1/tombers
0 svihla Gary Svihla,,, /emd/ansys2/svihla
0 stdlib Standards Coord CDMS,,, /users/stdlib
Here i want one more to be done.
In username column of the report is displaying the 5th column (GECOS) of the "/etc/passwd".
Previous unix admin of our company userd to give three commas (,,,) after each names mentioed in the 5th column (comment field) of the passwd file.
Now i want to remove commas (,,,) if it is suffixed with the name.
Here I want the name only but not commas.
Pls give your suggestion on modifying the script.
I have attached the file containing my script and output report.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-28-2009 05:10 AM
тАО05-28-2009 05:10 AM
Re: How to align the space between column in output of the script.
To eliminate the commas from your output, add the following line just before your 'printf':
# givenname=$(echo "${givenname}"|sed -e 's/,/ /g')
This replaces every comma character with a space. Thus if there is more than just the first field of the GECOS field, other subfields are left space-delimited.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-28-2009 05:14 AM
тАО05-28-2009 05:14 AM
Re: How to align the space between column in output of the script.
change givenname as :
givenname=`grep ^"$uname:" /etc/passwd|cut -d: -f5` | sed -e 's/,//g'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-28-2009 05:19 AM
тАО05-28-2009 05:19 AM
Re: How to align the space between column in output of the script.
try this
givenname=$(echo "${givenname}"|sed -e 's/,/ /g')
Regards
Sunny
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-28-2009 01:22 PM
тАО05-28-2009 01:22 PM
Re: How to align the space between column in output of the script.
Hein, nice coding! But can we call this
"du -sk * | sort -rn" thing inside of awk? ;)
Unix operates with beer.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-28-2009 08:23 PM
тАО05-28-2009 08:23 PM
Re: How to align the space between column in output of the script.
>>Here I want the name only but not commas
There are 2 easy way to remove comma
1. open your output into vi
at the vi command prompt give this
:1,$s/,,,//gp
2. assum your output is in out.txt file
now give this command at comman prompt
#sed 's/,,,//'
Suraj
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-29-2009 02:51 AM
тАО05-29-2009 02:51 AM
Re: How to align the space between column in output of the script.
Yes but why bother. ;-)
"du -sk *" | getline
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-29-2009 03:35 AM
тАО05-29-2009 03:35 AM
Re: How to align the space between column in output of the script.
Thanks a lot now i am getting correct output that i want.
But some entries are repeating, i want to retain only one entry for each. so how to truncat the other entry.
A)My script:
#! /usr/bin/sh
cd /emd/home
printf "%-10s %-15s %-35s %-25s\n" "SPACE" "USERID" "USERNAME" "LINKED_DIRECTORY"
printf "%-10s %-15s %-35s %-25s\n" "-----" "------" "--------" "----------------"
du -sk * | sort -rn | while read space uname
do
#space=`echo $line | awk {'print $1'}`
#uname=`echo $line | awk {'print $2'}`
givenname=`grep ^"$uname:" /etc/passwd|cut -d: -f5|sed 's/,//g'`
linkdir=`ll -nd /emd/home/$uname | awk {'print $11'}`
printf "%-10s %-15s %-35s %-25s\n" "$space" "$uname" "$givenname" "$linkdir"
done
B)Output:
SPACE USERID USERNAME LINKED_DIRECTORY
----- ------ -------- ----------------
7436064 remoquil Soliman Remoquillo
5806320 pzhysl Jason Meyers
3069352 7en0fm Mark Herman
2033224 wabtec Wabtec sharedaccount
2032944 7e8e9u Steven Dettloff
1966224 paez
1888472 mentordata
1810456 infodba Iman Administrator
1650224 tek
1536368 reydl2 Matthew A Sawtell
1443248 redquest
1381032 nzr3k0 Nicholas Shim-Ping
103832 lzrxcl Emma Biddings ---------------------------> First time
Emma Biddings ------------------------------------------------------> second time
6544 oracle Oracle Administrator --------------------> First time
Oracle Administrator -----------------------------------------------> Second time
0 vz1fmk Ish Patel
0 tzrrzp Roopesh Shroff /emd/ansys2/tzrrzp
0 tpw /emd/tpw
0 tombers /emd/ansys1/tombers
0 svihla Gary Svihla /emd/ansys2/svihla
0 stdlib Standards Coord CDMS /users/stdlib
0 schueler /emd/ansys2/schueler
0 rz17by Maximo Test
C)I checked why they are comming two times:
1) 103832 lzrxcl Emma Biddings
Emma Biddings
root@lgapps:/root > grep lzrxcl /etc/passwd
lzrxcl:R1b5gn01B1VGU:1260:1002:Emma Biddings,,,:/emd/ansys2/lzrxcl:/usr/bin/sh
lzrxcl:R1b5gn01B1VGU:1260:1002:Emma Biddings,,,:/emd/ansys2/lzrxcl:/usr/bin/sh
2)6544 oracle Oracle Administrator
Oracle Administrator
root@lgapps:/root > grep ^oracle: /etc/passwd
oracle:oAx6JjE54AHZw:498:1010:Oracle Administrator,,,:/appl/oracle/dba/admin:/usr/bin/sh
oracle:oAx6JjE54AHZw:498:1010:Oracle Administrator,,,:/appl/oracle/dba/admin:/usr/bin/sh
I want only one entry for each directory.
Pls find the attachment for clear details.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-29-2009 03:56 AM
тАО05-29-2009 03:56 AM
Re: How to align the space between column in output of the script.
Can you attached it once again????
Regards
Sunny
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-29-2009 03:59 AM
тАО05-29-2009 03:59 AM
Re: How to align the space between column in output of the script.
Thanks, that solves the mystery:
givenname=$(grep "^$uname:" /etc/passwd |
awk -F: 'NR == 1 { gsub(",", "", $5); print $5 }')
(Or you could stick a "head -1" in the pipe.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-29-2009 04:02 AM
тАО05-29-2009 04:02 AM
Re: How to align the space between column in output of the script.
You can give head option in pipe like
head -1 if you want only one entry.
Regards
Sunny
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-29-2009 04:27 AM
тАО05-29-2009 04:27 AM
Re: How to align the space between column in output of the script.
Thanks a lot.
Now everything is fine.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-29-2009 04:31 AM
тАО05-29-2009 04:31 AM
Re: How to align the space between column in output of the script.
If you got proper solution please go ahead and closed this thread
Regards
Sunny
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-29-2009 05:15 AM
тАО05-29-2009 05:15 AM
Re: How to align the space between column in output of the script.
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-29-2009 05:31 AM
тАО05-29-2009 05:31 AM
Re: How to align the space between column in output of the script.
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-30-2009 12:53 AM
тАО05-30-2009 12:53 AM
Re: How to align the space between column in output of the script.
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)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-30-2009 04:51 AM
тАО05-30-2009 04:51 AM
Re: How to align the space between column in output of the script.
> 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...