0

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```

2
  • grep ... | awk ... is an anti-pattern. Probably this could be done with a single awk (or sed) call. What are the lines containing the string xenial-infra-security in the file (demo.log) ? Commented Sep 27, 2022 at 8:53
  • Do you want to remove strings between special characters including special characters? Do the answer below help?
    – m19v
    Commented Sep 27, 2022 at 14:02

1 Answer 1

1

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

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