Split strings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Split strings
# 8  
Old 01-29-2011
the simpler way is :
Code:
 echo "123456" | sed 's/\(.\)/\1 /g'

This User Gave Thanks to homeboy For This Post:
# 9  
Old 01-30-2011
Split string

Sorry got tied up with a issue:
Anyway here is some code samples to help
say we want to initialize an array with "0"

[CODE]
cat $1 |nawk 'BEGIN {
for(x=1;x<27;x++){
a[x]=0
}
Since awk doesn't do multi dimensional arrays say we build 1 for the Alpha by postion
b[1]="A"
b[2]="B"
b[3]="C"
b[4]="D"
..............
}

Now lets walk the input string $0 is the line in and as we hit
the Alpha Chars we increment the correct array value.

[CODE]
{
str_max=(length($0))
for(x=1;x<=str_max;x++){
z=toupper(substr($0,x,1))
if(z == "A"){
a[1]++
}else if(z == "B"){
a[2]++
}else if(z == "C"){
a[3]++

............

Then the end like this
}END{
for(x=1;x<27;x++){
printf("%s: %s\n",b[x],a[x])
}
}'
[\CODE]

output would be like: for the line you supplied.
A: 28
B: 0
C: 9
D: 0
E: 0
F: 0
G: 19

Last edited by aix-guy; 01-30-2011 at 01:46 AM.. Reason: my sample needed the BEGIN
# 10  
Old 01-30-2011
Code:
sed 's/./& /g'

Some awks can do:
Code:
awk '$1=$1' FS=

Or to give an idea of what awk can do
Code:
$ echo "ATGCCAGGGGAGCCCAGAAGGTAAAACTTGATCTGAAATGTATGTTTATATATAATTTAGGTAATCAATTGGCATGTGAA
ATCGCGTATAACGTTTCAGCTTAAAATGCCTATCGCGTATAACGTTTCAGCTTAAAATGCCTATCGCGTATAATGAAGTA" |  
awk '{for(i=1;i<=NF;i++)A[$i" "i]++}END{for(i in A)print i, A[i]}' FS= | sort -k1,1 -k2,2n
A 1 2
A 6 1
A 8 1
A 10 1
A 11 2
A 16 1
A 18 2
A 19 1
A 23 2
A 24 2
A 25 2
A 26 2
A 31 1
A 32 1
A 36 1
A 37 1
A 38 1
A 39 1
A 41 1
A 42 2
A 48 1
A 49 1
A 50 1
A 52 1
A 54 2
A 55 2
A 56 1
A 57 1
A 59 1
A 63 2
A 64 1
A 67 1
A 68 1
A 70 1
A 72 1
A 73 1
A 74 1
A 76 1
A 77 1
A 79 1
A 80 2
C 3 1
C 4 1
C 5 2
C 12 1
C 13 1
C 14 1
C 15 1
C 17 1
C 20 1
C 27 1
C 29 1
C 30 1
C 33 1
C 34 1
C 36 1
C 43 1
C 48 1
C 51 1
C 60 1
C 61 1
C 65 1
C 66 1
C 67 1
C 73 1
G 3 1
G 4 1
G 6 1
G 7 1
G 8 1
G 9 1
G 10 1
G 12 1
G 13 1
G 17 1
G 19 1
G 20 1
G 21 1
G 28 1
G 30 1
G 35 2
G 37 1
G 40 1
G 44 2
G 50 1
G 59 1
G 60 1
G 61 1
G 66 1
G 68 1
G 71 1
G 72 1
G 75 1
G 76 1
G 78 2
T 2 2
T 7 1
T 9 1
T 14 1
T 15 1
T 16 1
T 21 1
T 22 2
T 27 1
T 28 1
T 29 1
T 31 1
T 32 1
T 33 1
T 34 1
T 38 1
T 39 1
T 40 1
T 41 1
T 43 1
T 45 2
T 46 2
T 47 2
T 49 1
T 51 1
T 52 1
T 53 2
T 56 1
T 57 1
T 58 2
T 62 2
T 64 1
T 65 1
T 69 2
T 70 1
T 71 1
T 74 1
T 75 1
T 77 1
T 79 1

This User Gave Thanks to Scrutinizer For This Post:
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. 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

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

5. Shell Programming and Scripting

awk to split one field and print the last two fields within the split part.

Hello; I have a file consists of 4 columns separated by tab. The problem is the third fields. Some of the them are very long but can be split by the vertical bar "|". Also some of them do not contain the string "UniProt", but I could ignore it at this moment, and sort the file afterwards. Here is... (5 Replies)
Discussion started by: yifangt
5 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. 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

8. Shell Programming and Scripting

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... (2 Replies)
Discussion started by: my_Perl
2 Replies

9. UNIX for Dummies Questions & Answers

Split a file with no pattern -- Split, Csplit, Awk

I have gone through all the threads in the forum and tested out different things. I am trying to split a 3GB file into multiple files. Some files are even larger than this. For example: split -l 3000000 filename.txt This is very slow and it splits the file with 3 million records in each... (10 Replies)
Discussion started by: madhunk
10 Replies

10. Shell Programming and Scripting

How to concatenate two strings or several strings into one string in B-shell?

like connect "summer" and "winter" to "summerwinter"? Can anybody help me? thanks a lot. (2 Replies)
Discussion started by: fontana
2 Replies
Login or Register to Ask a Question