Putting together substrings if pattern is matched


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Putting together substrings if pattern is matched
# 8  
Old 03-14-2014
Yes I had interpreted it such that you wanted to leave labels that appear only once untouched, and that labels would appear once or twice.

Yoda's suggestion something similar, but cuts off 8 characters at the end of single entries.

It seems to me MadeinGermany's approach is closest to what you are looking for..
# 9  
Old 03-14-2014
Your solution is actually what I was looking for, expect that for single occurring %ID's are as long as the input. So something like this would be the same in the output as in the input.

Quote:
%TESGO
dfkjsdfgogogocatcatdogtwelvetwentygogogo
# 10  
Old 03-14-2014
Could you post an input sample with also single and maybe triple entries (?) and what the output should look like?
# 11  
Old 03-14-2014
An example for single occurrence looks like this.
input:
Code:
%TESGO
dfkjsdfgogogocatcatdogtwelvetwentygogogo

output:
Code:
%TESGO
ntygogogodfkjsdfgo


for a triple occurrence (or even an occurrence of 4 or 5 etc..) it would be great if the %ID was modified to label what combination was being used:

input:
Code:
%TESGO
dfkjsdfgogogocatcatdogtwelvetwentygogogo
%TESGO
gogoCatDoggobye
%TESGO
byenowsoso

output:
Code:
%TESGO_A_C
yenowsosodfkjsdfgo
%TESGO_A_B
tDoggobyedfkjsdfgo

In all cases the last 9 substrings in each output line comes from the first occurrence of the first ID string.

Last edited by Scrutinizer; 03-14-2014 at 09:10 AM..
# 12  
Old 03-14-2014
This should work with single and double cases and if there are more than two, it takes the value of the last one, as mentioned in post #1:
Code:
awk '
  /^%/{
    getline s
    A[$1]=A[$1] s
  }                                           
  END{
    for (i in A) {
      print i
      print substr(A[i], length(A[i])-8) substr(A[i],1,9)
    }           
  }
' file


Last edited by Scrutinizer; 03-16-2014 at 06:04 AM..
# 13  
Old 03-22-2014
Assuming we have say 4 occurrences of the same ID and want to combine the end of ID 4 with the beginning of ID4, the beginning of ID 3, and the beginning of ID 2, but not with the beginning of ID1. How can this be done?

Basically, the combinations should look like this.

Code:
end of 4 with beginning of 4
            with beginning of 3
            with beginning of 2
end of 3 with beginning of 3
            with beginning of 2
end of 2 with beginning of 2
end of 1 with beginning of 1


input:
Code:
%dog
aaaaaaaaaaAAAAAAAAA
%dog
bbbbbbbbbbBBBBBBBBB
%dog
cccccccccccCCCCCCCCC
%dog
xxxxxxxxxxXXXXXXXXX

the output should look like this:
Code:
%dog
XXXXXXXXXxxxxxxxxx
%dog
XXXXXXXXXccccccccc
%dog
XXXXXXXXXbbbbbbbbb
%dog
CCCCCCCCCccccccccc
%dog
CCCCCCCCCbbbbbbbbb
%dog
BBBBBBBBBbbbbbbbbb
%dog
AAAAAAAAAaaaaaaaa

I am trying to do something like this but not sure how to print all of these combinations

Code:
awk '{
      if ( $1 ~ /^%/ )
      {
                A[$1]++
                i = $1
      }
      else
                {for (j = 2; j <=i; j++)
                A = newA
                F=1
                G[F]=$1
                        }
                        END {
                                for (k in A)
                                {
                                        print k
                                        print substr(R[A[k],k], length(R[A[k])-8) substr(R[1,k],1,9)
                        }
                }}' 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

Compare two files when pattern matched

I have two files say FILE1 and FILE2. FILE1 contains 80,000 filename in sorted order and another file FILE2 contains 6,000 filenames is also in sorted order. I want to compare the filename for each file and copy them in to a folder when filename is matched. File1.txt contain 80,000... (8 Replies)
Discussion started by: imranrasheedamu
8 Replies

2. UNIX for Advanced & Expert Users

To print from the first line until pattern is matched

Hi I want to print the line until pattern is matched. I am using below code: sed -n '1,/pattern / p' file It is working fine for me , but its not working for exact match. sed -n '1,/^LAC$/ p' file Input: LACC FEGHRA 0 LACC FACAF 0 LACC DARA 0 LACC TALAC 0 LAC ILACTC 0... (8 Replies)
Discussion started by: Abhisrajput
8 Replies

3. Shell Programming and Scripting

Matched a pattern from multiple columns

Hi, I need to extract an info in $1 based on a matched pattern in $2,$3,$4, and $5. The sample input file as follows:- ID Pat1 Pat2 Pro1 use1 add41 M M M add87 M M M M add32 ... (16 Replies)
Discussion started by: redse171
16 Replies

4. Shell Programming and Scripting

How to use sed to search a particular pattern in a file backward after a pattern is matched.?

Hi, I have two files file1.txt and file2.txt. Please see the attachments. In file2.txt (which actually is a diff output between two versions of file1.txt.), I extract the pattern corresponding to 1172c1172. Now ,In file1.txt I have to search for this pattern 1172c1172 and if found, I have to... (9 Replies)
Discussion started by: saurabh kumar
9 Replies

5. Shell Programming and Scripting

Insert certain field of matched pattern line above pattern

Hello every, I am stuck in a problem. I have file like this. I want to add the fifth field of the match pattern line above the lines starting with "# @D". The delimiter is "|" eg > # @D0.00016870300|0.05501020000|12876|12934|3||Qp||Pleistocene||"3 Qp Pleistocene"|Q # @P... (5 Replies)
Discussion started by: jyu3
5 Replies

6. Shell Programming and Scripting

Print only matched pattern in perl

Hi, I have script like below: #!/usr/local/bin/perl use strict; use warnings; while (<DATA>) { ( my ($s_id) = /^\d+\|(\d+?)\|/ ) ; if ( $s_id == 1 ){ s/^(.*\|)*.*ABC\.pi=(+|+)*.*ABC\.id=(\d+|+).*$/$1$2|$3/s; print "$1$2|$3\n"; (2 Replies)
Discussion started by: sol_nov
2 Replies

7. Shell Programming and Scripting

Grep word between matched pattern

would like to print word between matched patterns using sed for example : create INDEX SCOTT.OR_PK ON table_name(....) would like to print between SCOTT. and ON which is OR_PK Please help me out Thanks (4 Replies)
Discussion started by: jhonnyrip
4 Replies

8. Shell Programming and Scripting

print last matched pattern using perl

Hi, If there exist multiple pattern in a file, how can I find the last record matching the pattern through perl. The below script searches for the pattern everywhere in an input file. #! /usr/bin/perl -s -wnl BEGIN { $pattern or warn"Usage: $0 -pattern='RE' \n" and exit 255;... (5 Replies)
Discussion started by: er_ashu
5 Replies

9. Shell Programming and Scripting

Replacing a word after a matched pattern

Hello, Actually i want to replace the word after a matched pattern. For Ex: lets say that i am reading a file line by line while read line do echo $line # i need to search whether a pattern exists in the file and replace the word after if the pattern exist. # for example :... (1 Reply)
Discussion started by: maxmave
1 Replies

10. UNIX for Dummies Questions & Answers

Count of matched pattern occurance

In a file a pattern is occured many times randomly. Even it may appear more then once in the same line too. How i can get the number of times that pattern appeared in the file? let the file name is abc.txt and the pattern is "xyz". I used the following code: grep -ic "xyz" abc.txt but it is... (3 Replies)
Discussion started by: palash2k
3 Replies
Login or Register to Ask a Question