Is there a command in Linux/Unix to comment out multiple lines at once? - linux

I am looking for command or a shortcut to comment out multiple lines at once in Linux/Unix. For example, in a file we have 200 lines and I want to comment first 100 lines only. I know we can use # before every line to make it as comment. But is there a way to do all 100 lines at once and save time?

Sure, using sed:
sed -i '1,100s/^/# /' file
-i makes sed modify file in-place; 1,100 is the range of lines 1–100 in the file; “s” is for “substitute”; ^ is the beginning of the line; # is its replacement.

Related

use sed to delete patterns on a range of lines

I have a large text file that I would like to divide into segments and use sed to delete certain patterns in place. I would like to do this in a single command line using a pipe. For example:
sed -n 1,10p <text file> | sed -i 's/<pattern to remove>//'
The code above attempts to take the first 10 lines of the text file and remove the patterns from the 10 lines in place. The resulting text file should have the first 10 lines modified. The code above doesn't work because the second command after the pipe requires a input file. Please help!
Something like
sed -i '1,10s/pattern//' foo.txt
though for in place editing of a file I prefer ed or perl instead of relying on a non standard sed extension like -i.
This seems to do what you're asking ....
sed -ni '1,10s/pattern//p' file

How to use sed to comment and add lines in a config-file

I am looking for a way to achieve the following:
A certain directory contains 4 (config) files:
File1
File2
File3
File4
I want my bash script to read in each of the files, one by one. In each file, look for a certain line starting with "params: ". I want to comment out this line and then in the next line put "params: changed according to my will".
I know there are a lot of handy tools such as sed to aid with these kind of tasks. So I gave it a try:
sed -ri 's/params:/^\\\\*' File1.conf
sed -ri '/params:/params: changed according to my will' File1.conf
Questions: Does the first line really substitute the regex params: with \\ following a copy of the entire line in which params: was found? I am not sure I can use the * here.
Well, and how would I achieve that these commands are executed for all of the 4 files?
So this command will comment every line beggining by params: in you files, and append a text in the next line
sed -E -i 's/^(params:.*)$/\/\/\1\nYOUR NEW LINE HERE/g'
the pattern ^(params:.*)$ will match any whole line beggining by params:, and the parenthesis indicate that this is a capturing group.
Then, it is used in the second part of the sed command via \1, which is the reference of the first capturing group found. So you can see the second part comments the first line, add a line break and finally your text.
You can execute this for all your files simply by going sed -E -i 's/^(params:.*)$/\/\/\1\nYOUR NEW LINE HERE/g' file1 file2 file3 file4
Hope this helps!
You can do this:
for i in **conf
do
cp $i $i.bak
sed -i 's/\(params:\)\(.*\)$/#\1\2\n\1new value/'
done
With: \(params:\)\(.*\)
match params: and store it in `\1
match text following .*\: and store it in \2
Then create two lines:
The initial line commented: #\1\2\n
The new line with your wanted value: \1new value
This might work for you (GNU sed and parallel):
parallel --dry-run -q sed -i 's/^params:/#&/;T;aparams: bla bla' {} ::: file[1-4]
Run this in the desired directory and if the commands are correct remove the --dry-run option and run for real.

Delete Line if it contains letters - Linux

I have this command sed -i 's/[A-Za-z]//g' file.txt that gets rid of any letters in my file but now have come to the realization that I need to be a little more steep with these errors.
How can I alter this command to Delete the line completely if there is letters in it?
000000asd000,12
000000000000,123
Would go to this
000000000000,123
sed -i '/[a-zA-Z]/d' file.txt
The /.../ command to match lines containing a letter, then d to delete the line.

How to delete lines in a ".data" file?

I have an enormous file, about 10 MB, and it has about 175,000 lines. I tried truncated it like this:
sed '500,175000d' <file-name.data>
I reopen the file, and all of the lines are still there! I tested this with other files and it works. For some reason the .data extension doesn't work? How do I delete these lines?
You need to either redirect the output to a new file like
sed '500,175000d' file-name.data >newFile
or use the edit in place option which rewrites the input file
sed -i '500,175000d' file-name.data
as pointed out by Wintermute
Edit:
A faster sed would be just
sed -i '500q' file-name.data # prints 1-500 and quits after line 500

How do I delete lines of a given interval in a file containing a matching word?

I need to use sed to delete from a file only the lines containing a given word. But if my file has 50 lines, I only need to look at the first 30 lines for example.
I know that:
sed '1,30d' f.txt
will delete the first 30 lines of the file.
And:
sed '/Text/d' f.txt
will delete all the lines in the file containing the matching word "Text".
How do I combine these two to solve my problem?
Thank you.
It seems that you want to delete the lines containing Text within the first 30 lines in your file.
You can say:
sed '1,30{/Text/d}' f.txt
If you wanted to delete the first 30 lines and the lines containing Text anywhere in the file, you could say:
sed '1,30d;/Text/d' f.txt
You miss "-i" option:
sed -i '1,30{/Text/d}' f.txt
sed "/Text/ d;30 q" f.txt
should be faster, especially on huge file IF you discard line after 30. If not previous solution are the best

Resources