To split a string to obtain the words delimited by whitespaces


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To split a string to obtain the words delimited by whitespaces
# 1  
Old 08-06-2007
To split a string to obtain the words delimited by whitespaces

Please can someone thow some light what is the best way to split a string to obtain the words delimited by whitespaces.
# 2  
Old 08-06-2007
you could use awk,

split + perl

Code:
perl -e ' while (<>) { chomp; my @arr = split(/ /); foreach (@arr) { print "$_\n" } }'  filename


Last edited by matrixmadhan; 08-06-2007 at 03:09 PM.. Reason: added example
# 3  
Old 08-06-2007
awk example:
Code:
echo "Hi there,  I am a sentence." | awk '{ for(i=1; i <= NF; i++) {print $i } }'

shell example:
Code:
#/bin/ksh
splitit()
{
      set -A arr  $*
      let i=0
      while [[ $i -lt ${#arr[*]} ]]
      do    
             echo ${arr[i]}
             let i=$i+1
      done 
}
splitit hi there I am a sentence.

# 4  
Old 08-06-2007
Code:
mStr='Today is Monday, August 06 2007'
for mEach in $mStr
do
  echo 'Each = '$mEach
done

# 5  
Old 08-06-2007
Depend on what you want to do with the stripped array:
for simple usages cut is good enough : cut -d" " f1,2,3 $your_string
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Construct Array from String seperated by different whitespaces

My string variable which gets the output from the result of a database query has values as below: line="2019-09-11 15:17:55 CR1234 anonymous Deployed DR_only Back_APP" I wish to construct an array (my_array) which should have entries as below. Note: 1. The first... (6 Replies)
Discussion started by: mohtashims
6 Replies

2. Shell Programming and Scripting

Awkscript to reduce words delimited with comma on right hand to columns

I have a large database with the following structure: Indicword,Indicword,Indicword=English on a line. Not all lines will have this structure. Some might have a single word mapping to a single word in Indic. An example will make this clear ... (4 Replies)
Discussion started by: gimley
4 Replies

3. Shell Programming and Scripting

Help/Advise please for converting space delimited string variable to comma delimited with quote

Hi, I am wanting to create a script that will construct a SQL statement based on a a space delimited string that it read from a config file. Example of the SQL will be For example, it will read a string like "AAA BBB CCC" and assign to a variable named IN_STRING. I then concatenate... (2 Replies)
Discussion started by: newbie_01
2 Replies

4. Shell Programming and Scripting

How to split a file with delimited string?

I have a unix file text.txt with below content aaaaa bbbbbbb cccccccccc As of 2013 ddddddddd eeeeeeeeee eeeeeeeee fffffffff As of 2014 gggggggggggg hhhhhhhhh iiiiiiiiiiiiiiii As of 2016 Now I've to split this file with each file ending with line 'As of' . Please suggest how can I do... (6 Replies)
Discussion started by: Steven77
6 Replies

5. Shell Programming and Scripting

Split a free form text delimited by space to words with other fields

Hi, I need your help for below with shell scripting or perl I/P key, Sentence customer1, I am David customer2, I am Taylor O/P Key, Words Customer1,I Customer1,am Customer1,David Customer2,I Customer2,am Customer2,Taylor (4 Replies)
Discussion started by: monishathampi
4 Replies

6. UNIX for Dummies Questions & Answers

How to use the join command to obtain tab delimited text files as an output?

How do you use the join command and obtain tab delimited text files as an output? Thanks! (2 Replies)
Discussion started by: evelibertine
2 Replies

7. Shell Programming and Scripting

Substring in string with whitespaces

hello, i have this string: variable="1234 /PARAMETER_1:text /PARAMETER_2:othertext" i tried to do expr match $variable '.*\(*\)' but i keep getting expr error i need to extract the word text... thank you (4 Replies)
Discussion started by: Aloush89
4 Replies

8. UNIX for Dummies Questions & Answers

number of words in delimited string

Hi, I'm looking for one liner code for counting number of words in a delimited string.. I know about wc -w ..but if i give wc -w a.txt,b.txt it won't work with delimited sting as it was looking for files with a.txt and b.txt I know there will be a simple solution..i couldn't... (5 Replies)
Discussion started by: ammu
5 Replies

9. Shell Programming and Scripting

separating comma delimited words

Hi, I have file with text ________________________________ GROUP:firstname1.lastname1,first_name2.last_name2,first_name3.last_name3 HEAD:firstname.lastname ________________________________ I need help to pick the names separately ie.. Need out put as var1 =firstname1.lastname1... (4 Replies)
Discussion started by: rider29
4 Replies

10. Shell Programming and Scripting

How to split pipe delimited file

I have a pipe delimited input file as below. First byte of the each line indicate the record type. Then i need to split the file based on record_type = null,0,1,2,6 and create 5 files. How do i do this in a ksh script? Pls help |sl||SL|SL|SL|1996/04/03|1988/09/15|C|A|sl||||*|... (4 Replies)
Discussion started by: njgirl
4 Replies
Login or Register to Ask a Question