22

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?

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

3 Answers 3

17

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
6
  • The line did the magic... the output replaced in the file itself.. Commented Aug 12, 2014 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. Commented Aug 12, 2014 at 13:19
  • It worked.. Can i extract 'hello' and {apple,orange,mango} from my example by making simple changes in the above command? Commented Aug 12, 2014 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. Commented Aug 12, 2014 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}. Commented Aug 12, 2014 at 14:48
17

You can use cut:

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

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:

mytext="xAxBxAxBx"
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.

3
  • 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}. Commented Aug 12, 2014 at 13:55
  • is there any way to modify the command echo ${mytext%%B*} to get text after B. in your example Commented Aug 12, 2014 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. Commented Aug 12, 2014 at 15:41

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.