6

I have a file that contains the following lines:

SUKsoft:
SUKsoft: App-Conduct_Risk_Comment
SUKsoft: App-Conduct_Risk_R
SUKsoft: App-Conduct_Risk_RW

How can I remove the line "SUKsoft:"?

This line could be in any place of the file (beginning as it is now, or in the middle).

Is there a command to do this?

3

grep searches for lines that satisfy a pattern. grep -v discards lines that satisfy a pattern.

grep -v '^SUKsoft: *$'

The pattern is: lines that start (^) with SUKsoft:, possibly followed by spaces but nothing else until the end of the line ($).

8

To remove the line use

sed -i '/SUKsoft:\s*$/d' your_file 

Example

% cat foo
SUKsoft: 
SUKsoft: App-Conduct_Risk_Comment   
SUKsoft: App-Conduct_Risk_R
SUKsoft: App-Conduct_Risk_RW

% sed -i '/SUKsoft:\s*$/d' foo

% cat foo                    
SUKsoft: App-Conduct_Risk_Comment   
SUKsoft: App-Conduct_Risk_R
SUKsoft: App-Conduct_Risk_RW
  • You are actually doing it the other way around as matching all lines having SUKsoft:[space][any stuff after that] @souravc answer is much closer to the op question – Kwaadpepper Oct 29 '15 at 13:22
  • Any stuff after that? What do you mean? \s*$ means, many whitespaces as possible and a line end. There is no any stuff – A.B. Oct 29 '15 at 13:26
  • Oh never mind, misread that – Kwaadpepper Oct 29 '15 at 13:30
  • 2
    Note that \s would need -E on most other platforms and may even be unsupported altogether. Since this is Ubuntu specific site it's fine. [[:space:]] for Posix. – Melvyn Oct 29 '15 at 18:20
  • @Melvyn thank you for the note. I will add this. – A.B. Oct 30 '15 at 6:18
4

Here are the steps to remove the desired line:

$ sed 's/SUKsoft: *$//' file.txt

SUKsoft: App-Conduct_Risk_Comment
SUKsoft: App-Conduct_Risk_R
SUKsoft: App-Conduct_Risk_RW

I am assuming file.txt contains the lines.

Or,

$ sed 's/SUKsoft: *$//; /^$/d' file.txt
SUKsoft: App-Conduct_Risk_Comment
SUKsoft: App-Conduct_Risk_R
SUKsoft: App-Conduct_Risk_RW

it does not leave any blank line.

To edit in the file you can use,

sed -i 's/SUKsoft: *$//' file.txt

or

sed -i 's/SUKsoft: *$//; /^$/d' file.txt

as per your need.

See A.B's answer where it is done in more compact manner. Thanks to Wildcard.

  • 2
    Note that there is a space after the : in the OP's text. The space would need to be included in the regex for this to work. I would recommend inserting ` *` (space asterisk) after the colon and before the $ in the regex, so that any number of spaces (including 0) would be matched. Also, just to be sure you don't match unexpected lines, it might be good to prepend a ^. – Wildcard Oct 29 '15 at 13:08
  • Also, I only realized it when I read A.B.'s answer, but (1) the g flag isn't doing anything here (since there can only be a match at the end of the line anyways) and (2) this will leave blank lines in the file as opposed to removing the lines completely. – Wildcard Oct 29 '15 at 13:15
1

Judging from your post't raw source there's no space or sequence of spaces after "SUKsoft:", however just to be on the safe side this command will take care of those if present.

Using Perl:

perl -ne '!/^SUKsoft: *$/&&print' input
  • !/^SUKsoft:$/&&print: if the current line doesn't match the pattern ^SUKsoft: *$, which matches a line starting with SUKsoft: string followed by zero or more spaces, prints the line;
% cat input
SUKsoft:
SUKsoft: App-Conduct_Risk_Comment
SUKsoft: App-Conduct_Risk_R
SUKsoft: App-Conduct_Risk_RW
% perl -ne 'print unless /^SUKsoft: *$/' input
SUKsoft: App-Conduct_Risk_Comment
SUKsoft: App-Conduct_Risk_R
SUKsoft: App-Conduct_Risk_RW

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.