LinuxQuestions.org
Review your favorite Linux distribution.
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 05-15-2012, 04:58 PM   #1
masavini
Member
 
Registered: Jun 2008
Posts: 285

Rep: Reputation: 6
remove duplicated stringd in a line...


hi,
i have this code:

Code:
$ echo "bubu gaga bubu dede" | awk '{ while(++i<=NF) printf (!a[$i]++) ? $i FS : ""; i=split("",a); print "" }'
bubu gaga dede
i need to modify the awk command so that "-" is considered a field separator (like " ").

i.e.:
Code:
$ echo "bubu gaga bubu-dede" | awk ....
bubu gaga dede
can you help me?
many thanks...
 
Old 05-15-2012, 05:24 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Code:
awk 'BEGIN{FS = "[ -]"}{ while(++i<=NF) printf (!a[$i]++) ? $i " " : ""; i=split("",a); print "" }'
 
Old 05-15-2012, 05:30 PM   #3
masavini
Member
 
Registered: Jun 2008
Posts: 285

Original Poster
Rep: Reputation: 6
many thanks colucix! it works great...
e anche il fatto che siamo entrambi di bologna credo sia una discreta coincidenza!
 
Old 05-15-2012, 05:38 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
He he! Bologna forever!
 
Old 05-15-2012, 05:43 PM   #5
masavini
Member
 
Registered: Jun 2008
Posts: 285

Original Poster
Rep: Reputation: 6
btw,
if you don't mind, would you take a look at this thread, as well?
http://www.linuxquestions.org/questi...ations-945160/

if you could help me, you'd surely increase your bonus to get a new battery for your laptop: removed

Last edited by masavini; 05-15-2012 at 06:55 PM.
 
Old 05-15-2012, 06:52 PM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by masavini View Post
btw,
if you don't mind, would you take a look at this thread, as well?
http://www.linuxquestions.org/questi...ations-945160/

if you could help me, you'd surely increase your bonus to get a new battery for your laptop:
Are you trying to corrupt a LQ mod?! Jokes apart, please remove the commercial link (it could denote advertising). Thank you. Ciao.
 
Old 05-15-2012, 07:00 PM   #7
masavini
Member
 
Registered: Jun 2008
Posts: 285

Original Poster
Rep: Reputation: 6
ops...
i guess corruption is when you try to let someone do something he shouldn't... but i can't see why you shouldn't help me with that problem...
 
Old 05-16-2012, 05:56 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by masavini View Post
ops...
i guess corruption is when you try to let someone do something he shouldn't... but i can't see why you shouldn't help me with that problem...
I was joking! Actually I tried to help, but it was late in the night and I wasn't able to write down a (working) code. However, Nominal Animal solved the issue in my behalf!
 
Old 05-16-2012, 07:01 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Here is a slight revision if you like:
Code:
echo "bubu gaga bubu-dede" | awk '{ORS=RT}!_[$0]++' RS="[ -\n]"
 
1 members found this post helpful.
Old 05-16-2012, 07:32 AM   #10
masavini
Member
 
Registered: Jun 2008
Posts: 285

Original Poster
Rep: Reputation: 6
mmm...

Code:
$ echo "bubu gaga bubu-dede" | awk '{ORS=RT}!_[$0]++' RS="[ -\n]"
awk: cmd. line:1: fatal: Invalid range end: /[ -
]/
 
Old 05-16-2012, 07:52 AM   #11
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
It depends on the shell you're using. It works in bash. Anyway, since the problem happens outside the awk one-liner, put the RS definition inside it and the trick is done:
Code:
echo "bubu gaga bubu-dede" | awk 'BEGIN{RS="[ -\n]"}{ORS=RT}!_[$0]++'
or eventually try single quotes in place of double quotes:
Code:
echo "bubu gaga bubu-dede" | awk '{ORS=RT}!_[$0]++' RS='[ -\n]'
 
Old 05-16-2012, 08:04 AM   #12
masavini
Member
 
Registered: Jun 2008
Posts: 285

Original Poster
Rep: Reputation: 6
mmm...

Code:
$ echo "bubu gaga bubu-dede" | awk '{ORS=RT}!_[$0]++' RS='[ -\n]'
awk: cmd. line:1: fatal: Invalid range end: /[ -
]/
$ echo "bubu gaga bubu-dede" | awk 'BEGIN{RS="[ -\n]"}{ORS=RT}!_[$0]++'
awk: fatal: Invalid range end: /[ -
]/
 
Old 05-16-2012, 08:23 AM   #13
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Which version of awk are you running?
Code:
awk --version
 
Old 05-16-2012, 08:25 AM   #14
masavini
Member
 
Registered: Jun 2008
Posts: 285

Original Poster
Rep: Reputation: 6
Code:
$ awk --version
GNU Awk 3.1.7
 
Old 05-16-2012, 08:36 AM   #15
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
The same for me. At this point it might depend on the locale settings and the ambiguous interpretation of the dash - inside the character list. Your best bet is to move it at the beginning of the list:
Code:
echo "bubu gaga bubu-dede" | awk '{ORS=RT}!_[$0]++' RS='[-\n ]'
 
  


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
[SOLVED] duplicated title line in Ubuntu 11.10 peter576 Linux - Newbie 1 12-03-2011 10:50 AM
[SOLVED] Remove line break if next line does not start with "PX" joje47 Programming 8 05-25-2011 08:50 AM
deleting duplicate lines without deleting first instance of the duplicated line jkeertir Linux - Newbie 2 02-07-2011 06:55 AM
Get duplicated names listed in /etc/passwd including the line number... shorte85 Linux - Newbie 6 03-23-2009 10:52 AM
How to remove duplicated launcher on gnome panel menu? openfun Debian 8 10-06-2005 06:49 PM

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

All times are GMT -5. The time now is 06:51 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