Removing characters and some output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removing characters and some output
# 1  
Old 09-04-2015
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 ::

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

And what I want is :

Code:
http-/0.0.0.0:8091-9

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

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

which is wrong.

What other command can I use ?
# 2  
Old 09-04-2015
Quote:
Originally Posted by Junaid Subhani
What other command can I use ?
Code:
echo "(http-/0.0.0.0:8091-9)|23:00:41" | perl -nle '/\((.+)\)\|\d/ and print $1'

Quote:
So I am using an awk command to return a specific column in a file.
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.

Last edited by Aia; 09-04-2015 at 08:48 PM..
This User Gave Thanks to Aia For This Post:
# 3  
Old 09-04-2015
It would seem that the awk command:
Code:
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.
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 09-04-2015
Thanks guys. Both solutions work perfectly.

Another issue that I have.

I have a file:

Code:
[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.

Code:
[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.
# 5  
Old 09-04-2015
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:
Code:
awk -F'[()]' '{list[$2]} END{for(i in list) print i}' file

or:
Code:
awk -F'[()]' 'list[$2]++ == 0 {print $2}' file


Last edited by Don Cragun; 09-04-2015 at 10:40 PM.. Reason: Add alternative awk script.
# 6  
Old 09-04-2015
Quote:
Note: 'uniq' does not detect repeated lines unless they are adjacent. You may want to sort the input first, or use 'sort -u' without 'uniq'.

Code:
perl -nle 'print unless $seen{$_}++' list

Code:
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
Code:
perl -nle 'if(/\((.+)\)\|\d/){ print $1 unless $seen{$1}++ }' junaid.file


Last edited by Aia; 09-04-2015 at 10:54 PM.. Reason: option
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing all characters up to initial '{'

Hi there, Im having a bit of difficulty with this one and I suspect its because of the character I want to match against maybe causing me a problem, but i wanted to remove everything up to (but not including) the first instance of '{' in a string so for example the string that I want to... (2 Replies)
Discussion started by: hcclnoodles
2 Replies

2. Shell Programming and Scripting

Removing Characters From finger Output

Hi Does anyone know of a command that will extract only the Login name alison and year 2005 from the following output. jamesm> finger alison Login name: alison In real life: Alison Smith - Queensland Directory: /data/home/alison Shell: /usr/bin/ksh... (2 Replies)
Discussion started by: jamba1
2 Replies

3. Shell Programming and Scripting

Removing ^M characters from a file

Hi, I want to removing ^M characters from a file and combine the line with the next line. ex: issue i have: ABC^M^M DEF solution i need: ABCDEF I found that you by using the following command you can remove new line characters. tr -d '\r' < infile.csv > outfile.csv still... (10 Replies)
Discussion started by: mwrg
10 Replies

4. UNIX for Dummies Questions & Answers

Removing ^M characters

hi I have a perl script conv.pl. when i execute this file and direct i to log file I see lots of ^M characters in the log file. There is no ^M in conv.pl file. Log file is generated only after conv.pl is executed. Please help as how to get rid of these. This conv.pl is going to get schduled... (5 Replies)
Discussion started by: infyanurag
5 Replies

5. UNIX for Advanced & Expert Users

Removing ^M characters

hi I have a perl script conv.pl. when i execute this file and direct i to log file I see lots of ^M characters in the log file. There is no ^M in conv.pl file. Log file is generated only after conv.pl is executed. Please help as how to get rid of these. This conv.pl is going to get schduled... (0 Replies)
Discussion started by: infyanurag
0 Replies

6. UNIX for Dummies Questions & Answers

removing characters

Hi all, Any help on how to do the following? :eek: I have an infile as follows: _thisishowyouwritehelloworld _thisisalsohowyouwritehelloworld2 I want to delete the characters from "_" to "how" and be left with: youwritehelloworld youwritehelloworld2 I am able to do delete from a... (2 Replies)
Discussion started by: dr_sabz
2 Replies

7. Shell Programming and Scripting

help removing characters for output file in shell script

hi i'm new to shell scripts and have a small problem i am running a batch converter that returns all flash .flv files in a directory and create a png image from each one the problem i have is the $1 variable , its ok on the first call but on the secound call $1.png , i have extra... (1 Reply)
Discussion started by: wingchun22
1 Replies

8. HP-UX

Removing ^D and ^H characters

Hi, I have a very huge file and it contains some unprintable characters like ^H and ^D. If I try to remove using cat test1.ser| tr -d '\136 110'>newfile1 it is only removing ^and all spaces in the file. How can I remove these characters (^D ^H) and keep my spaces as it is? Thanks &... (1 Reply)
Discussion started by: arsheshadri
1 Replies

9. Shell Programming and Scripting

Removing certain characters in a file

Hi I have a file that has semicolons in it (;) is there a way to just remove these in the file. Example name: Joe Smith; group: Group1; name: Mary White; group: Group2; (2 Replies)
Discussion started by: bombcan
2 Replies

10. Shell Programming and Scripting

Help with removing characters like ^M

I have few files in unix which are in dos format. While I am copying these files, ^M, ^@, etc characters are being generated. I tried dos2unix command in Linux and it doesn't work. I tried sed to remove these characters but they won't go. I came to about this 'tr' command and tried to use it... (16 Replies)
Discussion started by: chiru_h
16 Replies
Login or Register to Ask a Question