Multiple patterns in SED delete

In summary: You can use a custom script in whichever language you are comfortable with. Basically you need to read the file line by line and use regex to check if it matches any of the conditions you want to delete, and then write the non-matching lines to a new file. Another option is to use the egrep command, which allows for easier regex syntax. For example:egrep -v '(^$|^ |^ |^=|^/)' somefile > newfileThis command will ignore lines that start with an empty line, a line with a single space, a line with a tab, a line with an equal sign, or a line with a forward slash. You can add more conditions as needed.
  • #1
swartzism
103
0
I would like to delete all lines starting with the following

- \t (tab)
- \n (newline)
- /
- (single space)
- =

I've tried `sed -n '/^\(\t\|\n\|/\| \|=\)$/d' OUTPUT.txt > test.txt` and combos of that to no avail. What am I doing wrong?
 
Technology news on Phys.org
  • #2
Without special syntax sed will not look at \n the way you want. Plus \t will not work. You have to hit the tab key and get what seem to be spaces.
egrep syntax is easier for me at the moment - but you can use the regex - note that a bunch of spaces is not spaces:
^$ is regex for a line that starts with \n, the | symbol is alternation.
Code:
egrep -v '(^$|^   |^ |^=|^/)' somefile > newfile

You can also use POSIX character classes like
Code:
^[:space:]
to get either \t or ' ' (space).
 
  • #3
swartzism said:
I would like to delete all lines starting with the following

- \t (tab)
- \n (newline)
- /
- (single space)
- =

I've tried `sed -n '/^\(\t\|\n\|/\| \|=\)$/d' OUTPUT.txt > test.txt` and combos of that to no avail. What am I doing wrong?
Are you wanting to do this in windows or linux? As jim mcnamara indicated, sed is not the best choice; sed brings more problems than you could imagine.
 

1. What is SED delete?

SED delete, or "stream editor delete", is a command-line utility used in Unix and Linux systems to delete specific patterns from a text file or stream.

2. How does SED delete work?

SED delete works by taking in a set of instructions or commands, known as a "script", and applying those commands to each line of a given input. The script specifies which patterns to match and what actions to take on those matches, such as deleting them.

3. Can SED delete multiple patterns at once?

Yes, SED delete can handle multiple patterns at once. The script can include multiple commands, each targeting a different pattern, and all of them will be applied to the input in order.

4. Is SED delete case-sensitive?

By default, SED delete is case-sensitive, meaning it will only match patterns that have the exact same capitalization as specified in the script. However, there are options to make it case-insensitive if desired.

5. Are there any risks to using SED delete?

As with any command-line utility, there is always a risk of accidentally deleting important information with SED delete if the script is not carefully crafted. It is important to thoroughly test the script on a backup of the original file before applying it to important data.

Similar threads

  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
19
Views
1K
  • Programming and Computer Science
Replies
31
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
1
Views
962
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
2
Replies
36
Views
2K
  • Programming and Computer Science
Replies
4
Views
345
  • Programming and Computer Science
Replies
3
Views
1K
Replies
28
Views
571
Back
Top