How to use sed to delete multiple lines when the pattern is matched and stop until the first blank line? - linux

I am very new to shell script.
How can I delete multiple lines when the pattern is matched and stop deleting until the first blank line is matched?

You can do this:
sed '/STARTING_PATTERN/,/^$/d' filename
This will select all the lines starting from STARTING_PATTERN upto a blank line ^$ and then delete those lines.
To edit files in place, use -i option.
sed -i '/STARTING_PATTER/,/^$/d' filename
Or using awk:
awk 'BEGIN{f=1} /STARTING_PATTERN/{f=0} f{print} !$0{f=1}' filename

Related

Copy lines from one file to another in Linux excluding comments

How do I copy lines from one file to another in Linux without opening source and destination files and I need to exclude the comments when copying the lines.
I do not want to copy the comments in the first file and the files are in different locations
Assuming lines are commented with # at the very beginning of each line, the following should work:
grep -v "^#" path/to/input/file >path/to/output/file
(Note: This will either create a new output file or irreversibly overwrite the output file if it already exists)
Assuming comment lines in your file contain # at the beginning of each line, the following sed command will delete these lines:
$ sed '/^#/d' path/to/input-file > path/to/output-file
If your file can also contain lines with whitespace before the #, the following sed command will delete lines beginning with zero or more spaces or tabs (in any order), followed by a hash (#) character:
$ sed '/^[ \t]*#/d' path/to/input-file > path/to/output-file
If your file also contains lines containing code followed by a comment, the following sed command should work:
$ sed -e '/^[ \t]*#/d' -e 's/#.*$//' path/to/input-file > path/to/output-file

blank lines in new file written by sed

I used sed '/pattern/d' file > newfile to remove some lines in a text file, but there are lots of blank lines left in the new file.
How can I modify the command to avoid the blank lines?
sed '/pattern/d; /^$/d' file > newfile
There is some good discussion about regular expressions for deleting empty lines in a file in this Stack Overflow post
sed '/^$/d' file >newfile
...will do it for you.
The command that you use will delete all the lines you want to delete, but it leaves the blank lines that are already there in the original file in place. To delete these too, simply apply a second filter to the input:
$ sed -e '/pattern/d' -e '/^[:blank:]*$/d' <file >newfile
The second filter will remove lines that are empty, or that contains only whitespace characters (i.e., that are "blank" in their appearance).

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.

Inserting string in file in nth line after pattern using sed

I want to insert word after nth line after pattern using sed.
I tied to modify this command but it inserts only in first line after pattern.
sed -i '/myPattern/a \ LineIWantToinser ' myFile
What command should I use to insert for example in third line after pattern?
Easiest way to do it with GNU sed is.. (maybe some direct solution exists!?)
sed -n '/pattern/=' file
to see line where pattern is (grep also can be used here with -n)
then if linenumber+ numoflines is for example 123
sed '123aSOME INSERTED TEXT AFTER THAT LINE' file
where little a is append command (after that line, if i is used will be pre pattern line)
ps. I'm eager to see if #neronlevelu (or other sed Lover) will find some better sed solution.
Edit: i've found it, it seems a for append or i for insert must? be on first position on line when using { with ; inside } like
sed '/pattern/{N;N;N;
a SOME TEXT FOR INSERTING
}' file
sed '/pattern/{N;N;N;i \
Line to add after 3 lines with patterne as starting counter
' YourFile
number of N to add line between pattern and inserted line.
there is no check for end of file or pattern in the 3 lines. (not specified in PO)
A version with bash and ed:
ed -s myFile <<<$'/myPattern/+3a\n LineIWantToinser \n.\nwq'
ed enables us to use the line addressing /myPattern/+3.

sed replace string in a first line

How can I replace a string but only in the first line of the file using the program "sed"?
The commands s/test/blah/1 and 1s/test/blah/ don't seem to work. Is there another way?
This might work for you (GNU sed):
sed -i '1!b;s/test/blah/' file
will only substitute the first test for blah on the first line only.
Or if you just want to change the first line:
sed -i '1c\replacement' file
This will do it:
sed -i '1s/^.*$/Newline/' textfile.txt
Failing that just make sure the match is unique to line one only:
sed -i 's/this is line one and its unique/Changed line one to this string/' filename.txt
The -i option writes the change to the file instead of just displaying the output to stdout.
EDIT:
To replace the whole line by matching the common string would be:
sed -i 's/^.*COMMONSTRING$/Newline/'
Where ^ matches the start of the line, $ matches the end of the line and .* matches everything upto COMMONSTRING
this replaces all matches, not just the first match, only in the first line of course:
sed -i '1s/test/blah/g' file
the /g did the trick to replace more than one matches, if any exists.

Resources