2
root@server:~/foo$ cat INPUT.txt
limit. Current u'^'ZO
limit. Current usage'^'FR
limit. Current us'^'AS
limit. Current usage 95 %.'^'FR
limit. Cur'^'AEW
root@server:~/foo$ sed "s/limit.SOMEMAGIC/limit./g" INPUT.txt
limit.'^'ZO
limit.'^'FR
limit.'^'AS
limit.'^'FR
limit.'^'AEW

Question: how can we delete the strings (" Cur......") after "limit." until the: " ' " ? Searching for "SOMEMAGIC" :)

  • Don't be root: Use a tool such as sudo so you don't have to be logged in as root. Else one day you will cut your own foot off. – ctrl-alt-delor Aug 7 '14 at 10:15
0

On the specific input you show, you could simply do

perl -pe "s/ Cur[^']*//" INPUT.txt 

or, in sed

sed "s/ Cur[^']*//" INPUT.txt 

These will simply delete a space followed by Cur and as many non-' characters as possible. If you need to do this only if the previous word was limit., use one of the other answers or:

awk "\$1~/limit\./{sub(/Cur[^']*/,\"\",\$2)}1;" INPUT.txt

There are some quoting acrobatics going on there because we need to enclose the awk scriptlet in " because we need the [^'] so the shell will expand $1 and $2 before they are passed to awk unless we escape them as \$1 and \$2.

4

You can use:

sed -e "s/^\(limit\.\)[^']*/\1/" < data

This matches limit. at the start of a line, followed by as many characters as possible that aren't ', and replaces it with just the part at the start (\1). You can edit the pattern and the replacement according to how your data actually is - the important part is [^']*.

2

With perl (version > 5.10.0):

$ perl -pe "s/^limit\.\K(.*?)\'/\'/" file
limit.'^'ZO
limit.'^'FR
limit.'^'AS
limit.'^'FR
limit.'^'AEW

From perldoc perlrebackslash:

\K  This appeared in perl 5.10.0. Anything matched left of "\K" is not
    included in $&, and will not be replaced if the pattern is used in a
    substitution. This lets you write "s/PAT1 \K PAT2/REPL/x" instead of
    "s/(PAT1) PAT2/${1}REPL/x" or "s/(?<=PAT1) PAT2/REPL/x".

    Mnemonic: Keep.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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