Removing characters and some output

Hi guys,

So I am using an awk command to return a specific column in a file.

I also need to remove some extra characters.

What I have is ::

(http-/0.0.0.0:8091-9)|23:00:41

And what I want is :

http-/0.0.0.0:8091-9

I tried using the td command but it is only removing the ( , ) and | giving me :

http-/0.0.0.0:8091-923:00:41

which is wrong.

What other command can I use ?

echo "(http-/0.0.0.0:8091-9)|23:00:41" | perl -nle '/\((.+)\)\|\d/ and print $1'

I suspect that you can do much better if you extract the value clean from the first pass when you use awk to get the specific column.
If you post a few lines from the original file containing representative data, maybe a straight shot can be suggested.

1 Like

It would seem that the awk command:

awk -F'[()]' '{print $2}' file

does exactly what you want.

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk .

1 Like

Thanks guys. Both solutions work perfectly.

Another issue that I have.

I have a file:

[user@server tmp]$ cat list 
http-/0.0.0.0:8091-1
http-/0.0.0.0:8091-4
http-/0.0.0.0:8091-1
http-/0.0.0.0:8091-10
http-/0.0.0.0:8091-7
http-/0.0.0.0:8091-17
http-/0.0.0.0:8091-15

Now I want only unique strings to be returned.

[user@server tmp]$ cat list  | uniq
http-/0.0.0.0:8091-1
http-/0.0.0.0:8091-4
http-/0.0.0.0:8091-1
http-/0.0.0.0:8091-10
http-/0.0.0.0:8091-7
http-/0.0.0.0:8091-17
http-/0.0.0.0:8091-15

Any idea why the uniq command isnt working ? I am getting the string http-/0.0.0.0:8091-1 twice.

The uniq utility looks for adjacent duplicate lines (i.e., it is normally used when given sorted input). But, if you're using awk anyway, there is no need to invoke uniq nor to sort the awk output. Try:

awk -F'[()]' '{list[$2]} END{for(i in list) print i}' file

or:

awk -F'[()]' 'list[$2]++ == 0 {print $2}' file
perl -nle 'print unless $seen{$_}++' list
http-/0.0.0.0:8091-1
http-/0.0.0.0:8091-4
http-/0.0.0.0:8091-10
http-/0.0.0.0:8091-7
http-/0.0.0.0:8091-17
http-/0.0.0.0:8091-15

or if you want to do it in one pass

perl -nle 'if(/\((.+)\)\|\d/){ print $1 unless $seen{$1}++ }' junaid.file