Multiple patterns in SED delete

Click For Summary
To delete lines starting with specific characters such as tab, newline, slash, single space, or equals sign, the initial attempt using `sed` was ineffective due to its handling of special characters. The correct approach involves using `egrep`, which simplifies the process. The suggested command is `egrep -v '(^$|^ |^ |^=|^/)' somefile > newfile`, effectively filtering out unwanted lines. Additionally, utilizing POSIX character classes like `^[:space:]` can capture both tabs and spaces. It's noted that `sed` may not be the best tool for this task, especially in a Windows environment, as it can introduce complications.
swartzism
Messages
103
Reaction score
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
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).
 
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.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 19 ·
Replies
19
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 31 ·
2
Replies
31
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 75 ·
3
Replies
75
Views
6K
  • · Replies 36 ·
2
Replies
36
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
Replies
4
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K