Replacing string characters using CLI

0 votes

I want to replace a few characters in a string. For eg. say replace all x,y,z with c in aabbxxddeeyyzzkk.

I know it can be done using

$string" | tr 'x' 'c' | tr 'y' 'c'

But I want some way to do this without using pipes.

Jun 11, 2019 in Linux Administration by Atul
• 10,240 points

edited Jun 11, 2019 by Atul 1,040 views

1 answer to this question.

0 votes

To replace each of xy and z with c use:

echo "$string" | tr xyz c

in order to replace the repeating occurrences with a single c use:

echo "$string" | sed -r 's/[xyz]+/c/g'

answered Jun 11, 2019 by DareDev
• 6,890 points

Related Questions In Linux Administration

0 votes
1 answer

How to remove brackets using sed?

Try this: sed 's/[()]//g' <<< Hi(hello).How are you? READ MORE

answered Feb 13, 2019 in Linux Administration by Omkar
• 69,220 points
4,374 views
0 votes
2 answers

how to loop through the content of a file using bash

#!/bin/bash for i in  `cat peptides.txt` do echo $i done READ MORE

answered Sep 5, 2020 in Linux Administration by Prakash K. Aithal
3,003 views
0 votes
1 answer

Downloading file using SSH from a server without web access

Run this command in  your terminal: scp your_username@remotehost.edu:foobar.txt ...READ MORE

answered Mar 5, 2019 in Linux Administration by ajs3033
• 7,300 points
1,079 views
0 votes
1 answer

redirecting stdout and stderr to file using bash

cmd >>file.txt 2>&1 Bash always executes and redirects ...READ MORE

answered Mar 19, 2019 in Linux Administration by ajs3033
• 7,300 points
22,368 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How do I set variable if a specific package version is installed in CFEngine?

Here is what you can do.Just use packagesmatching to ...READ MORE

answered Jul 12, 2018 in Other DevOps Questions by Atul
• 10,240 points
1,196 views
0 votes
2 answers

How do I find all files containing specific text on Linux?

grep -rnw 'text to find recursively inside ...READ MORE

answered May 20, 2019 in Linux Administration by Ritesh Singh
40,440 views
0 votes
1 answer

How to perform proxy http request using curl?

One of the ways is: export http_proxy=http://your.proxy.server:port/ Then you ...READ MORE

answered Mar 26, 2019 in Linux Administration by DareDev
• 6,890 points
4,102 views
0 votes
1 answer

Access Denied to user root@localhost while using mysql

For MySQL versions < 5.7: the default root ...READ MORE

answered May 13, 2019 in Linux Administration by DareDev
• 6,890 points
3,796 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP