How to remove function from file on Linux? [closed] - linux

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

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 entry based on the value of first column [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 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.

inserting a certain text between in every occurrence of two following tabs [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 am trying to insert NA between every occurrence of two tab characters immediately following each other in a text file. How can I do it with a sed command?
This might work for you (GNU sed):
sed ':a;s/\t\t/\tNA\t/g;ta' file
This covers all occurrances of \t\t throughout a file
Or if you prefer:
sed 's/\t\t/\tNA\t/g;s//\tNA\t/g' file
Like this:
sed 's/xx/xNAx/g' file
where you type x using Control-V TAB
Or, if you have GNU sed, you can type:
sed 's/\t\t/\tNA\t/g' file

How to add a character in front of multiple words in linux [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Here is a text file containing many words,each is separated by space breaks or line breaks.
Now I want to add a character,like "#" "$" "#" in front of each of them,
and I found doing this job one by one will take too much time,
are there any better ways,in bash?
Try using sed
sed -r 's/([^ ]+)/#\1/g' file
Or more concisely,
sed -r 's/[^ ]+/#&/g' file
Sample input
abc def pqr-stu xyz
Output
#abc #def #pqr-stu #xyz
Using sed, you could say:
sed 's/\b\w/#&/g' inputfile
This would append # before every word.

Resources