Bash add at the end of the line not working [closed] - linux

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 4 years ago.
Improve this question
I have a bash script that adds " at the end of some lines. In order to do this, I used sed 's/$/"/g', however, at most of the lines, it prints it at start of the line, replacing the first character. Example:
a
silly
example =>>>
"
"illy
example"

This behavior means you have DOS line ending in your file. You should convert your file to Linux file first.
In case you cannot do that, here is an alternate sed solution that takes into consideration presence of an optional \r (carriage return) before line break:
sed -E $'s/\r?$/"&/g' file

Related

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

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

Match lines that start with specifc letters and exclude certain words [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
I have a file that contains a series of lines as follows:
dbxxx
dbxxxx
dbxx
tdxx
tdxxx
sbxx
sbxxxxx
dbxx_migrated
tdxxx_old
Where x = one digit.
I need to create an output that would make sure to display lines that start with the following: db,td and sb and exclude any lines that has _migrated and _old.
Is this possible to do using grep?
You can combine regular expressions and logical operators with awk:
$ awk '/^(db)|(td)|(sb)/ && !/_(old)|(migrated)/' file
dbxxx
dbxxxx
dbxx
tdxx
tdxxx
sbxx
sbxxxxx
grep -E '^(db|td|sb)' file | grep -Ev '_(migrated|old)$'
or
perl -lne 'print if /^(db|td|sb)/ and not /_(migrated|old)$/' 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