How extract strings (perl)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How extract strings (perl)
# 1  
Old 10-01-2009
Data How extract strings (perl)

Sample data:
Code:
revision001 |  some text | some text
Comment: some comment
Brief: 1) brief
        2) brief
------------------------------------------
revision002 |  some text | some text
Brief: 1) brief
2) brief
FIX: some fix
------------------------------------------
revision003 |  some text | some text
Comment: some comment

Q: how extract "Brief" and "revision"?

need output:
Code:
revision001
    Brief: 1) brief
            2) brief
revision002
    Brief: 1) brief
    2) brief

I'm try:
<command> | perl -ne 'print if (/^Brief: /../(?:.*: )|(?:\-+)/ or /^revision[0-9]* /)'

Last edited by inotech; 10-01-2009 at 06:06 AM..
# 2  
Old 10-01-2009
Please post the (incorrect) output.
# 3  
Old 10-01-2009
bingo in shell

Code:
<some command> | egrep -v "^\-+$" |  while read LINE; do
    [[ ${LINE} =~ "^revision[0-9]+"  ]] && { L_BRIEF="1"; L_REF=${LINE%%|*}; }
    if [[ ${LINE} =~ "^Brief: " ]]; then
        echo -e "${L_REF}\n\t${LINE}"; L_BRIEF="0"
    elif [[ ! ${LINE} =~ "^.+: " ]]&&[ "x${L_BRIEF}" = "x0" ]; then
        echo -e "\t${LINE}"
    else
        L_BRIEF="1"
    fi
done

not perl ... but working
# 4  
Old 10-01-2009
Code:
$
$ cat f1
revision001 |  some text | some text
Comment: some comment
Brief: 1) brief
        2) brief
------------------------------------------
revision002 |  some text | some text
Brief: 1) brief
2) brief
FIX: some fix
------------------------------------------
revision003 |  some text | some text
Comment: some comment
$
$ ##
$ cat f1 |
> perl -lne 'BEGIN{undef $/}
>            while(/(revision.*\).*?\n).*?/msg){($x = $1)=~s/(Comment|\-+).*?\n| \|.+//g;
>            print $x}'
revision001
Brief: 1) brief
        2) brief
revision002
Brief: 1) brief
2) brief
$
$

tyler_durden
# 5  
Old 10-01-2009
Thnx

Thanks
# 6  
Old 10-12-2009
New trouble. As prev.

file data.dat:
Code:
N xxx xxx 353
 & xxx 99 012
 & xxxx                         99 000
09203809 290188309
9210830 0293801
B <some digits>
 & <some digits>
 & <some digits>
N yy yyyy 400
 & yyyy 01 000
 & xxxx                         00 000
9909 0000 9990
000 FAIL ....
000 FAIL ....

try:
Code:
perl -ne 'print if /N\s+.+?(?!\s+\&.+)/s' ./data.dat

result:
Code:
N xxx xxx 353
N yy yyyy 400

need:
Code:
N xxx xxx 353
 & xxx 99 012
 & xxxx                         99 000
N yy yyyy 400
 & yyyy 01 000
 & xxxx                         00 000

and

Code:
N xxx xxx 353 xxx 99 012 xxxx                         99 000
N yy yyyy 400 yyyy 01 000 xxxx                         00 000

# 7  
Old 10-12-2009
Code:
local $/="---";
open FH,"<a.txt";
while(<FH>){
 print $1,"\n",$2 if /.*(revision[0-9]+).*(Brief:.*?)(FIX|---)/s;
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract strings from output

I am having the following output when executing a dig command : dig @1.1.1.1 google.com +noall +answer +stats ; <<>> DiG 9.11.4-P1 <<>> @1.1.1.1 google.com +noall +answer +stats ; (1 server found) ;; global options: +cmd obodrm.prod.at.dmdsdp.com. 86154 IN A ... (1 Reply)
Discussion started by: liviusbr
1 Replies

2. UNIX for Beginners Questions & Answers

Extract content between strings

Hello i am stuck with this. i have input which is as follows /type/work /works/OL10627594W 3 2019-04-24T16:46:21.351549 {"created": {"type": "/type/datetime", "value": "2009-12-11T03:18:17.488715"}, "title": "Tog the dog", "covers": , "last_modified": {"type":... (3 Replies)
Discussion started by: ahfze
3 Replies

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

4. Shell Programming and Scripting

Extract text between two strings

Hi, I have a text like these: ECHO "BEGGINING THE SHELL....." MV FILE1 > FILE2 UNIQ_ID=${1} PARTITION_1=`${PL}/Q${CON}.KSH "SELECT ....." PARTITION_2=`${PL}/Q${CON}.KSH "SELECT ........" ${PL}/Q${CON}.KSH "CREATE ...." IF .... ....... I would like to extract only text that only... (4 Replies)
Discussion started by: mierdatuti
4 Replies

5. UNIX for Dummies Questions & Answers

Extract strings based on the value

I have a file with multiple columns (in this case, the file has 3 columns): NM_001006304 (-33.7) XM_418228 (-38.4) JN880447 (-33.7) CR387600 (-33.7) CR524203 (-36.3) GALGA_6AKII_KRT75 (-33.7) GALGA25_SC7 (-31.9) CR352795 (-36.3) NM_204172 (-31.7) NM_204137 (-31.9) NM_001030561 (-36.3) AB011672... (7 Replies)
Discussion started by: yuejian
7 Replies

6. UNIX for Dummies Questions & Answers

Extract code between 2 strings.

Hi, Im having some problems with this. I have loaded a file with html code. All code is placed in the same line. I want to get everything between two given strings (including these strings and get only the first appearance). Example: File contains <html><body><a href='a.html'>abc</a><a... (5 Replies)
Discussion started by: ngb
5 Replies

7. Shell Programming and Scripting

sed to extract all strings

Hi, I have a text file containing 2 lines as follows: I'm trying to extract all the strings following an "AME." The output would be as follows: BUSINESS_UNIT PROJECT_ID ACTIVITY_ID RES_USER1 RESOURCE_ID_FROM ANALYSIS_TYPE BI_DISTRIB_STATUS BUSINESS_UNIT PROJECT_ID ACTIVITY_ID... (5 Replies)
Discussion started by: simpletech369
5 Replies

8. Shell Programming and Scripting

Extract two strings from a file and create a new file with these strings

I have the following lines in a log file. It would be great if some one can help me to create a new file with the just entries in the below format. 66.150.161.195 HPSAC=Z05 66.150.161.196 HPSAC=A05 That is just extract the IP address and the string DPSAC=its value 66.150.161.195 -... (1 Reply)
Discussion started by: Tuxidow
1 Replies

9. Shell Programming and Scripting

Extract strings from file - Help

Hi, I have a file say with following lines (the lines could start from any column and there can be many many create statements in the file) create table table1....table definition... insert into table1 values..... create or replace view view1....view definition.... What i want is to... (2 Replies)
Discussion started by: whoami191
2 Replies

10. Shell Programming and Scripting

extract strings between tags

Hi, I have data as follows in a text file <key='data1'> <String>abcdef</String> <String>abcdef1</String> <String>abcdef2</String> </key> <key='data2'> <String>abcdef</String> <String>abcdef1</String> <String>abcdef2</String> <String>abcdef3</String> </key> Is there a way i... (10 Replies)
Discussion started by: userscript
10 Replies
Login or Register to Ask a Question