perl search string for cut data


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl search string for cut data
# 1  
Old 04-29-2011
perl search string for cut data

Code:
perl -lne '$/="1H1XXXXX";print $_ if /0001|0002|0003/' data.txt> output.txt

Code:
 more data.txt
1H1XXXXX|0001|Y|
aaa
bbb
ccc
1H1XXXXX|0005|N|
bbb
g
1H1XXXXX|0001|Y|
hhh
ddd
222
1H1XXXXX|0002|Y|
444
1H1XXXXX|0002|N|
222
1H1XXXXX|0003|Y|
hhhh

Code:
more output.txt
1H1XXXXX|0001|Y|
|0001|Y|
hhh
ddd
222

|0002|Y|
444

|0002|N|
222

|0003|Y|
hhhh

please help edit script for out put
Code:
1H1XXXXX|0001|Y|
aaa
bbb
ccc
1H1XXXXX|0001|Y|
hhh
ddd
222
1H1XXXXX|0002|Y|
444
1H1XXXXX|0002|N|
222
1H1XXXXX|0003|Y|
hhhh

# 2  
Old 04-29-2011
try this,
Code:
perl -nle 'if(/1H1XXXXX\|(0001|0002|0003)\|(Y|N)/){print;$flg=1;next}if($flg && $_!~ /^1H1XXXXX/){print} else {$flg=0}' data.txt

This User Gave Thanks to pravin27 For This Post:
# 3  
Old 04-29-2011
Thank you pravin27 for Script Smilie
# 4  
Old 05-10-2011
Script
Code:
more  list_input.txt
acc_input1
acc_input2

more acc_input1
0001
0003

more acc_input2
0002


while read K
        do
    ACCT2=`more ${K}|perl -i -pe "s/\n$//g"|awk '{print substr($0,1,length($0)-1)}'`
       echo ${ACCT2} 
       echo "perl -nle 'if(/1H1XXXXX\|$ACCT2\|/){print;\$flg=1;next}if(\$flg && \$_!~ /^1H1XXXXX/){print} else {\$flg=0}' data.txt > out_${K}.txt"
      perl -nle 'if(/1H1XXXXX\|$ACCT2\|/){print;\$flg=1;next}if(\$flg && \$_!~ /^1H1XXXXX/){print} else {\$flg=0}' data.txt > out_${K}.txt   ###<<< Error in line       
  done <  list_input.txt

LogError
Code:
0001|0003
perl -nle 'if(/1H1XXXXX\|0001|0003\|/){print;$flg=1;next}if($flg && $_!~ /^1H1XXXXX/){print} else {$flg=0}' data.txt > out_acc_input1.txtxt
Can't modify single ref constructor in scalar assignment at -e line 1, near "1;"
Execution of -e aborted due to compilation errors.
Can't modify single ref constructor in scalar assignment at -e line 1, near "1;"
Execution of -e aborted due to compilation errors.

Why not use Script in Shell ?

Last edited by kittiwas; 05-10-2011 at 05:33 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cut the data with a string that has \ in it

Hi Team, Here's the record in a file. abc\USER DEFINED\123\345\adf\aq1 Delimiter here is "\USER DEFINED\" Expected output: abc|123\345\adf\aq1 Find "\USER DEFINED\" and replace it with "|". Can anyone please help us to fix this issue? Please use CODE tags as required by... (5 Replies)
Discussion started by: kmanivan82
5 Replies

2. UNIX for Dummies Questions & Answers

Loop with Perl (string search)

I am using a perl script to reverse and complement sequences if a string is found. The script works as expected as standalone but I would like to use it in my bash file. However, I am not getting my expected result. My test.txt file >Sample_72... (8 Replies)
Discussion started by: Xterra
8 Replies

3. UNIX for Dummies Questions & Answers

Search different string using perl

Hello, I want to search two strings in a file and print the same in the new file using perl script. Can anyone suggest me how to do this... The file looks like below: <UML:ModelElement.requirement> <UML:Dependency name="Row_MainColumn_FW_0009"> <UML:ModelElement.taggedValue>... (3 Replies)
Discussion started by: suvendu4urs
3 Replies

4. Shell Programming and Scripting

Cut Data In Bigfile with Perl

I want to create new file for START-END but i know NO 0003/02 only one in file Ex. Data FILE Data.txt (Data ~1,000,000 Line) I use Script perl perl -lne '$/="END";print $_."END" if /0003\/02/' fileOut put script perl but I want create Out put All No in START-END have NO 0003/02 Please... (8 Replies)
Discussion started by: kittiwas
8 Replies

5. Shell Programming and Scripting

Extracting data between tags based on search string from unix file

Input file is on Linux box and the input file has data in just one line with 1699741696 characters. Sample Input: <xxx><document coll="uspatfull" version="0"><CMSdoc>xxxantivirus</CMSdoc><tag1>1</tag1></document><document coll="uspatfull"... (5 Replies)
Discussion started by: gaya
5 Replies

6. Shell Programming and Scripting

search of string from an array in Perl

Hi All I want to search a string from an array in Perl. If a match occurs, assign that string to a variable else assign 'No match'. I tried writing the script as follows but it's in vain. Please help me.. #!/usr/bin/perl use strict; my $NER; my @text=("ORG","PER"); ... (4 Replies)
Discussion started by: my_Perl
4 Replies

7. Shell Programming and Scripting

Perl search in a string for....

ok so what I am trying to do is search through 200k files that have ext .000 or .702. for *@yahoo.com.tw and if it finds that in the file. then remove the file. this is my code... what am i doing wrong. it seams it will only find asdflkajsdf@yahoo.com.tw as a string and not *@yahoo.com.tw so it... (5 Replies)
Discussion started by: Philux
5 Replies

8. Shell Programming and Scripting

search for a string -perl

Hi, I have a line where i need to get certain part of it.. example.. text txt tt: 1909 thats how exactly it looks and all spaces are to be counted.. i need to retrieve 1909.. Thanks (11 Replies)
Discussion started by: meghana
11 Replies

9. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies

10. Shell Programming and Scripting

Perl: Search for string then parse next line

Hi All, I have a file that I need to be able to find a pattern match on one line then parse data on the next or subsequent lines - I will know which line needs to be parsed beforehand. This is what I currently have: while (<COMMAND_OUT>) { if ($_ =~ m/TEST/) { ... (4 Replies)
Discussion started by: pondlife
4 Replies
Login or Register to Ask a Question