Home > Software design >  How to remove string between two special characters in bash
How to remove string between two special characters in bash

Time:10-01

Hi how can remove the words between two special characters in bash like from / to |

#cat demo.log| grep -i xenial-infra-security | awk '{print $1"|"$2}'
 libtiff5/**xenial-infra-security**|4.0.6-1ubuntu0.8 esm3
 tzdata/**xenial-infra-security,xenial-infra-security**|2022c-0ubuntu0.16.04 esm1```

CodePudding user response:

One can do it e.g. with sed as follows:

# Including special characters
echo "libtiff5/**xenial-infra-security**|4.0.6-1ubuntu0.8 esm3" | sed 's/\/\(.*\)|//g'
# Output: libtiff54.0.6-1ubuntu0.8 esm3

# Excluding special characters
echo "libtiff5/**xenial-infra-security**|4.0.6-1ubuntu0.8 esm3" | sed 's/\/\(.*\)|/\/|/g'
# Output: libtiff5/|4.0.6-1ubuntu0.8 esm3

or if you have your text in e.g. file.txt:

sed -e 's/\/\(.*\)|//g' -i file.txt
cat file.txt
# Output: 
# libtiff54.0.6-1ubuntu0.8 esm3
# tzdata2022c-0ubuntu0.16.04 esm1

P.S. If you want to replace the strings between two special charachters with e.g. - for readability:

echo "libtiff5/**xenial-infra-security**|4.0.6-1ubuntu0.8 esm3" | sed 's/\/\(.*\)|/-/g'
#Output: libtiff5-4.0.6-1ubuntu0.8 esm3
  • Related