LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 01-19-2010, 12:16 PM   #1
saurin
Member
 
Registered: Aug 2008
Posts: 50

Rep: Reputation: 0
how to remove two alternate char from file


How to remove alternate char from file using shell script?
if the file content is "1234567890" the output file should be "24680"

somebody already told me that following is the solution and it is working.

sed 's/\(.\)\(.\)/\2/g' filename

now there may be small modification to generate two alternate byte, what it should be?
if the file content is "1234567890" the output file should be "125689"
 
Old 01-19-2010, 12:45 PM   #2
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Just replace the . by .. in the two grouping expressions.

Read man sed or info sed for an explanation of the stream editor.

Anyhow, the dot matches any single character, the parenthesis define groups of expressions, and the \2 refers to the second group, so s/\(.\)\(.\)/\2/g means substitute for every group of two characters the second character of the set. Thus sed 's/\(..\)\(..\)/\2/g' filename would substitute, for every group of four characters, the last two characters in the group.

By the way, most distributions are shipped with tools to convert from unicode to to alternative encodings.
 
Old 01-19-2010, 01:07 PM   #3
saurin
Member
 
Registered: Aug 2008
Posts: 50

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by PTrenholme View Post
Just replace the . by .. in the two grouping expressions.

Read man sed or info sed for an explanation of the stream editor.

Anyhow, the dot matches any single character, the parenthesis define groups of expressions, and the \2 refers to the second group, so s/\(.\)\(.\)/\2/g means substitute for every group of two characters the second character of the set. Thus sed 's/\(..\)\(..\)/\2/g' filename would substitute, for every group of four characters, the last two characters in the group.

By the way, most distributions are shipped with tools to convert from unicode to to alternative encodings.
can we pass the particular range like if i want to remove character which occurs at every 100 character interval then is there any method?
 
Old 01-19-2010, 03:54 PM   #4
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Sure. Look at the (<expression>){n,m} construction in the manual or info documentation (referenced above) for details. (Or look at info grep in the "Regular Expressions" section.) If you don't have it installed, the pinfo command is a nice-to-have "enhanced" info reader. It replaces both the info and man commands and is (IMHO) somewhat easier to navigate.
 
Old 01-19-2010, 10:46 PM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
No need regex. KISS
Code:
# echo "1234567890" | awk -vFS= '{for(i=2;i<=NF;i+=2) printf $i}'
24680
 
  


Reply



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
how to remove alternate char from file saurin Programming 1 01-14-2010 04:53 PM
Menuconfig+load alternate config file option gauravholey Linux - Kernel 1 07-03-2009 01:51 AM
Read First Char from file in C++ ckoniecny Programming 2 09-30-2006 03:43 AM
remove singel char lines Ljohan Programming 4 03-16-2006 04:11 AM
alternate config file for sshd w/ RH EL3 kmitz Linux - Security 1 01-13-2005 04:46 PM

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

All times are GMT -5. The time now is 01:45 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
Open Source Consulting | Domain Registration