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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Split a free form text delimited by space to words with other fields
# 1  
Old 02-24-2013
Code 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

Thanks in advace!

Monisha

Last edited by monishathampi; 02-24-2013 at 05:38 PM..
# 2  
Old 02-24-2013
How about an awk program?
Code:
awk -F, 'NR==1{$2="Words";print}NR>1{p=$1;split($2,A," ");for(i in A) print p, A[i]}' OFS=, filename

# 3  
Old 02-24-2013
thanks, will try it and reply..appreciate your help..
more replies are welcome Smilie
# 4  
Old 02-24-2013
Quote:
Originally Posted by bipinajith
How about an awk program?
Code:
awk -F, 'NR==1{$2="Words";print}NR>1{p=$1;split($2,A," ");for(i in A) print p, A[i]}' OFS=, filename

If it is necessary to preserve word order, then the use of for (i in A) is incorrect. The order in which that for-loop visits the array members is unspecified. It may work today with one awk implementation and fail in the future with another.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 5  
Old 02-24-2013
You are absolutely correct Alister. Thanks for pointing that out. Smilie

Here is a modified version:
Code:
awk -F, 'NR==1{$2="Words";print}NR>1{p=$1;n=split($2,A," ");for(i=1;i<=n;i++) print p, A[i]}' OFS=, file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rearrange fields of delimited text file

I want to rearrange the fields of delimited text file after sorting first line (only): input file: a_13;a_2;a_1;a_10 13;2;1;10 the result should be: a_1;a_2;a_10;a_13 1;2;10;13 any help would be appreciated andy (20 Replies)
Discussion started by: andy2000
20 Replies

2. UNIX for Dummies Questions & Answers

Changing only the first space to a tab in a space delimited text file

Hi, I have a space delimited text file but I only want to change the first space to a tab and keep the rest of the spaces intact. How do I go about doing that? Thanks! (3 Replies)
Discussion started by: evelibertine
3 Replies

3. UNIX for Dummies Questions & Answers

Swap two rows in a space delimited text file?

Hi, How do you swap two rows in a space delimited text file? Thanks! (4 Replies)
Discussion started by: evelibertine
4 Replies

4. UNIX for Dummies Questions & Answers

Adding tags to a specific column of a space delimited text file

I have a space delimited text file with two columns. I would like to add NA to the first column of the text file. Input: 19625 10.4791768259 19700 10.8146489183 19701 10.9084026759 19702 10.9861346978 19703 10.9304364984 Output: NA19625 10.4791768259 NA19700 10.8146489183... (1 Reply)
Discussion started by: evelibertine
1 Replies

5. UNIX for Dummies Questions & Answers

How do you view specific columns from a space delimited text file?

I have a space delimited text file with 1,000,000+ columns? I would only like to view specific ones (let's say through 1:10), how can I do that? Thanks! (3 Replies)
Discussion started by: evelibertine
3 Replies

6. UNIX for Dummies Questions & Answers

Deleting cells that contain a specific number only from a space delimited text file

I have this space delimited large text file with more than 1,000,000+ columns and about 100 rows. I want to delete all the cells that consist of just 2 (leave 2's that are not by themselves intact): File before modification aa bb cc 2 NA100 dd aa b1 c2 2 NA102 de File after modification... (1 Reply)
Discussion started by: evelibertine
1 Replies

7. UNIX for Dummies Questions & Answers

Deleting columns from a space delimited text file

I have a space delimited text file with 1,000,000+ columns and 100 rows. I want to delete columns 2 through 5 (2 and 5) included from the text file. How do I do that? Thanks. (3 Replies)
Discussion started by: evelibertine
3 Replies

8. Shell Programming and Scripting

replace 3rd field of space delimited text file

how to replace the 3rd colum? Each line begins similarly, but they all ends variously. XX YY 03 variable text here XX YY 03 more variable text here XX YY 03 even more variable text here really long setence XX YY 03 variable numbers also appear 03 11. 123 456 XX YY 03 the occasional comma,... (4 Replies)
Discussion started by: ajp7701
4 Replies

9. UNIX for Dummies Questions & Answers

Searching for text in a Space delimited File

Hi I am trying to search a firewall syslog space delimeted file for all of the different tcp and udp destination ports. I know that grep will find lines that contain specific text. And I have tried using the the the cut command to cut out of the file certain colums. However the test I am... (6 Replies)
Discussion started by: andyblaylock
6 Replies

10. Shell Programming and Scripting

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. (4 Replies)
Discussion started by: Sudhakar333
4 Replies
Login or Register to Ask a Question