Extraction of strings from a file, after pattern matching


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Extraction of strings from a file, after pattern matching
# 1  
Old 10-21-2011
Extraction of strings from a file, after pattern matching

I need to extract strings from a file.

The file contains data like:

Plan ABCD
IN-+-172BB---118C2C---GGN_342-+-MM77_23--+-LAS24_3|GGK_774
| | \-LAS24_2|GGN_774
| +-AA_800_1-+-BAS_000|GGK_362
| | \-BAS_001|GGK_360
| \-DD_000T1---DAM_001|STEEL_0


Plan SHELL_1
IN-+-CRCBB---118C2D---FRB_342-+-SS77_23--+-LAS20_1|GGK_734
| +-AB_800_1-+-BAS_001|GGK_332
| | \-BAS_003|GGK_700


Where it shows a sort of chart, where I need a list of all the unique strings:
1. All the strings starting with Plan (e.g. ABCD, SHELL_1)
2. All the strings within - and - (e.g. 118C2C, AB_800_1, FRB_342, 172BB etc.).
3. All the strings with single - and containing | in between separately (e.g. LAS20_1|GGk_734, BAS_001|GGK_332, BAS_003|GGK_700, LAS24_3|GGK_774 etc.).

Its a long file, can someone help me how to extract these strings.

I tried 2 approaches, first to read the file line by line, and using awk to print the arguments till NF is reached , but I got error that can not open file.
Second, I converted the file in a simple delimited file, through sed, but still unable to extract these strings out.

Last edited by abkush; 10-21-2011 at 08:27 AM..
# 2  
Old 10-21-2011
Could you give some example output for your example input?
This User Gave Thanks to CarloM For This Post:
# 3  
Old 10-21-2011
Output for the given portion of the file i.e.

Input File:

Plan ABCD
IN-+-172BB---118C2C---GGN_342-+-MM77_23--+-LAS24_3|GGK_774
| | \-LAS24_2|GGN_774
| +-AA_800_1-+-BAS_000|GGK_362
| | \-BAS_001|GGK_360
| \-DD_000T1---DAM_001|STEEL_0


Plan SHELL_1
IN-+-CRCBB---118C2D---FRB_342-+-SS77_23--+-LAS20_1|GGK_734
| +-AB_800_1-+-BAS_001|GGK_332
| | \-BAS_003|GGK_700




Output will consists of 3 files, like below

File_1 with below data

Plan ABCD,
Plan SHELL_1

File_2 with below data

172BB
118C2C
GGN_342
MM77_23
AA_800_1
DD_000T1
CRCBB
118C2D
FRB_342
SS77_23
AB_800_1

File_3 with below data

LAS24_3|GGK_774
LAS24_2|GGN_774
BAS_000|GGK_362
BAS_001|GGK_360
DAM_001|STEEL_0
LAS20_1|GGK_734
BAS_001|GGK_332
BAS_003|GGK_700
# 4  
Old 10-21-2011
You could do something like:

Code:
awk '/^Plan/ {print $2}' infile > File1
 
awk -F'-' '$0 !~ /^PLAN/ {for (i=1;i<=NF;i++) {if (($i != "") && ($i != "+") && (match($i,"[|]") == 0)) { print $i }}}' infile > File2
 
awk -F'-' '$0 !~ /^PLAN/ {for (i=1;i<=NF;i++) {if (($i != "") && ($i != "+") && (match($i,"[|]") > 0)) { print $i }}}' infile > File3

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

(g)awk: Matching strings from one file in another file between two strings

Hello all, I can get close to what I am looking for but cannot seem to hit it exactly and was wondering if I could get your help. I have the following sample from textfile with many thousands of lines: File 1 PS001,001 HLK PS002,004 L<G PS004,002 XNN PS004,006 BVX PS004,006 ZBX=... (7 Replies)
Discussion started by: jvoot
7 Replies

2. Shell Programming and Scripting

Help with tag value extraction from xml file based on a matching condition

Hi , I have a situation where I need to search an xml file for the presence of a tag <FollowOnFrom> and also , presence of partial part of the following tag <ContractRequest _LoadId and if these 2 exist ,then extract the value from the following tag <_LocalId> which is "CW2094139". There... (2 Replies)
Discussion started by: paul1234
2 Replies

3. Shell Programming and Scripting

Big pattern file matching within another pattern file in awk or shell

Hi I need to do a patten match between files . I am new to shell scripting and have come up with this so far. It take 50 seconds to process files of 2mb size . I need to tune this code as file size will be around 50mb and need to save time. Main issue is that I need to search the pattern from... (2 Replies)
Discussion started by: nitin_daharwal
2 Replies

4. Shell Programming and Scripting

Need to append matching strings in a file

Hi , I am writing a shell script to check pvsizes in linux box. # for i in `cat vgs1` > do > echo "########### $i ###########" > pvs|grep -i $i|awk '{print $2,$1,$5}'>pvs_$i > pvs|grep -i $i|awk '{print $1}'|while read a > do > fdisk -l $a|head -2|tail -1|awk '{print $2,$3}'>pvs_$i1 >... (3 Replies)
Discussion started by: nanduri
3 Replies

5. Shell Programming and Scripting

Get matching string pattern from a file

Hi, file -> temp.txt cat temp.txt /home/pradeep/123/a_asp.html /home/pradeep/123/a_asp1.html /home/pradeep/435/a_asp2.html /home/pradeep/arun/abc/a_dfr.html /home/pradeep/arun/123/a_kir.html /home/pradeep/123/arun/a_dir.html .... .... .. i need to get a_*.html(bolded strings... (4 Replies)
Discussion started by: pradebban
4 Replies

6. Shell Programming and Scripting

Help with matching pattern inside a file

I have a huge file that has roughly 30304 lines. I need to extract specific info from that file. For example, Box 1 > *aaaaaaaajjjj* > hbbvjvj > jdnnfddllll > *dgdfhfekwjh* Box 2 > *aaaaaaa'aj'jjj* > dse hkjuejef bfdw > dyeee > dsewq > *dgdfhfekwjh* >feweiuei Box 3 > *aaaa"aaaaj"jjj* >... (25 Replies)
Discussion started by: Ernst
25 Replies

7. Shell Programming and Scripting

Extracting the strings matching a pattern from a word

Hi All , I need to extract the strings that are matching with the pattern : CUST.<AnyStringOfAnyLength>.<AnyStringOfAnyLength> from a file and then write all these string into another file. e.g. If a file SOURCE contains following lines : IF(CUST.ABCD.EFGH==1) THEN CUST.ABCD.EFGH =... (7 Replies)
Discussion started by: swapnil.nawale
7 Replies

8. Programming

File Pattern Matching C++

Hi, I have large files with fixed length fields or fields seperated by delimeter. I would like to do validation on some or all fields to check for numeric or date or characters etc.. I would like to write this in C++. Please let me know if any one have any ideas on this. Thanks for all... (2 Replies)
Discussion started by: rameshmelam
2 Replies

9. UNIX for Advanced & Expert Users

extraction of data from a text file which follows certain pattern

hi everybody, i have a file, in it I need to extract some data that follows a particular pattern.. For example: my file contains like now running Speak225 sep 22 mon 16:34:05 2008 -------------------------------- ... (4 Replies)
Discussion started by: mohkris
4 Replies

10. Shell Programming and Scripting

Pattern matching for file

Hi All, I'm new to perl, My requirement is to check if particular file exists. e.g. filename.txt, filename1.txt, filename2.txt etc I tried the below code:- my $var1 = "filename.txt" if ( -e ($var1 = ~ /file\w/)) { print "File found \n"; } else { print "File not found \n"; } ... (0 Replies)
Discussion started by: doitnow
0 Replies
Login or Register to Ask a Question