Parsing the string into several rows


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parsing the string into several rows
# 1  
Old 08-28-2010
Parsing the string into several rows

I'm getting the input row in this format it contains 2 alphabets followed by numbers between 1 and 7 and again 2 alphabets followed by numbers between 1 and 7.

Now I need to parse this input into several output rows in this format 2 alphabets followed by each number occurrence

suppose this is input format

Code:
ID      Code
1       AB1234567DF345    
2       FD357DC34    
3       DS234


And this is my desired requirement

Code:
ID      Code
1       AB1    
1       AB2   
1       AB3    
1       AB4 
1       AB5 
1       AB6 
1       AB7 
1       DF3
1       DF4
1       DF5 
2       FD3 
2       FD5   
2       FD7 
2       DC3   
2       DC4 
3       DS2   
3       DS3 
3       DS4

Any help is highly appreciated


thank you

Last edited by Scott; 08-29-2010 at 06:41 AM.. Reason: Please use code tags
# 2  
Old 08-29-2010
Hi,

Try this,

Code:
 awk '{len=length($2);for (i=1;i<=len;i++) {if ( substr($2,i,1) ~ /[1-7]/){printf "%d %s%s\n", $1,ch,substr($2,i,1)}else{ch=substr($2,i,2);i++;} }}' input.txt

# 3  
Old 08-29-2010
Thanks .... I will try it....
# 4  
Old 08-29-2010
Code:
awk '
NR==1{print;next} 
{
  split($2,a,"")
  for (i=3;i<=length($2);i++) 
    {
      if (a[i]~/[A-Z]/) {a[1]=a[i];a[2]=a[i+1];i+=2}
      print $1 "\t" a[1]a[2]a[i]
    }
}' infile


Last edited by rdcwayx; 08-29-2010 at 10:09 PM..
# 5  
Old 08-30-2010
perl

Code:
while(<DATA>){
  if($.==1){
   print;
   next;
  }
  my @tmp = split;
  my $key=$tmp[0];
  my @t = split /(?<=[0-9])(?=[A-Z])/,$tmp[1];
  foreach(@t){
    if(/([A-Z]*)/){
      my $tmp = $1;
      s/(?<=[0-9])(?=[0-9])/"\n".$key." ".$tmp/eg;
      print $key," ",$_;
    }
    print "\n";
  }
}
__DATA__
ID      Code
1       AB1234567DF345
2       FD357DC34
3       DS234

# 6  
Old 08-31-2010
Yet another Perl solution:

Code:
$
$
$ cat f9
1       AB1234567DF345
2       FD357DC34
3       DS234
$
$
$ ##
$ perl -lne '/^(\d+)\s+(\w+)$/ and ($x,$y)=($1,$2);
             while ($y =~ /(\D+)(\d+)/g) {
               ($p,$q) = ($1,$2);
               while ($q =~ /(.)/g) {print "$x  $p$1"}
             }' f9
1  AB1
1  AB2
1  AB3
1  AB4
1  AB5
1  AB6
1  AB7
1  DF3
1  DF4
1  DF5
2  FD3
2  FD5
2  FD7
2  DC3
2  DC4
3  DS2
3  DS3
3  DS4
$
$
$

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parsing a long string string problem for procmail

Hi everyone, I am working on fetchmail + procmail to filter mails and I am having problem with parsing a long line in the body of the email. Could anyone help me construct a reg exp for this string below. It needs to match exactly as this string. GetRyt... (4 Replies)
Discussion started by: cwiggler
4 Replies

2. Shell Programming and Scripting

Parsing a CSV file and deleting all rows on condition

Hello list, I am working on a csv file which contains two fields per record which contain IP addresses. What I am trying to do is find records which have identical fields(IP addresses) which occur 4(four) times, and if they do, delete all records with that specific identical field(ip address). ... (4 Replies)
Discussion started by: landossa
4 Replies

3. Shell Programming and Scripting

Need help on parsing string

for i in `cat list`;do lol=`curl -m 2 -s ${i} | grep 'class=info' | cut -d '>' -f14 | cut -d '<' -f1 | sed '/^$/d'`; if ;then echo "$i,$lol" >> dirty; echo "$i,$lol";fi; done cut: you must specify a list of bytes, characters, or fields Try `cut --help' for more information. it gave me that... (0 Replies)
Discussion started by: p33plime
0 Replies

4. Shell Programming and Scripting

String parsing

Hi, name=VDSL_TTV_ HN_SUB create coid=MA5603U phone=5678 portpolicy=APortSelectionPolicy rfu10=TTV rfu3=Dot1q sz7_portmode=VDSL2 rfu5=1234 srprofile.sy_profname=$ADSL_TTV_SubProfile1 I have a line like this. Its a single line.I need the output as name=VDSL_TTV_ HN_SUB create... (1 Reply)
Discussion started by: giri_luck
1 Replies

5. Shell Programming and Scripting

parsing rows

Hi, I have a file that looks like this (tab seperated): Barry -3 -4 -5 -10 -4 6 -8 20 -6 NaN NaN NaN Brend -2 4 -3 -7 -3 8 -9 -10 -6 NaN NaN NaN NaN NaN Harry -10 -9 -40 6 -7 3 -7 -2 -5 NaN NaN NaN NaN NaN NaN NaN I want to print the first column... (4 Replies)
Discussion started by: gisele_l
4 Replies

6. Shell Programming and Scripting

Parsing of file for Report Generation (String parsing and splitting)

Hey guys, I have this file generated by me... i want to create some HTML output from it. The problem is that i am really confused about how do I go about reading the file. The file is in the following format: TID1 Name1 ATime=xx AResult=yyy AExpected=yyy BTime=xx BResult=yyy... (8 Replies)
Discussion started by: umar.shaikh
8 Replies

7. UNIX for Advanced & Expert Users

Parsing String, Search then display rows

Get occurence of "open" considering duplicates(get the last open). Once you are pointing to the last open count 2 rows to get the correct data. Every begin and end statement, there is a "close" and "open". There can be many "close" and "open" within the begin and end statement but we are... (9 Replies)
Discussion started by: buddyme
9 Replies

8. Programming

parsing string in c

how can i remove the special characters hi iam print the string variable . suppse: while(str!=NULL) printf("******* %s ********** %d ",str,strlen(str)); output as: ****srinu ******** 5 **** phani******** 63 ****srinu ******** 5 **** phani******** 63 so my problem is how can i... (3 Replies)
Discussion started by: phani_sree
3 Replies

9. UNIX for Dummies Questions & Answers

String Parsing help

I have a string like string_name = data1 data2 data3 .... Now I need to take take these datas into individual variables such that I finally have something like string1 = data1 string2 = data2 string3 = data3 (5 Replies)
Discussion started by: jasjot31
5 Replies

10. Shell Programming and Scripting

Need help parsing a string

Hi, I'm writing a shell script that outputs, among other things, some of the information that is outputted by the mysqladmin status command. The output of the command looks like this: Uptime: 816351 Threads: 19 Questions: 80719739 Slow queries: 1419 Opens: 15903523 Flush tables: 1 Open tables:... (6 Replies)
Discussion started by: achieve
6 Replies
Login or Register to Ask a Question