I know that  have been asked on this forum but, as far as I can see, none of them addressed the problem of patterns being in different lines. Namely, given a text file  ( one ) ( two ) (  three  )  four   How may I delete everything that is between each '(' and ')' pair, even when the elements of the pair are on different lines? The desired result is  () () ()  four
122795
I have a requirement like i have to remove the strings or numbers between two Parenthesis in a file. I used sed command but it is working on a single line. My opening Parenthesis is in one line and the closing parenthesis is in other line. How can i do it?  I tried this command using sed:  sed -e 's/([^()]*)//g'   but this works only when My opening and closing parenthesis are on the same line.  For Example:-  Input file:  select a ,b ,c FROM ABCD (select e ,f ,g ,h FROM XYZ)   Output should be:  select a ,b ,c FROM ABCD
11666
A huge (up to 2 GiB) text file of mine contains about 100 exact duplicates of every line in it (useless in my case, as the file is a CSV-like data table).  What I need is to remove all the repetitions while (preferably, but this can be sacrificed for a significant performance boost) maintaining the original sequence order. In the result each line is to be unique. If there were 100 equal lines (usually the duplicates are spread across the file and won't be neighbours) there is to be only one of the kind left.  I have written a program in Scala (consider it Java if you don't know about Scala) to implement this. But maybe there are faster C-written native tools able to do this faster?  UPDATE: the awk '!seen[$0]++' filename solution seemed working just fine for me as long as the files were near 2 GiB or smaller but now as I am to clean-up a 8 GiB file it doesn't work any more. It seems taking infinity on a Mac with 4 GiB RAM and a 64-bit Windows 7 PC with 4 GiB RAM and 6 GiB swap just runs out of memory. And I don't feel enthusiastic about trying it on Linux with 4 GiB RAM given this experience.
77738
cat file_1  my colour is red my rose is red my colour is blue my rose id blue  cat file_2  red blue  cat output_file should be my colour is red my colour is blue   here i am using   cat file_2 | while read line;do cat file_1 | grep "$line" | head -1;done   here i am trying to get the top most line containing the pattern "red" and "blue" which is present in the file_2  is there any other way  to do , as fast as possible, while loop is taking time
19426
If I have a string "1 2 3 2 1" - or an array [1,2,3,2,1] - how can I select the unique values, i.e.  "1 2 3 2 1" produces "1 2 3"    or  [1,2,3,2,1] produces [1,2,3]   Similar to uniq but uniq seems to work on whole lines, not patterns within a line...
18132
Consider a text file with the following entries:  aaa bbb ccc ddd eee fff ggg hhh iii   Given a pattern (e.g. fff), I would like to grep the file above to get in the output:  all_lines except (pattern_matching_lines  U (B lines_before) U (A lines_after))   For example, if B = 2 and A = 1, the output with pattern = fff should be:  aaa bbb ccc hhh iii   How can I do this with grep or other command line tools?    Note, when I try:  grep -v 'fff'  -A1 -B2 file.txt   I don't get what I want. I instead get:  aaa bbb ccc ddd eee fff -- -- fff ggg hhh iii
16793
I have a multi line log entry format that I need to process.   The log looks something like this:   --START-- Device=B Data=asdfasdf Lorem=Ipsum --END-- --START-- Device=A Data=asdfasdf Lorem=Ipsum --END-- --START-- Device=B Data=asdfasdf --END-- --START-- Device=A Data=asdfasdf --END-- --START-- Device=B Data=asdfasdf --END-- --START-- Device=C Data=asdfasdf Lorem=Ipsum --END--   I want to print everything between --START-- and --END-- if a particular pattern is matched.   e.g:   Print all entries where Device=A  --START-- Device=A Data=asdfasdf Lorem=Ipsum --END-- --START-- Device=A Data=asdfasdf --END--   All I've been able to do so far is write:   sed -e -n '/--START--/,/--END--/p' &lt; input   Which effectively prints the input but I think I need to add {} to filter with N and then print if that condition matches.   I also think I'm completely lost.  Any idea on how to print multiple lines if a single line matches a condition?
123868
I am working with some text that is full of stuff between brackets [] that I don't want. Since I can delete the brackets myself, I don't need the one-liner to do that for me, but I do need a one-liner that will remove everything between them.  What about parentheses () instead of brackets?
38733
I want to know if there is an easy way to split a line in a file into multiple lines, such as this:  I have  A B C 1 2 3 4   And I want to get something like this, based on the first string of the line:  A B A C 1 2 1 3 1 4   Basically based on the first string in the line create multiple lines with the second and third and fourth string and so on.
25827
I needed to find all the files that contained a specific string pattern. The first solution that comes to mind is using find piped with xargs grep:  find . -iname '*.py' | xargs grep -e 'YOUR_PATTERN'   But if I need to find patterns that spans on more than one line, I'm stuck because vanilla grep can't find multiline patterns.
3235
I would like to get the multi pattern match with implicit AND between patterns, i.e. equivalent to running several greps in a sequence:  grep pattern1 | grep pattern2 | ...   So how to convert it to something like?  grep pattern1 &amp; pattern2 &amp; pattern3   I would like to use single grep because I am building arguments dynamically, so everything has to fit in one string. Using filter is system feature, not grep, so it is not an argument for it.    Don't confuse this question with:  grep "pattern1\|pattern2\|..."   This is an OR multi pattern match.
49149
Given a file, "test.log" with the following contents:  line1 Patient 123 45566 line2 Patient 432 line3 Patient 234 456 line4 Patient 321 line5   I am trying to select line 2 and line 4 with this pattern:  grep "Patient\s\d+\s" test.log # but this works testing at https://rubular.com/   Doesn't work nor does this:  grep "Patient\s\d+\n" test.log # but this works testing at https://regexr.com/47qd5   What am I doing wrong?
119949
I was trying to remove all the lines of a file except the last line but the following command did not work, although file.txt is not empty.  $cat file.txt |tail -1 &gt; file.txt  $cat file.txt   Why is it so?
65413
I'd like to know if there is a way that I could cat file like php.ini and remove all lines starting with ;  For example, if the file contained this:  ;   - Show all errors, except for notices ; ;error_reporting = E_ALL &amp; ~E_NOTICE ; ;   - Show only errors ; ;error_reporting = E_COMPILE_ERROR|E_ERROR|E_CORE_ERROR ; ;   - Show all errors except for notices ; error_reporting  =  E_ALL &amp; ~E_NOTICE   and I ran the correct command cat | {remove comments command}, then I would end up with:  error_reporting  =  E_ALL &amp; ~E_NOTICE   Note - I assumed that cat would be the best way to do this but I'm actually fine with the answer using another utility like awk, sed, egrep, etc.
48630
$ echo -e 'CH12\nCH23au' | sed '/^CH/s=^=&lt;b&gt;=' &lt;b&gt;CH12 &lt;b&gt;CH23au   I know I can match lines starting with CH by ^CH but how can I match multiple patterns?   Example:  Input:  CH12 CH23au   Output:  CH12 &lt;b&gt;CH23au   How to only put &lt;b&gt; where there is ^CH and au in the line?
56815
Is there a way to specify a regular expression to match every 2nd occurrence of a pattern in a string?  Examples   searching for a against string abcdabcd should find one occurrence at position 5 searching for ab  against string abcdabcd should find one occurrence at position 5 searching for dab  against string abcdabcd should find no occurrences searching for a against string aaaa should find two occurrences at positions 2 and 4
96362
Recently, I asked a  how to sort pairs of lines, and one of the answers suggested concatenating lines with sed, like this:  cat file.txt | sed -n 'N;s/\n//;p' | sort -t";" -k43,43n | perl -F';' -ane '$,=";";print @F[0..13],"\n";print @F[14..$#F]'   which worked brilliantly, but now my problem generalized to sorting n-tupels of lines, which I can't figure out how to do with sed.   Everything I found was either for 2 lines or ALL lines, but I need n lines (where n is 5 at the moment, but a general way would be great).  Bonus points for a nice way of rewriting the perl part to accomodate n lines, but the problem is really about the sed part.  I am also not hung on sed specifically, so if you have a nice solution using a different command line tool, please post it.  Update: Example input (n == 3)  a1;b1;c1;  n1;m1;l1;  d1;e1;f1;g1 n2;m2;l2; a2;b2;c2; d2;e2;f2;g2
87030
I am doing grep and sed and I get 2 lines of a file that I am interested in. How can I get these lines in a single line ending with the new line character? Now I am getting:    pattern1   pattern2     I would like to get pattern1 pattern2 \n