How to name a file like users-DDMMYYYY format?

Posted on 2005-04-18
Medium Priority
Last Modified: 2013-12-23
How can i write a shell script that extracts all the usernames from the file /etc/passwd into a file /tmp/users-DDMMYYYY, then sorts this file alphabetically into another file /tmp/sortedusers-DDMMYYYY and finally join all the usernames together into a single line, with each username separated by a horinzontal tab (thiswill be display on the screen)? DDMMYYYY refers to the current system date.
Thanks for any help
Question by:anhphuc
  • 2
  • 2
LVL 38

Accepted Solution

wesly_chen earned 60 total points
ID: 13813058
> extracts all the usernames from the file /etc/passwd into a file /tmp/users-DDMMYYYY
> sorts this file alphabetically into another file /tmp/sortedusers-DDMMYYYY

> join all the usernames together into a single line


awk -F: '{print $1}' /etc/passwd | sort -u > /tmp/sortedusers-`date +%d%m%Y`

for USERS in /tmp/sortedusers-`date +%d%m%Y`
   echo -e $USERS \\t  >> /tmp/users-`date +%d%m%Y`
LVL 51

Assisted Solution

ahoffmann earned 40 total points
ID: 13813503
awk -F: '{print $1}' /etc/passwd | sort -u > /tmp/sortedusers-`date +%d%m%Y`
tr '\012' '\009' </tmp/sortedusers-`date +%d%m%Y`

Author Comment

ID: 13821803
i've got another solution

cut -d: -f1 /etc/passwd | sort -u >/tmp/users-'date-+'%d%m%Y'
tr '\t' '\n'  </tmp/users-'date-+'%d%m%Y'
LVL 38

Expert Comment

ID: 13821826
Your solution won't work with /tmp/users-'date-+'%d%m%Y'
It's back quote ` , not '  .
LVL 51

Expert Comment

ID: 13821968
also take care that your tr syntax works with GNU tr only

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Originally, this post was published on Monitis Blog, you can check it here . It goes without saying that technology has transformed society and the very nature of how we live, work, and communicate in ways that would’ve been incomprehensible 5 ye…
Make the most of your online learning experience.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question