Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

shell programming

Posted on 2004-10-04
13
Medium Priority
?
274 Views
Last Modified: 2010-04-20
I do this
    ypcat passwd
and I get this (part of it only)
abcdefa:x:39655:904:First Name:/home/nusstu/abcdefa:/bin/bash
ghijkla:x:37968:903:Second Name, Surname:/home/nusstu/ghijkla:/bin/acctsh disable /bin/bash

In my shell script, I do this

#!/bin/bash

# incorrect usage
if [ ! $# -eq 0 ]
then
    echo "Usage: $0"

    # unsuccessful exit
    exit 1
# correct usage
else
    REC=`ypcat passwd | tr ":" " "`

    echo "$REC"

    for FIELD in $REC
    do
        echo $FIELD
    done

    # successful exit
    exit 0
fi


My above intention is to get the output and store inside my variable REC but then when I display it, there's no line separation (all are on the same line).


But then i am stucked.
My intention is to get only the name part and display them out.
My expected output is

First Name
Second Name, Surname
0
Comment
Question by:ee_guest
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
  • +1
13 Comments
 
LVL 33

Accepted Solution

by:
hongjun earned 100 total points
ID: 12224204
try this

ypcat passwd | awk -F\: '{print $5}'  
0
 

Author Comment

by:ee_guest
ID: 12224243
good job.
but then is it possible to use string manipulation?
like read each line and then print out each line.
After which, we perform some manipulation to get the 5th column.
0
 
LVL 10

Assisted Solution

by:Luxana
Luxana earned 100 total points
ID: 12224313
# cat /etc/passwd | cut -d ':' -f 5 | sed 's/,//g'

yoou can use sed for manipulating strings and STDIN | STDOUT

in this case you have just names...
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 10

Expert Comment

by:Luxana
ID: 12224328
What exactly you need your output look like?
0
 

Author Comment

by:ee_guest
ID: 12224398
What's the use of the sed? Without it, I think will still work.
0
 

Author Comment

by:ee_guest
ID: 12224404
my final output should be all the names like this


First Name
Second Name, Surname



0
 
LVL 10

Expert Comment

by:Luxana
ID: 12224434
Second Name isn't same as surname?
I'm still not sure about output...:-)sorry

with sed you can manipulate standart input to standart output using " | " so you read line from 5th column and then use sed to make changes.
0
 
LVL 3

Expert Comment

by:jacauc
ID: 12224449
...and then you can consider using gawk instead of awk as it is much more powerful.
You can then just point each value into a variable like $1 $2 etc... and then use then anywhere you like.

Should i elaborate?
Cheers
J
0
 

Author Comment

by:ee_guest
ID: 12224468
basically, i want to display the 5th column..
don't care about the surname
0
 
LVL 10

Expert Comment

by:Luxana
ID: 12224512
# cat /etc/passwd | cut -d ':' -f 5 | sed 's/,//g'

here is 5th column of /etc/passwd

in my case I use sed 's/,//g' to remove ''," form my 5th column...

0
 

Author Comment

by:ee_guest
ID: 12224895
BTW, anyone knows how to answer my question above?

get the output and store inside my variable REC but then when I display it, there's no line separation (all are on the same line).
0
 

Author Comment

by:ee_guest
ID: 12224902
That is I should be able to use the loop to display line by line

abcdefa:x:39655:904:First Name:/home/nusstu/abcdefa:/bin/bash
ghijkla:x:37968:903:Second Name, Surname:/home/nusstu/ghijkla:/bin/acctsh disable /bin/bash


0
 
LVL 3

Expert Comment

by:jacauc
ID: 12224973
With gawk you can specify which character to use as a field seperator... in this case it a colon :
0

Featured Post

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses

650 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