Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have a text file and I need to get rid off the entire information just before the = symbol in each line, for example:

The line:

File 1515 = foo.txt

needs to look like this:

=foo.txt

So, I had this line using sed:

sed s/=[^=]*^// everyone.pls

but it deletes all the characters after the = symbol.

So, my question is: how can I delete the characters before =?

Don't forget that I don't want to delete the = symbol, I need just before it.

share|improve this question

5 Answers 5

To delete everything before the first equal sign:

sed 's/^[^=]*=/=/' everyone.pls

If the file everyone.pls looks like:

$ cat everyone.pls
File 1515 = foo.txt
File 1516 = foo=.xls

The the above command produces:

$ sed 's/^[^=]*=/=/' everyone.pls
= foo.txt
= foo=.xls

In the regex ^[^=]*=/, the leading caret means that the regex has to match starting at the beginning of the line. The expression [^=]*= matches every character up to and including the first equal sign. Because we want the equal sign in the output, we need to substitute for all that an equal sign.

share|improve this answer

You can use

sed 's/.*=/=/g' everyone.pls

to remove the characters before =

share|improve this answer
    
This will remove characters up until the last equals. This may or may not be what the OP wants. –  Boris the Spider Mar 24 at 8:34
    
OP mentioned that he need to get rid off the entire information just before the = symbol –  devav2 Mar 24 at 9:01
sed 's/.*= */=/' YourFile

in your sample there i s space after = that you remove also, just 'adapt' other reply that don't take it into account but what you ask (until =).

share|improve this answer

Here are some other options, these will all delete everything up to the first =.

  1. Perl

    perl -pe 's/.+?=/=/' everyone.pls
    

    The -p switch means "print every line" after applying the script passed with -e. s/pattern/replacement/ is the substitution operator and the regex means match any character up to the first =: . means any character and + means one or more and ? makes the match non-greedy, it will stop at the shortest matching string.

  2. awk

    awk -F= '{$1="=";print;}' everyone.pls 
    

    awk is a scripting language that automatically splits its input into fields and makes the fields available as $1,$2...$N. The -F= sets the field separator to = and $1="=" will therefore replace the contents of the line before the first equals sign with an = and print the rest.

  3. grep

    grep -o '=.*' everyone.pls 
    

    The -o cause grep to only print the matched portion of the line. So, we search for the first = up until the end of the line.

  4. bash

    while read line; do echo "=${line#*=}"; done < everyone.pls
    

    This is using a while loop to read each line, saving it in the variable $line and then using bash's string manipulation capabilities to modify it. The construct ${variable#pattern} will remove the shortest match of pattern from the end of $variable.

  5. Needlessly complex

    cut -d= -f 2- everyone.pls | sed 's/^/=/'  
    

    cut also works on fields, -d= makes it cut on =, -f 2- makes it print everything from the 2nd field to the end of the line and the sed substitution command adds an =.

share|improve this answer
    
In perl you can lookahead for the last = using (?==) and then you wouldn't have to replace the = back. I don't think sed supports that but ssed does. –  Boris the Spider Mar 24 at 8:33
    
@BoristheSpider, yes and lookarounds are often great but here they would just make the regex longer and harder to understand for non-experts, without adding any benefit. AFAIK, sed does not support PCREs, the best it can do is EREs with -r and they don't have lookarounds but thanks for mentioning ssed, did not know about it and it looks great! –  terdon Mar 24 at 13:56

You might see the problem the other way round and try to match the part you want to keep:

sed 's:.*\(=.*$\):\1:' everyone.pls

i.e. match everything but surround the part you are interested in in brackets then you add the first group back with \1

share|improve this answer

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.