I need to delete text from a line before delimiter.

For example:

(123434): hello::{apple,orange,mango}.

I need to delete text before first :. i.e. (123434).

Is there any command in linux to perform this task?

  • You might want to look into sed or awk – Paul Aug 11 '14 at 20:58
  • Is this text part of a file, or part of a bash variable? – Bernhard Aug 12 '14 at 13:39
  • It is part of file. – user80538 Aug 12 '14 at 14:34

This sed command should do the trick. The following command will overwrite the file:

sed -i 's/^[^:]*:/:/' file

To just print the output, remove the -i flag. To put the output in a new file, remove the -i flag and redirect the output:

sed 's/^[^:]*:/:/' file > new_file
  • The line did the magic... the output replaced in the file itself.. – user80538 Aug 12 '14 at 13:14
  • when i compare two files using sdiff, after removing the text from both files, the difference showing me the removed text for me. this command overwriting the file or just showing the output. – user80538 Aug 12 '14 at 13:19
  • It worked.. Can i extract 'hello' and {apple,orange,mango} from my example by making simple changes in the above command? – user80538 Aug 12 '14 at 14:36
  • @user80538, It's hard to tell what you want when you say extract in this context. Is this part of a larger shell script? You may want to ask a new question with the details of the larger problem you're trying to solve. – drs Aug 12 '14 at 14:42
  • It is just simple comparison of two files with same data but only need to find where the data is differing in two files. I need to check each individual section in the line . The data in individual sections may jumble. like {apple,orange,mango} can be {apple,mango,orange}. – user80538 Aug 12 '14 at 14:48

You can use cut:

echo '(123434): hello::{apple,orange,mango}.' | cut -d: -f2-

You can do this with a quite simple Bash instruction:

mytext="(123434): hello::{apple,orange,mango}."

echo ${mytext#*)}

This will print : hello::{apple,orange,mango}.. You can cut prefixes and suffixes in similar way. Moreover, prefix and suffix definitions can contain wildcards.

Cut prefix - up to first prefix wildcard match: ${variable#prefix}

Cut prefix - up to last prefix wildcard match: ${variable##prefix}

Cut suffix - up to first suffix wildcard match: ${variable%suffix}

Cut suffix - up to last suffix wildcard match: ${variable%%suffix}

It may look unclear at first sight, see the following example:

echo ${mytext#*A}    # will print: xBxAxBx
echo ${mytext##*A}   # will print: xBx
echo ${mytext%B*}    # will print: xAxBxAx
echo ${mytext%%B*}   # will print: xAx
echo ${mytext%%C*}   # will print: xAxBxAxBx

In the last example, pattern doesn't match, so nothing is cut.

  • It'd be more robust to use echo ${mytext#*:}:. This will properly handle lines like (123)(434): hello::{apple,orange,mango}. and 123434: hello::{apple,orange,mango}. – drs Aug 12 '14 at 13:55
  • is there any way to modify the command echo ${mytext%%B*} to get text after B. in your example – user80538 Aug 12 '14 at 14:43
  • @user80538 yes, just cut the text before B, inclusive ;-), i.e.: ${mytext#*B} for text after the first B, and ${mytext##*B} for text after the second B. – user77422 Aug 12 '14 at 15:41

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.