The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
split string using separetor rinku Shell Programming and Scripting 10 06-16-2009 02:01 PM
[KSH] Split string into array piooooter Shell Programming and Scripting 3 09-01-2007 12:22 PM
KSH split string into variables drd_2b Shell Programming and Scripting 5 04-23-2006 07:07 PM
split string help senthilk615 Shell Programming and Scripting 4 03-27-2006 06:43 PM
split a file at a specified string jpl35 Shell Programming and Scripting 6 07-04-2002 11:41 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-08-2006
gazingdown gazingdown is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 1
split a string

Hi

I have a script that loops though lines of a file and reads each line in to a variable ($LINE).

I want to look at the line and split it into it's constituent parts.

e.g. a line might be "This is a string"
I want to then have variables set to each element thus:
A=This
B=is
C=a
D=string

I'm guessing it should be simple but cannot figure it out.
  #2 (permalink)  
Old 02-08-2006
gauravgoel gauravgoel is offline
Registered User
  
 

Join Date: Dec 2005
Location: India
Posts: 218
can you please elaborate a bit more, maybe with some sample input and output.
And will the no. of word be fixed in the string
  #3 (permalink)  
Old 02-08-2006
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,643
Code:
while read a b c d e f
do
    echo "a = $a"
    echo "b = $b"
    echo "c = $c"
    echo "d = $d"
    echo "e = $e"
    echo "f = $f"
done < myfile
Add as many variables as the largest possible number of words in a line. Otherwise, consider awk which does all this automatically.
  #4 (permalink)  
Old 02-09-2006
Abhishek Ghose Abhishek Ghose is offline
Registered User
  
 

Join Date: Sep 2005
Location: Chennai
Posts: 81
whether you need awk depends on what you want to after the assignment. If you want the variable outside the awk statement maybe using awk may not be very helpful. But, in situations , where you require processing data, this is seldom the case---you can do everything you want inside awk.
awk '{for(i=1;i<=NF;++i) { print "Field " i " is "$i} print ""}' file_name for example prints out all fields, in all lines in the file
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 06:16 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0