The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Career Guidance sufi_431 What's on Your Mind? 1 10-23-2007 07:54 AM
NIM Guidance scottsl AIX 1 06-12-2006 08:18 AM
Sed help needed stevefox Shell Programming and Scripting 5 12-04-2005 09:44 PM
Looking for guidance in obtaining a job in Unix Administration pkali290 SCO 4 12-08-2003 11:51 PM
select in ksh, any guidance for bash? Shakey21 UNIX for Dummies Questions & Answers 1 05-09-2002 09:40 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-07-2008
Registered User
 

Join Date: Mar 2008
Posts: 10
Stumble this Post!
A little guidance needed :)

Hi guys, I'm learning the ropes of BASH and am doing a few exercises to see if its sinking in but have gotten stuck on something I KNOW is looking at me right in the face but just isn't registering.

I'm creating a script that needs to get specific strings from a line. So using the "ls -l <filename>" command, i get a string with 6 fields (permissions and so forth). No i know there is a way to get an individual field from the 6 fields and store it but i just cant remember it. I believe its a pipe to the grep command but I'm unsure and for all i know there is an easier method.

Any help would be appreciated!

Code:

str = $(ls -l <filename>) #To get the info

now i just need to "split" the data - if it was perl, easy enough :P.

Edit: No AWK or SED.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-07-2008
Moderator
 

Join Date: Feb 2007
Posts: 1,958
Stumble this Post!
Quote:
Originally Posted by shadow0001 View Post
Edit: No AWK or SED.
This sounds like a homework question, try:

Code:
man cut
Regards
Reply With Quote
  #3 (permalink)  
Old 03-07-2008
Registered User
 

Join Date: Mar 2008
Posts: 10
Stumble this Post!
Indeed you could call it something like that.. Still if I'm not going to get anywhere sitting around and fiddling about then i may as well ask. Otherwise im not learning anything .. just wasting a bit of time.

Cheers non the less!

Code

str=$(ls -l <filename> | cut -d ' ' -f 1)

-d Delimiter (space in my case)
-f fields (1st one in my case)

Just a brief explanation if someone was stuck on a similar situation.

Last edited by shadow0001; 03-07-2008 at 07:35 AM.
Reply With Quote
  #4 (permalink)  
Old 03-07-2008
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,846
Stumble this Post!
The default field separator is a space. -d will not break anything but it is not required.
Reply With Quote
  #5 (permalink)  
Old 03-07-2008
Registered User
 

Join Date: Mar 2008
Posts: 10
Stumble this Post!
I tried that however it did not work - brought up the entire line with out the option. Unix doesn't like me very much lol.

A final (i hope) question. If i had the work "command" and i wanted to split it into characters, how would i go about doing this ? See i need to be able to process each character individually using a loop however i am not aware of a command that allows me to do this.

Thanks again.
Reply With Quote
  #6 (permalink)  
Old 03-07-2008
Registered User
 

Join Date: Aug 2007
Posts: 9
Stumble this Post!
position=1

#put the following in a loop
#cut -c stands for column
while [ $position -ge 8 ]
do
echo `cut -c$position`
position=`expr $position + 1`
done
Reply With Quote
  #7 (permalink)  
Old 03-07-2008
Registered User
 

Join Date: Aug 2007
Posts: 9
Stumble this Post!
position=1
word="command"
#put the following in a loop
#cut -c stands for column
while [ $position -ge 8 ]
do
echo `echo $word |cut -c$position`
position=`expr $position + 1`
done
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 07:46 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0