LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   remove duplicated stringd in a line... (https://www.linuxquestions.org/questions/programming-9/remove-duplicated-stringd-in-a-line-945162/)

masavini 05-15-2012 04:58 PM

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...

colucix 05-15-2012 05:24 PM

Code:

awk 'BEGIN{FS = "[ -]"}{ while(++i<=NF) printf (!a[$i]++) ? $i " " : ""; i=split("",a); print "" }'

masavini 05-15-2012 05:30 PM

many thanks colucix! it works great...
e anche il fatto che siamo entrambi di bologna credo sia una discreta coincidenza! :)

colucix 05-15-2012 05:38 PM

He he! Bologna forever! ;)

masavini 05-15-2012 05:43 PM

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
:)

colucix 05-15-2012 06:52 PM

Quote:

Originally Posted by masavini (Post 4679364)
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.

masavini 05-15-2012 07:00 PM

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... :)

colucix 05-16-2012 05:56 AM

Quote:

Originally Posted by masavini (Post 4679403)
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! ;)

grail 05-16-2012 07:01 AM

Here is a slight revision if you like:
Code:

echo "bubu gaga bubu-dede" | awk '{ORS=RT}!_[$0]++' RS="[ -\n]"

masavini 05-16-2012 07:32 AM

mmm...

Code:

$ echo "bubu gaga bubu-dede" | awk '{ORS=RT}!_[$0]++' RS="[ -\n]"
awk: cmd. line:1: fatal: Invalid range end: /[ -
]/


colucix 05-16-2012 07:52 AM

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]'

masavini 05-16-2012 08:04 AM

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: /[ -
]/


colucix 05-16-2012 08:23 AM

Which version of awk are you running?
Code:

awk --version

masavini 05-16-2012 08:25 AM

Code:

$ awk --version
GNU Awk 3.1.7


colucix 05-16-2012 08:36 AM

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 ]'


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