3

I have a text file and I need to get rid of all characters before the equals symbol in each line, for example:

The line:

File 1515 = foo.txt

needs to look like this:

=foo.txt

I tried this sed command:

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

but it deletes all the characters after the equals symbol.

How can I delete the characters before =?

6 Answers 6

6

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.

4

You can use

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

to remove the characters before =

2
  • This will remove characters up until the last equals. This may or may not be what the OP wants. Mar 24, 2014 at 8:34
  • OP mentioned that he need to get rid off the entire information just before the = symbol
    – devav2
    Mar 24, 2014 at 9:01
1
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 =).

1

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 =.

2
  • 1
    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. Mar 24, 2014 at 8:33
  • 1
    @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, 2014 at 13:56
1

Almost right - just ordered your expressions backwards. What you want to do is replace all characters which are not = up to the first character which is = and any following spaces with a single = character.

Like this:

sed 's/[^=]*= */=/'

Your expression instead replaces all characters including and following the first = character on a line with a =.

And never mind the initial ^ leftmost anchor -; sed reads input left to right anyway, and so if it reads in an entire input line matching 0 = characters it will do nothing, but for all others it will only apply it's rule up to the leftmost = o a line.

0

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .