extract only the first occurrence of the strings

I searched a lot but I didnt find answers which would suit what I am looking for so I am posting this question.

I want to extract the first occurance of 'par','comp' 'order' and 'nameprefix'. How to do this for the example input? Is there any perl oneliners to do it?

    wertydhhdbc->auCdl = '( nil
    par    (xyz)
    comp     asd1
    order        (done)
    namePrefix        "X"
    par    (xyz)
    comp     asd
    order        (done)
    namePrefix        "R"
    namePrefix        "X"
    par    (qwer)
    comp     key
    order        (done)
    namePrefix        "X"
    comp     key
    order        (done )
    par    (qwer)
    order        (done)

Answers


Yes, you can use a simple Perl onle-liner to show the first line containing e.g. par:

perl -ne 'if (/par/) { print; last; }'  1.txt

The -n switch makes perl go over the input file line by one. The code then makes it print the line and stop once the given pattern is found.

Update:

To search for the first occurence of each of the words, a different technique must be used:

perl -ne 'if(/(comp|par|order)/) {$f{$1}++; print if 1 == $f{$1} ; last if 3 == keys %f}' 1.txt

3 is the number of words you are searching for.


Need Your Help

How can I convert an RGB image to grayscale but keep one color?

matlab image-processing colors rgb grayscale

So I am trying to create an effect similar to Sin City or other movies where they remove all colors except one from an image.

Selectively force file download with Apache based on URL parameter

apache .htaccess download url-parameters

I have an Apache 2.2 running and I am forcing some files to be downloaded using this .htaccess snippet:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.