• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 276
  • Last Modified:

shell programming

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
ee_guest
Asked:
ee_guest
  • 6
  • 4
  • 2
  • +1
2 Solutions
 
hongjunCommented:
try this

ypcat passwd | awk -F\: '{print $5}'  
0
 
ee_guestAuthor Commented:
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
 
LuxanaCommented:
# 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LuxanaCommented:
What exactly you need your output look like?
0
 
ee_guestAuthor Commented:
What's the use of the sed? Without it, I think will still work.
0
 
ee_guestAuthor Commented:
my final output should be all the names like this


First Name
Second Name, Surname



0
 
LuxanaCommented:
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
 
jacaucCommented:
...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
 
ee_guestAuthor Commented:
basically, i want to display the 5th column..
don't care about the surname
0
 
LuxanaCommented:
# 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
 
ee_guestAuthor Commented:
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
 
ee_guestAuthor Commented:
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
 
jacaucCommented:
With gawk you can specify which character to use as a field seperator... in this case it a colon :
0

Featured Post

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

  • 6
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now