Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have data of the following format that I want to input into LibreOffice calc

data | num   | num | num     | num

For some reason Libreoffice does not think the string "3214 " is a number by default (trailing white space).

I want to replace (\s)*| with | where \s stands for space and * for the Kleene star operation. And do this at multiple places in each line (all matches).

I tried:

sed  -i 's/(\s)*|/|/' DataStats0914.txt

But it has no effect.

share|improve this question
add comment

3 Answers

up vote 4 down vote accepted

Why would you quote it? If you want references, use \( and \) instead,

Also, note the g option, since that pattern apperas multiple times

sed -i 's/\s*|/|/g' DataStats0914.txt

Test case:

echo 'data | num | num | num | num' | sed 's/\s*|/|/g'

Got:

data| num| num| num| num

share|improve this answer
    
laziness of manual reading is the reason. But frankly the man pages intimidate me. –  Erik Oct 25 '12 at 4:17
add comment

There are several different regular expression syntaxes. (\s*) matches a sequence of whitespace in Perl and the many other modern programs that imitate its regexes. Sed uses an older variant of basic regular expressions in which parentheses stand for themselves and \(…\) is used for grouping. GNU sed (the version on Linux) has an option -r to use extended regular expressions (which Perl regexes extend further) instead. Also, | stands for itself in BRE but must be preceded by a backslash in ERE.

s/[ \t]\+|/|/ replaces whitespace followed by a | with GNU sed. If you want a POSIX-compliant sed command, use s/[ ][ ]*|/|/ where each pair of brackets contains a space and a tab, as \t and \+ are GNU extensions. Also, the s command only replaces one occurrence per line: add the g suffix to replace all occurrences.

sed  -i 's/[ \t]\+|/|/g' DataStats0914.txt
share|improve this answer
add comment

sed removing spaces:

sed -i 's/ *|/|/g' file
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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