INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Unix Shell Scripting - Add Extra Column In While Loop

Unix Shell Scripting - Add Extra Column In While Loop

Unix Shell Scripting - Add Extra Column In While Loop

(OP)
I have a file server.txt containing different hostnames like:

CODE -->

hostname1.com
hostname2.com 

My shell script servers.sh is written to get the /etc/passwd and /etc/group files from the list in servers.txt file.

I wish to add the hostname from which the entries came from in my final output file. My script looks something like below:

CODE -->

while read HOST ;
        do
           sshpass -p $password ssh -n $username@$HOST 'cat /etc/passwd'>>users.txt
           sshpass -p $password ssh -n $username@$HOST 'cat /etc/group'>>groups.txt
done < servers.txt
echo -e "UserName;UID;GID;HomeDir;Shell" > final_users.csv
cut -d: -f1,3,4,6,7 users.txt  | tr ':' ';'>> final_users.csv
echo -e "GroupName;GID;Members" > final_groups.csv
awk -F '' '{for(i=4;i<=NF;i++)print$1";"$3";"$i}' groups.txt >> final_groups.csv 

The goal is to add another column in both

CODE -->

final_users.csv 
and

CODE -->

final_groups.csv 
like

CODE -->

hostname 
so I can know which servers each entry came from.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close