Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extract First Seven Characters Into A Variable

Status
Not open for further replies.

Grizzly521

Technical User
Oct 12, 2009
7
US
Hi Im trying to extract the first seven characters of everyline of a file, then set those seven characters as a variable.

The text file is generated by:

who >> who.txt

A line of text looks like:

Username pts/## otherdata

So Im trying to get the usernames which on my system are always seven characters. Then store them as $var, then run

MyScript $var

Then get the username off of the next line, repeat.

Another way would be to extract everything up to the first space character.

Ive been trying to get this done but I always end up just extracting the whole line.

Any help would be great.

Thanks!
 
Something like this perhaps?

Code:
while read username otherstuff
do
    MyScript $username
done < who.txt

Or another way:

Code:
for username in $(awk '{print $1}' who.txt)
do
    MyScript $username
done

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top