The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help removing lines with duplicated columns yahyaaa Shell Programming and Scripting 14 05-17-2008 04:33 AM
remove duplicated columns kamel.seg Shell Programming and Scripting 6 02-21-2008 03:36 AM
Can root ID be duplicated lorcan UNIX for Advanced & Expert Users 6 06-15-2007 08:06 PM
grep and delete 2nd duplicated of txt... -part2 happyv Shell Programming and Scripting 16 01-19-2007 01:40 AM
Log files duplicated themselves? DISTURBED UNIX for Dummies Questions & Answers 4 07-01-2002 06:54 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-10-2008
Registered User
 

Join Date: Jul 2006
Posts: 73
Stumble this Post!
using sed to get rid of duplicated columns...

I can not figure out this one, so I turn to unix.com for help, I have a file, in which there are some lines containing continuously duplicate columns, like the following

adb abc abc asd adfj
123 123 123 345
234 444 444 444 444 444 23

and the output I want is

adb abc asd adfj
123 345
234 444 23

Is it possible using sed to do this?

oh, btw, I thought this should work,

sed 's/(\([^ ]\)+ )+/\1/' file , but it does not...

Last edited by fedora; 04-10-2008 at 12:53 PM.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 04-10-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
Stumble this Post!
You need to have backrefs already in the first part to make sure you are actually replacing repeats, otherwise it will simply reduce every line to one token (or two, if you require a trailing space after the replacement). Also you need to make up your mind on whether your sed requires a backslash before grouping parentheses or not.

Code:
sed 's/\([^ ]* \)\1*/\1/g' file
I used * instead of +; if your sed understands the plus, then by all means use that.
Reply With Quote
  #3 (permalink)  
Old 04-10-2008
Registered User
 

Join Date: Jul 2006
Posts: 73
Stumble this Post!
Thanks, I messed up, time to go over sed and regexp again....


Quote:
Originally Posted by era View Post
You need to have backrefs already in the first part to make sure you are actually replacing repeats, otherwise it will simply reduce every line to one token (or two, if you require a trailing space after the replacement). Also you need to make up your mind on whether your sed requires a backslash before grouping parentheses or not.

Code:
sed 's/\([^ ]* \)\1*/\1/g' file
I used * instead of +; if your sed understands the plus, then by all means use that.
Reply With Quote
  #4 (permalink)  
Old 04-10-2008
Registered User
 

Join Date: Jul 2006
Posts: 73
Stumble this Post!
hmm, a second though, it seems that something is still wrong

>cat /tmp/test
123 123 123 345
akljsdfaljskd 7878 7878 7878 7878 123
akljsdfaljskd 7878 7878 7878 7878 123 123 123 345 234 345 345

>sed 's/\([^ ]* \)\1*/\1/g' /tmp/test
123 345
akljsdfaljskd 7878 123
akljsdfaljskd 7878 123 345 234 345 345

note the last line, the duplicate "345" are still there
Reply With Quote
  #5 (permalink)  
Old 04-10-2008
Registered User
 

Join Date: Jul 2006
Posts: 73
Stumble this Post!
hmm, that last column does not have "space", which is why...
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:21 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0