FacebookTwitter
Hatrack River Forum   
my profile login | search | faq | forum home

  next oldest topic   next newest topic
» Hatrack River Forum » Active Forums » Books, Films, Food and Culture » More help needed with ze Linux (appending)

   
Author Topic: More help needed with ze Linux (appending)
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
Ok, so I'm at the point now where when I write a name to a file I want to append a # if there is a multiple of that name for example.

y="Blayne Bradley"
echo $y
[cs326_2@Linux2.cs] Blayne Bradley

echo `echo $y | cut -d' ' -f2 | cut -c1-3``echo $y | cut -c1-3`

[cs326_2@Linux2.cs] BraBla

[cs326_2@Linux2.cs] x="`echo $y | cut -d' ' -f2 | cut -c1-3``echo $y | cut -c1-3`"

[cs326_2@Linux2.cs] echo $x
BraBla

Now I want to write to a file and I found that '>> Secure' works just fine, however what happens that when this automatic script is given a random file with a bunch of names and I get like names like this:

Blair, Braden
Blayne, Bradley
Black, Brain
Blaine, Brakensteinerubermiester

The script when finished will just give me 4 "BraBla"'s and when I give the password to them as well how can we tell whose user id is whose?

(btw the point of the script is to take a list of names, make a user id for each of em and make a random password for each of them and then write the full names, with the user id's and the paswords to a separate file.)

Firstly I'm figuring out how to append a number to the end of one of these said names, except the append sed command prints the character on the line after the userid and insert does it on the line before and I have no idea how to get it after the id. ANd I've been tearing through my Linux Shells by Example book and can find nothin.

Aside from the append command I'm putting idea's on paper on how to detect if a name already exists so far its like:

if [finds] $x, then >> Secure [/w] appended # (probly $x+i)

however when I try to test a form of this:

awk '/$x (#which should equal BraBla)/' Secure

it should've printed out all lines with BraBla to test to see if I can use command substitution in this, the command "works" as the "[cs326_2@Linux2.cs]" shows on the next line but displayed no output. *le sigh* what is wrong. Am I on the right track?

IP: Logged | Report this post to a Moderator
fugu13
Member
Member # 2859

 - posted      Profile for fugu13   Email fugu13         Edit/Delete Post 
How easy this is depends on a few things. State exactly how the problem is stated.

Btw, you may find it (far) easier to use awk for your purposes.

code:
awk '{split($0,names," "); fir = substr(names[1],1,3); las = substr(names[2],1,3); print $0, fir las}'

might give you some ideas.
Posts: 15770 | Registered: Dec 2001  |  IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
-The problem isn't state it runs but displays no output when I try to see if I could do a command substitute the entire cut function into the $x variable.

-When I try to append a number value it doesn't append it at the end of the name but instead appends it to the next line, I wish to know how to append/insert the character to the end of a name not on the line next or before.

IP: Logged | Report this post to a Moderator
fugu13
Member
Member # 2859

 - posted      Profile for fugu13   Email fugu13         Edit/Delete Post 
The problem was stated somehow; tell me how. What, specifically, did your teacher tell you to do?

One of the things it would be important to know is, does this script have to take into account existing usernames, or can it deal with just processing a list and ensuring all the usernames that result are unique?

As for adding a letter to the end of a line, stop thinking about it as inserting something in the file, think of it as reading everything in the file and outputting it again, with something at the end of the line(s) you want to modify. Use awk. Unix is based on a philosophy of piping text between commands, not modifying files in place.

Posts: 15770 | Registered: Dec 2001  |  IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
The assignment is to create a script that can take any list of names, make a unique userid based off of the first 3 chars of their first/last names (append a number if multiple) and from their createa random password based off of the Redhat Linux dictionary and write their full names, userid's and passowrd to a file.
IP: Logged | Report this post to a Moderator
King of Men
Member
Member # 6684

 - posted      Profile for King of Men   Email King of Men         Edit/Delete Post 
Another bit of useful info, what shell are you using? Incidentally, doesn't Hatrack have an anti-homework policy? Most forums do, I think.
Posts: 10645 | Registered: Jul 2004  |  IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
I'm asking for advice and what not, not for the actual code, unless of course I can't find said code in my Linux manual.

I'm pretty sure its bash shell.

IP: Logged | Report this post to a Moderator
fugu13
Member
Member # 2859

 - posted      Profile for fugu13   Email fugu13         Edit/Delete Post 
Use awk.

Keep track of repetitions of userids. I suggest looking at the use of strings as array indices: http://www.vectorsite.net/tsawk2.html#m6

Perhaps consider how you might use an array of that sort to keep track of how many times a name is used, and what you might check in order to decide whether or not to append a number (and whether or not you might already have some handily unique number to append).

Think not of appending, but what all you want to write on a given line and how print works in awk.

Posts: 15770 | Registered: Dec 2001  |  IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
i c, ok I'll keep it in mind for when I get back to it.
IP: Logged | Report this post to a Moderator
   

   Close Topic   Feature Topic   Move Topic   Delete Topic next oldest topic   next newest topic
 - Printer-friendly view of this topic
Hop To:


Contact Us | Hatrack River Home Page

Copyright © 2008 Hatrack River Enterprises Inc. All rights reserved.
Reproduction in whole or in part without permission is prohibited.


Powered by Infopop Corporation
UBB.classic™ 6.7.2