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?
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
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.
echo ${mytext#*:}:. This will properly handle lines like (123)(434): hello::{apple,orange,mango}. and 123434: hello::{apple,orange,mango}.
sedorawk