How to delete from a text file, all lines that DO NOT contain a specific string? [duplicate] - sed

This question already has answers here:
sed delete lines not containing specific string
(4 answers)
Remove all lines except matching pattern line best practice (sed)
(4 answers)
Closed 17 days ago.
How would I use sed to delete all lines in a text file which do not contain a specific string?
sed '/pattern to match/d' ./infile
I used this command, but it deletes the lines MATCHING a specific pattern,
but what I want is to delete the lines which DO NOT contain a specific pttern?

Related

Replace multiple lines of whitespace with one line using sed [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
How could I use sed to replace multiple lines of whitespace (including tabs and spaces) with just one empty line?
Example input:
Line 1
<Space><Space>
Line 2
<Tab>
Line 3
Line 4
Desired output:
Line 1
Line 2
Line 3
Line 4
This might work for you (GNU sed):
sed 'N;s/^[[:blank:]]*$//mg;/^\n$/!P;D' file
Append the next line.
Remove any spaces or tabs from blank lines.
If the first line of the possible two is empty do not print.
Delete upto and including the first newline or if a newline is not present, delete the line.
N.B. The P and D commands are special.

Using \n in stringdict file [duplicate]

This question already has answers here:
NSString: newline escape in plist
(5 answers)
Closed 1 year ago.
I m using stringdict file for pluralization in iOS. I need to use the \n for the new line but If I use \n in stringdict file it's not showing the text in the new file. How can I use \n for the new line?
Verified Solution:
The stringsdict file allows you to jump to a new line using the key combination (option + enter)

append previous line with current line if the file contains blanks [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I want to append lines to the previous line if the blank line is followed by the line.
For example:
A
B
C
1
D
E
B
Ouput:
A
B
C
1D
EB
See also Håkon Hægland's comment above. This is one of those problems that lends itself to treating the whole file as one long string (with embedded newline characters). Perl has a flag -0 for changing or turning off the default "record separator" definition that accomplishes this. Then you just need to realize that, depending on your definition of "blank line", you seem to be requesting that all sequences of two or more newline characters in a row simply be removed. (If your definition of "blank line" can include lines with blank characters on them (whitespace, i.e. spaces and tabs), you'll need a more complicated expression.) This compact one-liner will do it:
$ perl -0pe 's/\n\n+//g' blanklines
A
B
C
1D
EB
Now please tell me this was not a homework assignment.
Update: I realized a couple of additional things. 1) Since newline is included in Perl's whitespace special escape \s, expanding to handle the case of blank lines having blank characters on them is not really more complex: perl -0pe 's/\n\s+//g' blanklines. 2) There is an edge case that this solution doesn't handle right: blank lines at the end of the input. I'll leave that as a problem for the student. :-)

Regex to prevent repeated characters or numbers [duplicate]

This question already has answers here:
Regular Expression To Match String Not Starting With Or Ending With Spaces
(2 answers)
Regex for password: repetitive characters
(4 answers)
Closed 4 years ago.
static let password = "^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,}$"
I have this regex to validate password
at least one character
at least one Capital character
at least one number
length more than 8
I need to update it to
prevent any repeated sequence of characters or numbers
to prevent "111111" or "aaaaaaa" for example
to prevent starting and ending with space character
how to update my regex to match those requirements ?

How to eregi_replace to preg_replace [duplicate]

This question already has answers here:
How can I convert ereg expressions to preg in PHP?
(4 answers)
Closed 8 years ago.
eregi_replace('[0-9]+\.+[0-9]','',$cart['unit']);
How to change it to preg_replace?
I get an error: Warning: preg_replace() [function.preg-replace]: Unknown modifier '+' in ---
You can use your existing Regex almost unchanged in a preg_replace(). Just add delimiters and a case-insensitive modifier. You get
preg_replace('#[0-9]+\.+[0-9]#i','',$cart['unit']);
In fact, the case-sensitivity is irrelevant since your pattern only matches 0-9 and .