Deleting lines begin with # but not the line #!/bin/ksh - sed

How can I delete lines that begin with # but not #!/bin/ksh?
Using sed -e '/^#/ d' sed.sh will delete every line including #!/bin/ksh.

Thanks #Daniel H
sed -e '/#!\/bin\/ksh/p' -e '/^#/d' sed.sh
this is the command that gave the result I was looking for:

Like this:
sed '/^#/{/^#!\/bin\/ksh/d}' sed.sh
For all lines that start with a #, if they don't start with #!/bin/ksh, delete them.
Since comments (and the shebang line) might have spaces in front, more precise would be
sed '/^[[:space:]]*#/{/^[[:space:]]*#![[:space:]]*\/bin\/ksh/d}' sed.sh

Related

sed: remove matching line and everything after

I'm trying to remove all lines after the first blank line in a file with a git filter using sed.
This seems to remove everything after the blank line
sed -i '/^$/q' test.rpt
How do I also include the blank line itself to be deleted?
If this is GNU sed, just use Q instead of q.
sed -i '/^$/Q' test.rpt
For BSD sed, use -n switch to suppress automatic printing, and print lines manually. E.g:
sed -n -i '/^$/q;p' test.rpt
PS: You might want to change the regex to ^[[:blank:]]*$ to regard lines of all blank characters as blank lines as well.
Try this:-
sed -i '/^$/,$ d' inputfile

Sed Remove 3 last digits from string

27211;18:05:03479;20161025;0;0;0;0;10991;0;10991;000;0;0;000;1000000;0;0;000;0;0;0;82
Second string after ; is time. gg:mm:sssss:. I just want to be gg:mm:ss:
Like so:
27211;18:05:03;20161025;0;0;0;0;10991;0;10991;000;0;0;000;1000000;0;0;000;0;0;0;82
I tried with cut but it deletes everything after n'th occurance of character, and for now I am stuck, please help.
give this one liner a try:
awk -F';' -v OFS=";" 'sub(/...$/,"",$2)+1' file
It removes the last 3 chars from column 2.
update with sed one liner
If you are a fan of sed:
sed -r 's/(;[^;]*)...;/\1;/' file
With sed:
sed -r 's/^([^;]+;[^;]+)...;/\1;/' file
(Or)
sed -r 's/^([^;]+;[0-9]{2}:[0-9]{2}:[0-9]{2})...;/\1;/' file
It also can be something like sed 's/(.*)([0-9]{2}\:){2}([0-9]{3})[0-9]*\;(.*)/\1\2\3\4/g'
It is not very clean, but at least is more clear for me.
Regards
I'd use perl for this:
perl -pe 's/(?<=:\d\d)\d+(?=;)//' file
That removes any digits between "colon-digit-digit" and the semicolon (first match only, not globally in the line).
If you want to edit the file in-place: perl -i -pe ...
With sed:
sed -E 's/(:[0-9]{2})[0-9]{3}/\1/' file
or perl:
perl -pe's/:\d\d\B\K...//' file

Using sed to keep the beginning of a line

I have a file in which some lines start by a >
For these lines, and only these ones, I want to keep the first eleven characters.
How can I do that using sed ?
Or maybe something else is better ?
Thanks !
Muriel
Let's start with this test file:
$ cat file
line one with something or other
>1234567890abc
other line in file
To keep only the first 11 characters of lines starting with > while keeping all other lines:
$ sed -r '/^>/ s/(.{11}).*/\1/' file
line one with something or other
>1234567890
other line in file
To keep only the first eleven characters of lines starting with > and deleting all other lines:
$ sed -rn '/^>/ s/(.{11}).*/\1/p' file
>1234567890
The above was tested with GNU sed. For BSD sed, replace the -r option with -E.
Explanation:
/^>/ is a condition. It means that the command which follows only applies to lines that start with >
s/(.{11}).*/\1/ is a substitution command. It replaces the whole line with just the first eleven characters.
-r turns on extended regular expression format, eliminating the need for some escape characters.
-n turns off automatic printing. With -n in effect, lines are only printed if we explicitly ask them to be printed. In the second case above, that is done by adding a p after the substitute command.
Other forms:
$ sed -r 's/(>.{10}).*/\1/' file
line one with something or other
>1234567890
other line in file
And:
$ sed -rn 's/(>.{10}).*/\1/p' file
>1234567890

Sed: can't define the pattern correctly, can you please assist?

I'm trying to add "ARG1$" to the end of this line:
command[check_net_speed]=/usr/lib64/nagios/plugins/check_net_speed.sh $
I've tried:
sed -e 's/^command\[check_net_speed\]$/$ARG1$/g' /etc/nagios/nrpe.cfg
sed -e 's/.*speed.*/$ARG1$/g' /etc/nagios/nrpe.cfg
But none did the trick... what's the right way to catch the pattern of the "check_net_speed" command and add "ARG1$" at the end of the line, so the line will look like this:
command[check_net_speed]=/usr/lib64/nagios/plugins/check_net_speed.sh $ARG1$
Something like
sed -e 's/^command\[check_net_speed\].*/&ARG1$/g' input
command[check_net_speed]=/usr/lib64/nagios/plugins/check_net_speed.sh $ARG1$
Change your sed command like below,
sed -e '/^command\[check_net_speed\]/s~$~ARG1$~' file
/^command\[check_net_speed\]/ matches the lines which starts with command[check_net_speed] and it do the replacement on those matched lines.
$ in the regex part means end of the line. So the above command replaces the end of the line anchor with ARG1$
Example:
$ echo 'command[check_net_speed]=/usr/lib64/nagios/plugins/check_net_speed.sh $' | sed -e '/^command\[check_net_speed\]/s~$~ARG1$~'
command[check_net_speed]=/usr/lib64/nagios/plugins/check_net_speed.sh $ARG1$
$ has a special meaning: end of line. To treat it as a literal, you have to escape it:
sed '/^command\[check_net_speed\].*\$/s/$/ARG1$/' file
This will replace the end of line (indicated by $ alone) with the string ARG1$. So at the end, ARG1$ will be appended to the line.
The /command/ part is used to perform this replacement only in the lines containing the string command.
Test
$ cat a
command[check_net_speed]=/usr/lib64/nagios/plugins/check_net_speed.sh $
ddd
$ sed '/^command\[check_net_speed\].*\$/s/$/ARG1$/' a
command[check_net_speed]=/usr/lib64/nagios/plugins/check_net_speed.sh $ARG1$
ddd
As a supplement to nu11p01n73R's answer. Use
sed -e 's/^command\[check_net_speed\].*\$$/&ARG1$/g;q' /etc/nagios/nrpe.cfg
;q after substitution command means stop processing the rest of this file after first match.

sed + remove "#" and empty lines with one sed command

how to remove comment lines (as # bal bla ) and empty lines (lines without charecters) from file with one sed command?
THX
lidia
If you're worried about starting two sed processes in a pipeline for performance reasons, you probably shouldn't be, it's still very efficient. But based on your comment that you want to do in-place editing, you can still do that with distinct commands (sed commands rather than invocations of sed itself).
You can either use multiple -e arguments or separate commands with a semicolon, something like (just one of these, not both):
sed -i 's/#.*$//' -e '/^$/d' fileName
sed -i 's/#.*$//;/^$/d' fileName
The following transcript shows this in action:
pax> printf 'Line # with a comment\n\n# Line with only a comment\n' >file
pax> cat file
Line # with a comment
# Line with only a comment
pax> cp file filex ; sed -i 's/#.*$//;/^$/d' filex ; cat filex
Line
pax> cp file filex ; sed -i -e 's/#.*$//' -e '/^$/d' filex ; cat filex
Line
Note how the file is modified in-place even with two -e options. You can see that both commands are executed on each line. The line with a comment first has the comment removed then all is removed because it's empty.
In addition, the original empty line is also removed.
#paxdiablo has a good answer but it can be improved.
(1) The '/^$/d' clause only matches 100% blank lines.
If you want to also match lines that are entirely whitespace (spaces, tabs etc.) use this instead:
'/^\s*$/d'
(2) The 's/#.*$//' clause only matches lines that start with the # character in column 0.
If you want to also match lines that have only whitespace before the first # use this instead:
'/^\s*#.*$/d'
The above criteria may not be universal (e.g. within a HEREDOC block, or in a Python multi-line string the different approaches could be significant), but in many cases the conventional definition of "blank" lines include whitespace-only, and "comment" lines include whitespace-then-#.
(3) Lastly, on OSX at least, the #paxdiablo solution in which the first clause turns comment lines into blank lines, and the second clause strips blank lines (including what were originally comments) doesn't work. It seems to be more portable to make both clauses /d delete actions as I've done.
The revised command incorporating the above is:
sed -e '/^\s*#.*$/d' -e '/^\s*$/d' inputFile
This tiny jewel removes all # comments, no matter where they begin in a line (see caution below):
sed -e 's/\s*#.*$//'
Example:
text="
this is a # test
#this is a test
#this is a #test
this is # another #test
"
$echo "$text" | sed -e 's/\s*#.*$//'
this is a
this is
Next this removes any resulting blank lines:
$echo "$text" | sed -e 's/\s*#.*$//' | sed -e '/^\s*$/d'
Caution: Depending on the syntax and/or interpretation of the lines your processing, this might not be an appropriate solution, as it just stupidly removes end of lines, even if the '#' is part of your data or code. However, for use cases where you'll never use a hash except for as an end of line comment then it works fine. So just as with all coding, context must be taken into consideration.
Alternative variant, using grep:
cat file.txt | grep -Ev '(#.*$)|(^$)'
you can use awk
awk 'NF{gsub(/^[ \t]*#/,"");print}' file
First example(paxdiablo) is very good except its not change file, just output result. If you want to change it inline:
sudo sed -i 's/#.*$//;/^$/d' inputFile
On (one of) my linux boxes, sed understands extended regular expressions with the -r option, so:
sed -r '/(^\s*#)|(^\s*$)/d' squid.conf.installed
is very useful for showing all non-blank, non comment lines.
The regex matches either start of line followed by zero or more spaces or tabs followed by either a hash or end of line, and deletes those matching lines from the input.