LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-11-2017, 10:47 AM   #1
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,814

Rep: Reputation: 639Reputation: 639Reputation: 639Reputation: 639Reputation: 639Reputation: 639
Extract alternate letters and append original string


This is done as a learning exercise.

Have: a file with one character string on each line.
The strings may be of any length.

Want: a file with two character strings on each line.
The first one consists of characters 1, 3, 5, 7, 9, (all odd numbers)
and the second one is an unaltered copy of the original string.

With this InFile ...
Code:
honda
toyota
chevrolet
123456789abcdef
... this code ...
Code:
sed -r 's/(.)./\1/g' $InFile  \
|paste -d " " - $InFile       \
>$OutFile
... produced this OutFile ...
Code:
hna honda
tyt toyota
cerlt chevrolet
13579bdf 123456789abcdef
This solution uses two commands. Is there some fancy footwork using the pattern space or hold space which would enable a single sed to do the whole job?

Daniel B. Martin

Last edited by danielbmartin; 09-11-2017 at 10:54 AM. Reason: Tighten the code, slightly.
 
Old 09-11-2017, 11:42 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,511
Blog Entries: 3

Rep: Reputation: 2773Reputation: 2773Reputation: 2773Reputation: 2773Reputation: 2773Reputation: 2773Reputation: 2773Reputation: 2773Reputation: 2773Reputation: 2773Reputation: 2773
You could make a copy of the original line in the hold space, do your substitution work, append the hold space to the pattern space, and then remove the newline in the middle:

Code:
sed -E -e 'h; s/(.)./\1/g; G; s/\n/ /;'
That looks like it does what you want in one line.
 
2 members found this post helpful.
Old 09-11-2017, 01:02 PM   #3
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,814

Original Poster
Rep: Reputation: 639Reputation: 639Reputation: 639Reputation: 639Reputation: 639Reputation: 639
Quote:
Originally Posted by Turbocapitalist View Post
Code:
sed -E -e 'h; s/(.)./\1/g; G; s/\n/ /;'
Nice! Thank you!

LQ won't let me rep you again (must spread it around, etc.). That's an annoying limitation because a good post is a good post, and should be recognized as such.

Daniel B. Martin

.
 
1 members found this post helpful.
  


Reply

Tags
sed, text processing


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sed search for a string, duplicate original and replace the string Jykke Programming 17 06-29-2016 10:14 AM
extract part of a string and reinsert back into original tlin Programming 5 02-15-2012 01:38 PM
how to check if all letters of one string are in another string cliffyao Programming 4 11-12-2010 10:41 AM
[SOLVED] How to extract module names and append to modules.autoload.d bianchiaz Linux - Newbie 4 09-07-2009 09:24 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration