LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to delete every 2nd character from a string (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-delete-every-2nd-character-from-a-string-4175617895/)

freeroute 11-18-2017 01:49 PM

How to delete every 2nd character from a string
 
Is there a way to extract(delete) every 2nd character from a string with command line?
How can I do that?

Example:
Original string:
$826y4$31226$dZbD2JfjeZ3TbIe44M0zclck2O4T1l8j7Y7jfl6maZ2ecU900ObFJk9iz8iCE5AODPQx4QkiQjJOVmG

Result :
$2y$12$ZDJjZTI4MzlkOTljYjlmZeU0OFJk9iz8iCE5AODPQx4QkiQjJOVmG

Deleted(extracted) characters: 864326db2fe3be40cc241877f6a2c90b

Other examples:examples

Thanks for you help.

Ser Olmy 11-18-2017 02:41 PM

sed can do that if you combine a backreference with greedy mode:
Code:

~$ echo 0123456789 | sed "s/\(.\)./\1/g"
02468


TB0ne 11-18-2017 03:51 PM

Quote:

Originally Posted by freeroute (Post 5782258)
Is there a way to extract(delete) every 2nd character from a string with command line? How can I do that?

Example: Original string:
$826y4$31226$dZbD2JfjeZ3TbIe44M0zclck2O4T1l8j7Y7jfl6maZ2ecU900ObFJk9iz8iCE5AODPQx4QkiQjJOVmG

Result : $2y$12$ZDJjZTI4MzlkOTljYjlmZeU0OFJk9iz8iCE5AODPQx4QkiQjJOVmG

Deleted(extracted) characters: 864326db2fe3be40cc241877f6a2c90b

I'm seeing a pattern here:
https://www.linuxquestions.org/quest...ex-4175617261/
https://www.linuxquestions.org/quest...rd-4175617127/
https://www.linuxquestions.org/quest...le-4175614474/
https://www.linuxquestions.org/quest...ue-4175613273/

Seems like you've asked many questions like this, and shown no effort of your own when posting, rather just ask for a handout. Can you show us what YOU have done/tried on your own? Can you not apply any of the answers/advice you've been given before and change it to fit your needs? A lot of these sound like verbatim homework questions.

Read the LQ Rules and the "Question Guidelines" link in my posting signature.

freeroute 11-20-2017 06:21 PM

Quote:

Originally Posted by Ser Olmy (Post 5782270)
sed can do that if you combine a backreference with greedy mode:
Code:

~$ echo 0123456789 | sed "s/\(.\)./\1/g"
02468


Great...Thank you very much for your help.

JeremyBoden 11-20-2017 07:06 PM

But how would you do it, without using sed?

For example, can the OP do it with a bash script? :)


All times are GMT -5. The time now is 06:17 PM.