Splitting strings


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Splitting strings
# 1  
Old 04-08-2014
Splitting strings

I have a file that has two columns. I first column is an identifier and the second is a column of strings. I want to split the characters in the second column into substrings of length 5. So if the first line of the file has a string of length 10, the output should have the identifier repeated 2 times with each of the length 5 substrings

the input looks like this
Code:
DOG   abcdefghij
CAT   ttttto

the output should look like this
Code:
DOG  abcde
DOG  fghij
CAT   ttttt

any ideas?
# 2  
Old 04-08-2014
Hello,

Following may help you in same.

Code:
awk 'NR==1{match($2,/.....$/); a=substr($2,RSTART,RLENGTH); {match($2,/^...../); c=substr($2,RSTART,RLENGTH); print $1 OFS c ORS $1 OFS a}} NR==2{match($2,/^...../); b=substr($2,RSTART,RLENGTH); print $1 OFS b}' get_dat_check1211234

Output will be as follows.

Code:
DOG abcde
DOG fghij
CAT ttttt

NOTE: Where get_dat_check1211234.ksh is the input file name.


Thanks,
R. Singh

Last edited by RavinderSingh13; 04-08-2014 at 05:16 AM.. Reason: Added input file name
# 3  
Old 04-08-2014
This seems a little bit simpler to me:
Code:
awk '
{	while(length($2) >= 5) {
		print $1, substr($2, 1, 5)
		$2 = substr($2, 6)
	}
}' input_file

which produces the output:
Code:
DOG abcde
DOG fghij
CAT ttttt

I didn't see a discernable pattern to determine whether there should be two or three spaces between output fields, so this script just uses the default single space as the output field separator. If you can explain why your sample output uses different field separators for different output lines, we can try to help you get the desired results.
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 04-08-2014
Try also
Code:
 awk '{for (i=0;i<int(length($2)/5);i++) print $1, substr($2,i*5+1,5)} ' file

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to pass strings from a list of strings from another file and create multiple files?

Hello Everyone , Iam a newbie to shell programming and iam reaching out if anyone can help in this :- I have two files 1) Insert.txt 2) partition_list.txt insert.txt looks like this :- insert into emp1 partition (partition_name) (a1, b2, c4, s6, d8) select a1, b2, c4, (2 Replies)
Discussion started by: nubie2linux
2 Replies

2. UNIX for Dummies Questions & Answers

Issue when using egrep to extract strings (too many strings)

Dear all, I have a data like below (n of rows=400,000) and I want to extract the rows with certain strings. I use code below. It works if there is not too many strings for example n of strings <5000. while I have 90,000 strings to extract. If I use the egrep code below, I will get error: ... (3 Replies)
Discussion started by: forevertl
3 Replies

3. UNIX for Dummies Questions & Answers

Splitting strings based on delimiter

i have a snippet from server log delimited by forward slash. /a/b/c/d/filename i need to cut until last delimiter. So desired output should look like: /a/b/c/d can you please help? Thanks in advance. (7 Replies)
Discussion started by: alpha_1
7 Replies

4. Shell Programming and Scripting

awk Splitting strings

Hi All, There is a file with a data. If the line is longer than 'n', we splitting the line on the parts and print them. Each of the parts is less than or equal 'n'. For example: n = 2; "ABCDEFGHIJK" -> length 11 Results: "AB" "CD" EF" GH" "IJ" "K" Code, but there are some errors.... (9 Replies)
Discussion started by: booyaka
9 Replies

5. Shell Programming and Scripting

splitting tab delimited strings

hi i have a requirement to input a string to a shell script and to split the string to multiple fields, the string is copied from a row of three columns (name,age,address) in an excel sheet. the three columns (from excel) are seperated with a tab when pasted in the command prompt, but when the ... (2 Replies)
Discussion started by: midhun19
2 Replies

6. Shell Programming and Scripting

Delete lines in file containing duplicate strings, keeping longer strings

The question is not as simple as the title... I have a file, it looks like this <string name="string1">RZ-LED</string> <string name="string2">2.0</string> <string name="string2">Version 2.0</string> <string name="string3">BP</string> I would like to check for duplicate entries of... (11 Replies)
Discussion started by: raidzero
11 Replies

7. Shell Programming and Scripting

Splitting Concatenated Words With Largest Strings First

hello, I had posted earlier help for a script for splitting concatenated words . The script was supposed to read words from a master file and split concatenated words in the slave/input file. Thanks to the help I got, the following script which works very well was posted. It detects residues by... (14 Replies)
Discussion started by: gimley
14 Replies

8. 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

9. Programming

Splitting strings from file

Hi All I need help writing a Java program to split strings reading from a FILE and writing output into a FILE. e.g., My input is : International NNP Rockwell NNP Corp. NNP 's POS Tulsa NNP unit NN said VBDExpected output is: International I In Int Inte l al... (2 Replies)
Discussion started by: my_Perl
2 Replies

10. UNIX for Dummies Questions & Answers

splitting strings

Hi you, I have the following problem: I have a string like the followings: '166Mhz' or '128MB' or '300sec' or ... What I want to do is, I want to split the strings in a part with the numbers and a part with letters. Since the strings are not allway three digits and than text i couldn't do... (3 Replies)
Discussion started by: bensky
3 Replies
Login or Register to Ask a Question