LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Need help with small program (https://www.linuxquestions.org/questions/linux-newbie-8/need-help-with-small-program-380211/)

batalia 11-05-2005 10:33 AM

Need help with small program
 
Need to know how I would create this.

hanak:##chanak:10665:250::/home/home1/summer/chanak:/bin/bash
ling:##yling:9033:310::/home/home1/ugrad/yling:/bin/bash
sorgent:##tsorgent:10405:250::/home/home2/grads/sorgent:/bin/bash
cappucc:##gcappucc:9140:310::/home/home1/ugrad/cappucc:/bin/bash
abbott:##tabbott:11223:280::/home/home1/spring/tabbott:/bin/bash
smith1:##msmith1:10431:250::/home/home2/grads/smith1:/bin/bash
henson:##mhenson:9498:310::/home/home1/ugrad/henson:/bin/bash
naeemi:##hnaeemi:10090:310::/home/home1/ugrad/naeemi:/bin/bash
rucker:##drucker:9041:310::/home/home1/ugrad/rucker:/bin/bash
rao:##rrao:8825:320::/home/home3/phd/rao:/bin/bash

1) remove all the ":"
Hint: use the command "cut"
2) substitute grads with others and ugrad with another

3) print username, userID and Home directory
Hint: use awk

4) Format the output in "3)" with headers:

USER Name User ID Home Directory
xxxxx xxxxx xxxxx


would appriciate the help.

homey 11-05-2005 11:00 AM

Quote:

Hint: use the command "cut"
Yep! Also with sed and awk you should have no problem finishing your homework before Monday.

alienDog 11-05-2005 11:46 AM

in addition to cut, sed and awk, tr will also work ;)

Tinkster 11-05-2005 02:01 PM

Keep up the good work, guys, hints to how to solve
his homework are fine, a plain doing his job isn't.

To batalia:
Please read the rules you agreed to again - homework
questions ARE NOT ALLOWED.


Cheers,
Tink

batalia 11-06-2005 07:04 PM

I have 90% of this done. The commands I've used are

sed 's/:/ /g' project1

That removes all the semicolons

sed 's/grads/others/g' project1

sed 's/ugrad/another/g' project1

What I'm having problems with is adding the headings.

I've used the awk '{print "USER Name User ID Home Directory" }' project1

but that command places the USER Name User ID Home Directory at the ends of each line. Can someone help me with fixing this line of printing so I can add the headers to the file.

homey 11-06-2005 10:59 PM

Quote:

I've used the awk '{print "USER Name User ID Home Directory" }' project1
You're coming right along with that. I would use BEGIN to print the header and tabs ( \t ) between them.
Also, if you have replaced colons ( : ) with spaces, then I would use the space as field separator in awk

batalia 11-07-2005 07:39 AM

i've used this type of command

awk 'begin{print "USER Name User ID Home Directory" }' but what happens is ir prints username user id homedirectoy at the end of each line on all of the lines and not just ontop one time. Don't know what I'm doing wrong on this one.

homey 11-07-2005 08:18 AM

Quote:

awk 'begin{print "USER Name User ID Home Directory" }'
The word BEGIN is in caps. The header is only part of the picture, you still need to print the colums with data in them.
Don't forget to separate the columns with tabs.
For example: 'BEGIN {User Name\tUser ID\t\tHome Directory}

Don't forget the Input Field Separator. For example: awk -F:
Don't forget the Output Field Separator. For example: {OFS="\t\t"}
Don't forget to choose the columns which you want to print.
For example: {print$1,$3,$6}' project1

batalia 11-07-2005 02:41 PM

I think thats what I needed. Thanx a ton, I'm in college and the teacher teaches the class like everyone knows linux 100%. I was missing this {print$1,$3,$6}' project1 and the rest. Was giving me all kinds of strange results and linux is not like c++ or Visual basic. It doesnt tell you whats wrong. Thanx again.


All times are GMT -5. The time now is 04:40 PM.