Split strings based on length


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Split strings based on length
# 1  
Old 07-13-2009
Split strings based on length

Hi All

I am very much in need of help splitting strings based on length in Perl. e.g.,
Input text is :

International NOUN
Corp. NOUN
's POS
Tulsa NOUN


Output I want is :

International I In Int Inte l al nal onal NOUN
Corp. C Co Cor Corp . p. rp. orp. NOUN
's ' 's __nil__ __nil__ s 's __nil__ __nil__ POS
Tulsa T Tu Tul Tuls a sa lsa ulsa NOUN

Please help me. Thanks in advance.
# 2  
Old 07-13-2009
Code:
while(<DATA>){
	my @tmp=split;
	for(my $i=1;$i<=4;$i++){
		$arr[$i]=substr($tmp[0],0,$i);
		$arr[$i+4]=substr($tmp[0],-$i);
	}
	
	print $tmp[0]," ";
	print join " ", @arr;
	print " ",$tmp[1],"\n";
}
__DATA__
International NOUN 
Corp. NOUN
's POS
Tulsa NOUN

# 3  
Old 07-14-2009
Thanks for the reply. However, when I run it , it has gone to INFINITE loop. Also, please help me on how to read the input from a FILE and write the output into a FILE of the same format.

---------- Post updated 07-14-09 at 01:11 AM ---------- Previous update was 07-13-09 at 05:26 AM ----------

Thanks. It's working fine. I need help to modify above script for strings in utf8 format.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Append 0's based on length

I'm having data like this, "8955719","186497034","0001","M","3" "8955719","186497034","0002","M","10" "8955719","186497034","0003","M","10" "8955719","186497034","0004","M","3" "8955723","186499034","0001","M","3" "8955723","186499034","0002","M","10" "8955723","186499034","0003","M","10"... (3 Replies)
Discussion started by: Artlk
3 Replies

2. Shell Programming and Scripting

Split certain strings in a line for a specific column.

Hi, i need help to extract certain strings/words from lines with different length. I have 3 columns separated by tab delimiter. like below Probable arabinan endo-1,5-alpha-L-arabinosidase A (EC 3.2.1.99) (Endo-1,5-alpha-L-arabinanase A) (ABN A) abnA Ady3G14620 Probable arabinan... (5 Replies)
Discussion started by: redse171
5 Replies

3. Shell Programming and Scripting

awk to sum a column based on duplicate strings in another column and show split totals

Hi, I have a similar input format- A_1 2 B_0 4 A_1 1 B_2 5 A_4 1 and looking to print in this output format with headers. can you suggest in awk?awk because i am doing some pattern matching from parent file to print column 1 of my input using awk already.Thanks! letter number_of_letters... (5 Replies)
Discussion started by: prashob123
5 Replies

4. Shell Programming and Scripting

Split a fixed length file bases on last occurence of string

Hi, I need to split a file based on last occurece of a string. PFB the explanation I have a file in following format aaaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbb ccccccccccccccccccccccccccc ddddddddddddddddddddddddddd 3186rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr... (4 Replies)
Discussion started by: Neelkanth
4 Replies

5. Shell Programming and Scripting

Split strings

Hello I started to learn bash about 1 or 2 weeks, please help me. I have about 200000 strings like these: ATGCCAGGGGAGCCCAGAAGGTAAAACTTGATCTGAAATGTATGTTTATATATAATTTAGGTAATCAATTGGCATGTGAA and I need to split each letter to get: A T G C C A G G G G A G C C C A G A A G G T A A A A C T T... (9 Replies)
Discussion started by: geparada88
9 Replies

6. UNIX for Dummies Questions & Answers

Delete strings in file1 based on the list of strings in file2

Hello guys, should be a very easy questn for you: I need to delete strings in file1 based on the list of strings in file2. like file2: word1_word2_ word3_word5_ word3_word4_ word6_word7_ file1: word1_word2_otherwords..,word3_word5_others... (7 Replies)
Discussion started by: roussine
7 Replies

7. Shell Programming and Scripting

Replace strings based on position and length?

Suppose i have a file which contains thousands of records. e.g adjgmptjadmwpgjmwmd i need to replace the string from 3rd to 8th position using awk script in entire file. And also the positions will be passed as parameter. (3 Replies)
Discussion started by: laknar
3 Replies

8. Shell Programming and Scripting

remove strings of lowercase characters (with minimum length requirement)

Hi all, I want to delete all lowercase characters from my file, but only strings of length 7 and more. For example, how can I go from: JHGEFigeIGDUIirfyfiyhgfoiyfKJHGuioyrDHG To: JHGEFigeIGDUIKJHGuioyrDHG There should be a trick to add to sed 's///g', but I can't figure it out.... (2 Replies)
Discussion started by: elbuzzo
2 Replies

9. Shell Programming and Scripting

how to sort strings by length?

I'm trying to find the longest word in /usr/share/dict/words The first thing I can think of is to sort the content by length then it would be easy to find out, but then i realize theres no option of sort to sort by length. Could you guys please give me some help?:confused: (7 Replies)
Discussion started by: rockbike
7 Replies

10. UNIX for Dummies Questions & Answers

Need find a file based length

Can some please help me? Want to find files over 35 characters in length? I am running HPUX. Would it be possible with find? Thanks in advance (8 Replies)
Discussion started by: J_ang
8 Replies
Login or Register to Ask a Question