DQdlM
DQdlM

Reputation: 10244

How can I get sed to remove `\` followed by anything?

I am trying to write a sed script to convert LaTeX coded tables into tab delimited tables.

To do this I need to convert & into \t and strip out anything that is preceded by \.

This is what I have so far:

s/&/\t/g
s/\*/" "/g

The first line works as intended. In the second line I try to replace \ followed by anything with a space but it doesn't alter the lines with \ in them.

Any suggestions are appreciated. Also, can you briefly explain what suggested scripts "say"? I am new to sed and that really helps with the learning process!

Thanks

Upvotes: 0

Views: 352

Answers (2)

fiftyeight
fiftyeight

Reputation: 378

You need to escape the backslash as it is a special character.
If you want to denote "any character" you need to use . (a period)

the second expression should be:

s/\\.//g

I hope I understood your intention and you want to strip the character after the backslash, if you want to delete all the characters in the line after the backslash add a star (*) after the period.

Upvotes: 1

Jared Ng
Jared Ng

Reputation: 5071

Assuming you're running this as a sed script, and not directly on the command line:

s/\\.*/ /g

Explanation:

\\ - double backslash to match a literal backslash (a single \ is interpreted as "escape the following character", followed by a .* (. - match any single character, * - arbitrarily many times).

Upvotes: 2

Related Questions