Help with pattern match and Extract


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Help with pattern match and Extract
# 1  
Old 11-14-2018
Help with pattern match and Extract

Hi All,

I am having a file like below . Basically when SB comes in the text with B. I have to take the word till SB. When there only B I should take take till B. Tried for cut it by demilter but not able to build the logic

Code:
SB- CD B_RESTO SB_RESTO CRYSTALS BOILERS -->There SB and B so I should have output as SB- CD B_RESTO 
CONC- BC SB_RESTO CRYSTALS BOILERS--> There is only SB so I should have CONC- BC 
SCT- CQ SB_AO SUBBRANDS SCT_PINK LILAC
GSZ- IW SB_DOWNY NATURE BLENDS GSZ_LESS THAN 30 USES
PRC- CD SB_RESTO CRYSTALS BOILERS
SCT- DK B_METHOD SCT_LAVENDER PLUS CYPRESS --> There is only B so I should have SCT- DK
SCT- DK SB_METHOD AO SB SCT_LAVENDER PLUS CYPRESS--> I have SB and SB so I should have SCT- DK SB_METHOD AO



Expected output
Code:
SB- CD B_RESTO 
CONC- BC 
SCT- CQ 
GSZ- IW 
PRC- CD 
SCT- DK 
SCT- DK SB_METHOD AO

# 2  
Old 11-14-2018
What shell and operating system are you using?

What have you tried to solve this problem on your own? Where are you stuck?

Why isn't the output from the second line in your example:
Code:
CONC- BC SB_RESTO CRYSTALS BOILERS--> There is only

There are two occurrences of SB in that line and three additional occurrences of B as well (one before the first SB, one between the two occurrences of SB, and one after the last SB).
# 3  
Old 11-14-2018
I am using Redhat Linux. In my example I have SB alone so it should be CONC- BC. Things I have tried

cut -d' ' -f1,2,3 test.desc - where test.desc is my file. This will bring me from 2nd segment but I have to get by SB or B
# 4  
Old 11-15-2018
Posting 8 copies of the same partial (and confusing) answer doesn't help. Smilie

I asked what shell and operating system you're using. You have told us what operating system you're using; but not what shell you're using!

The second line of your sample input was:
Code:
CONC- BC SB_RESTO CRYSTALS BOILERS--> There is only SB so I should have CONC- BC

with SB clearly marked in red two times and B (not part of SB) clearly marked in magenta three other times. I repeat. Please explain why, according to the rules you specified, the output produced from that line of input should not be:
Code:
CONC- BC SB_RESTO CRYSTALS BOILERS--> There is only

Furthermore, the output you say should be produced for the third, fourth, and fifth input lines:
Code:
SCT- CQ SB_AO SUBBRANDS SCT_PINK LILAC
GSZ- IW SB_DOWNY NATURE BLENDS GSZ_LESS THAN 30 USES
PRC- CD SB_RESTO CRYSTALS BOILERS

which you said should "take till B" since SB doesn't appear twice says to me that the output should be:
Code:
SCT- CQ S
GSZ- IW S
PRC- CD S

since in all three of these cases there is an S in the input before the B that you have not shown in your expected output???

I find it very difficult to help you reach your goal if the output you say you want does not match the description of the output you say you want.

Please clarify your requirements!
These 2 Users Gave Thanks to Don Cragun For This Post:
# 5  
Old 11-15-2018
I must add that this user never posts his attempts.
He/she just requires someone else to code instead of him.

The search option confirms this.
This is just rude.

Dear arunkumar_mca

Various fairly complex awk solutions provided in past should be start to learn parsing text in unix environments.
Give it shot, try to write something, then come here to improve / rewrite your code using advice from folks here.

Regards
Peasant.
This User Gave Thanks to Peasant For This Post:
# 6  
Old 11-15-2018
I should have said that "_SB" or "_B".
I know it is against the form to post personal comment. @peasant I was trying to do in parallel to see how I can get this in from SQL. I was not being rude here and never . Don't use such comments. The multiple time post is something wrong with either with the form submit or with my enter I tried my max to delete it didnt allowed you can try my edit history I tried to remove all text and try saving it that also didn't work.You guys helped me a lot and not that my task ended there I took it and put in other code and I was working on it. Not something I want you to do my stuff
# 7  
Old 11-15-2018
Quote:
Originally Posted by arunkumar_mca
I should have said that "_SB" or "_B".

... ... ...
Is this supposed to help us figure out what you're trying to do?

Why should you have said anything about "_SB" or about "_B"? Neither of those strings appears anywhere in any line of your sample input!

If you're unwilling to answer our questions and are unwilling to give us a clear specification of what you are trying to do, there is very little that we can do to help you. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

2. Shell Programming and Scripting

Rearrange or replace only the second line after pattern match or pattern match

Im using the command below , but thats not the output that i want. it only prints the odd and even numbers. awk '{if(NR%2){print $0 > "1"}else{print $0 > "2"}}' Im hoping for something like this file1: Text hi this is just a test text1 text2 text3 text4 text5 text6 Text hi... (2 Replies)
Discussion started by: invinzin21
2 Replies

3. Shell Programming and Scripting

Extract lines that match a pattern

Hi all, I got a file that contains the following content, Actually it is a part of the file content, Installing XYZ XYZA Image, API 18, revision 2 Unzipping XYZ XYZA Image, API 18, revision 2 (1%) Unzipping XYZ XYZA Image, API 18, revision 2 (96%) Unzipping XYZ XYZA Image, API 18,... (7 Replies)
Discussion started by: Kashyap
7 Replies

4. Shell Programming and Scripting

sed : match one pattern then the next consecutive second pattern not working

Ive used this snippet of code on a solaris box thousands of times. But it isnt working on the new linux box sed -n '/interface LoopBack0/{N;/ ip address /p;}' *.conf its driving me nuts !! Is there something Im missing ? (7 Replies)
Discussion started by: popeye
7 Replies

5. UNIX for Dummies Questions & Answers

Match Pattern after certain pattern and Print words next to Pattern

Hi experts , im new to Unix,AWK ,and im just not able to get this right. I need to match for some patterns if it matches I need to print the next few words to it.. I have only three such conditions to match… But I need to print only those words that comes after satisfying the first condition..... (2 Replies)
Discussion started by: 100bees
2 Replies

6. Shell Programming and Scripting

Pattern Match & Extract from a string

Hi, I have long string in 2nd field, as shown below: REF1 | CLESCLJSCSHSCSMSCSNSCSRSCUDSCUFSCU7SCV1SCWPSCXGPDBACAPA0DHDPDMESED6 REF2 | SBR4PCBFPCDRSCSCG3SCHEBSCKNSCKPSCLLSCMCZXTNPCVFPCV6P4KL0DMDSDSASEWG I have a group of fixed patterns which can occur in these long strings & only... (11 Replies)
Discussion started by: karumudi7
11 Replies

7. Shell Programming and Scripting

Search for a pattern,extract value(s) from next line, extract lines having those extracted value(s)

I have hundreds of files to process. In each file I need to look for a pattern then extract value(s) from next line and then search for value(s) selected from point (2) in the same file at a specific position. HEADER ELECTRON TRANSPORT 18-MAR-98 1A7V TITLE CYTOCHROME... (7 Replies)
Discussion started by: AshwaniSharma09
7 Replies

8. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

9. Shell Programming and Scripting

Extract data from records that match pattern

Hi Guys, I have a file as follows: a b c 1 2 3 4 pp gg gh hh 1 2 fm 3 4 g h i j k l m 1 2 3 4 d e f g h j i k l 1 2 3 f 3 4 r t y u i o p d p re 1 2 3 f 4 t y w e q w r a s p a 1 2 3 4 I am trying to extract all the 2's from each row. 2 is just an example... (6 Replies)
Discussion started by: npatwardhan
6 Replies

10. Shell Programming and Scripting

Match first pattern first then extract second pattern match

My input file: <accession>Q91G55</accession> <name>043L_IIV6</name> <protein> <recommendedName> <location> <position position="294"/> </location> <fullName>Uncharacterized protein 043L</fullName> <accession>P18556</accession> <name>1106L_ASFB7</name> <protein> <recommendedName>... (5 Replies)
Discussion started by: patrick87
5 Replies
Login or Register to Ask a Question