LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 09-03-2010, 06:54 PM   #1
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,004

Rep: Reputation: 67
Sed, how do I match even characters only?


Say I had a string like so


TTAACC
BBDDXX
Let's say I only wanted to grab TAC and BDX the ones in the 2nd, 4th, and 6th spot.

sed -e '/^[A-Z]/\!d' file

Will grab the lines from the rest of the file assuming these are the only lines with capital letters at the very beginning of each line.

sed -e 's/^[A-Z]\{1\}//' file

Will delete the first 'T'. What I want to do is learn how to just pick the even characters, T(T)A(A)C(C) & B(B)D(D)X(X). Is there something like the \{1\} which can pick just the even or odd character matches?

How would I go about doing this?
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 09-03-2010, 07:50 PM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Hi,

try this
Code:
sed -r 's/(.)(.)/\1/g' infile    # will print every "odd" character
sed -r 's/(.)(.)/\2/g' infile    # will print every "even" character
You can verify that it works by using following test data:
tTaAcC
bBdDxX

The first sed will only print the lower case letters, the second one only the upper case letters.
Hope this helps.

[EDIT]
You can actually also do
Code:
sed -r 's/(.)./\1/g' infile    # will print every "odd" character
sed -r 's/.(.)/\1/g' infile    # will print every "even" character
Notice that the backreference now is \1 in both cases. However, the backreferences in the first example sort of relate to the "odd/even" aspect of the problem.

Last edited by crts; 09-03-2010 at 07:58 PM.
 
2 members found this post helpful.
Old 09-03-2010, 08:05 PM   #3
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,004

Original Poster
Rep: Reputation: 67
Wow that is awesome. Gotta read up on extended regex. Thanks crts.
 
Old 09-03-2010, 08:11 PM   #4
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Quote:
Originally Posted by trist007 View Post
Wow that is awesome. Gotta read up on extended regex. Thanks crts.
You are welcome.
 
  


Reply


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
Regular expression to match unspecified number of characters until a '>' cygnal Linux - General 7 07-15-2010 01:21 PM
sed - last occurence of a match mr_scary Linux - Desktop 7 02-13-2009 01:44 PM
irrelevant characters match in PERL pattern matching gaynut Programming 2 08-21-2008 11:52 PM
grep/sed/awk - find match, then match on next line gctaylor1 Programming 3 07-11-2007 09:55 AM
Regular expression to match 4 or more alpha characters sixerjman Programming 15 11-27-2006 01:03 AM


All times are GMT -5. The time now is 01:38 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration