Remove entry based on the value of first column [closed] - linux

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 5 years ago.
Improve this question
If 3549,2152,4701 in first column then remove the entry:
sample data:
18106|1.0.4.0/22
3549|1.0.10.0/24
5413|1.0.0.0/16
2152|1.4.0.0/16
3549|1.0.8.0/22
4701|1.0.0.0/8
Expedted output:
18106|1.0.4.0/22
5413|1.0.0.0/16
How to achieve this?

For your pattern to match only on the first field you have to anchor the expression to the start of the line:
grep -v -E '^(3549|2152|4701)\|'
The ^ marks the beginning of the line (and $ would mark the end of the line)
The -E activates enhanced regular expressions so you don't have to \ escape pipes and parentheses, and the -v inverses the search (returning only lines that do not match).
The ^ matches the start of the line then parentheses with the pipe symbol marks alternatives (3549, 2152 or 4701), and \| stands for the pipe symbol itself which your first field ends with, and needs to be escaped by the backslash so it's not treated as another alternation.
Be careful to use single quotes around it because otherwise the shell itself will interpret some of the special characters.

Related

How to remove set of matching characters at the end of the string in a shell scripts [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 6 months ago.
Improve this question
I want to remove set of matching characters at the end of the string in a shell script. It should work in all the linux flavours, ideally with out using tools like sed,awk.
I found some examples on web but all of them are about removing a single character type.
Below is a set of examples which shows what I am trying to achieve.
Please help.
1. Input : test_-
Output: test
2. Input: test-_-
Output: test
3. Input: test1__-
Output: test1
I want to remove the all the "hyphen" and "underscore" characters from the end of the string.
Since you are tagging this zsh:
Assuming that your string is stored in a variable input, you can do a
if [[ $input =~ ^((.*[^-_])) ]]
then
output=$MATCH
fi
The .* does a greedy match, which guarantees that the last character is neither a dash nor a hyphen.
In bash, this works similar, only that you have to set
output=${BASH_REMATCH[1]}
Supposing your data is in a file, like
test_-
test-_-
test1__-
with grep
grep -oP '[a-z]*[0-9]*' data.txt

How to find all lines which contain at least one of a set of words as a prefix [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 1 year ago.
Improve this question
I have a text file of words, one per line, called A.
I have another text file B.
How can I find all lines in B what have at least one of the words from A as a prefix?
I was hoping to be able to do this from the command line maybe using grep but any other command line solution would be great too.
For example, if A is
apple
bob
cheese
and B is
aple
bob123
ches
I would like the line bob123 to be returned.
One approach uses bash's process substitution and sed to add a regular expression beginning-of-line ^ anchor to each line of A, and then tells grep to use it as a list of regular expressions to search for:
$ grep -f <(sed 's/^/^/' a.txt) b.txt
bob123

Remove Unorder Number From Sublime [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 2 years ago.
Improve this question
i Have Data Like this
9372127603
9372130412
9372140
9372175041
937218
9372190908
9372191764
i need output like
9372127603
9372130412
9372175041
9372190908
9372191764
what i do to achieve this in sublime text ?
You can do this via regex Find and Replace. Open Find → Replace…, make sure the regex option is on, enter ^\d{1,9}\n into the Find: field, and make sure the Replace: field is empty:
Explanation:
^ beginning of line
\d any digit
{1,9} match preceding between 1 and 9 times, inclusive
\n newline character
test at Regex101
Once you've done that, hit Replace All and any numbers less than 10 digits long will be removed:

How to remove function from file on Linux? [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 2 years ago.
Improve this question
We have in our project several files that contains implementation of functions, what I need is to find in one of the files:
The specific location where a function is declared
Remove the function - meaning- the line where it start and the following 4 lines.
How can it be done using bash commands?
You can do this using sed. The exact command would depend on the function (and language) that you are trying to remove. In general, you can use sed -i '/STARTING_PATTERN/,/ENDING_PATTERN/d' file.ext to delete from STARTING_PATTERN to ENDING_PATTERN. Say you have a C function (obviously this is a dummy example):
double toDouble(int input) {
double output = (double)input;
return output;
}
You could use sed -i '/^double toDouble(int input) {$/,/^}$/d' file.c. This will delete from a line that has only the signature of the function (double toDouble(int input) {) to the next line that has only a } on it. Obviously you will need to tailor your starting and ending patterns to whatever the function you are trying to match looks like. The ^ and $ match the beginning and end of a line.
Edit: fixing typo

Vim, search in document header [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 8 years ago.
Improve this question
I need a macro/function that searches the current document, starting at the top, for a specific pattern. Stopping at the first line that isn't commented out.
For example in this vim script:
"This is the first line
"This is the third line
if exists("did_something")
It would stop at if line, return true if it found the pattern and false otherwise.
All help appreciated!
Let's say your pattern is the letter 'e'.
To search for a line that is not commented out, but contains an e:
/^\%(\s*"\)\#!.*e
This uses a negative lookahead (\#!) to confirm that the start of the line (^) isn't followed by a the beginning of a comment (whitespace and then a double quote \s*") but is followed by an 'e' (.*e). The \%( and \) makes the enclosed pattern an atom that other operators (like negative lookahead) can operate on as a unit.
To run a command on matching lines, use :g
:g/^\%(\s*"\)\#!.*e/echo "found one"
To see if the current line matches, use match()
:echo match( getline(line('.')), '^\%(\s*"\)\#!.*e' ) >= 0 ? 'true' : 'false'
The regex is pretty much always the same, the question is what do you want to do with matching lines?

Resources