how to get the number between two signs


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to get the number between two signs
# 1  
Old 10-12-2006
how to get the number between two signs

Hi,

I have the following problem. I want to extract only the numbers from a column with output like this one:

*:bootpc
*:ssh
192.168.0.90:42904->192.168.0.3:jabber-client
192.168.0.90:47380->192.168.0.4:3389

I looked through some of the sed commands but couldn't figure it out. The tricky part, at least for me, is to keep the *:bootpc, *:ssh, etc. and to leave only the port numbers from the rest of the stuff. Can anyone tell me how to accomplish this?
# 2  
Old 10-12-2006
Try whether this helps

$ egrep "bootpc|ssh" fname;egrep -v "bootpc|ssh" fname | nawk -F":|-" '{ print $2}'
*:bootpc
*:ssh
42904
47380

Last edited by mahendramahendr; 10-12-2006 at 05:21 AM..
# 3  
Old 10-12-2006
There is a dirty method

Code:
while read line; do
$number = `cut -d: -f2  $line | cut -d- -f1`
echo $number
loop << fname

# 4  
Old 10-12-2006
Try...
Code:
sed 's/.*:\([0-9]*\)->.*/\1/' file1

# 5  
Old 10-12-2006
Ygor and mahendramahendr your suggestions work fine. I'm trying to implement your one Ygor but I'm printing two colums at the same time and it strips off the first one entirely. This is the ouput and it should be with the Process name before it:

Code:
Process  name  dhcpcd  on  port  *:bootpc       Number  of  files  opened  12
Process  name  sshd    on  port  *:ssh  Number  of  files  opened  23
42904   Number  of  files  opened  56
47380   Number  of  files  opened  21

Is there a way around it? Here is my source:
Code:
output()
{
   lsof -w -n -i | uniq -f 7
}

output | awk 'NR>1{print "Process name\t" $1 "\ton port\t" $8}' | sed 's/.*:\([0-9]*\)->.*/\1/' | column -t > file1.list
output | awk 'NR>1{print $2}' | while read pid; do
lsof -p $pid | wc -l | awk '{print "Number of files opened\t" $1}' | column -t 
done > file2.list

paste file1.list file2.list

# 6  
Old 10-12-2006
Quote:
Originally Posted by sanchopansa
Code:
output()
{
   lsof -w -n -i | uniq -f 7
}

output | awk 'NR>1{print "Process name\t" $1 "\ton port\t" $8}' | sed 's/.*:\([0-9]*\)->.*/\1/' | column -t > file1.list
output | awk 'NR>1{print $2}' | while read pid; do
lsof -p $pid | wc -l | awk '{print "Number of files opened\t" $1}' | column -t 
done > file2.list

paste file1.list file2.list


change highlighted line to:

awk 'NR>1{split($8,arr,"->");print "Process name\t" $1 "\ton port\t" arr[1]}' | column -t > file1.list
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Mail command problem with " signs

Hello, I have compiled a perl script to extract email address from an email file and tried to send email as bash script but " signs are ommitted by the bash script. What am I doing wrong? I would appreciate if you could fix it. #!/bin/bash while read -r COL1 do echo "mail -s "This is a... (3 Replies)
Discussion started by: baris35
3 Replies

2. Shell Programming and Scripting

awk to find number in a field then print the line and the number

Hi I want to use awk to match where field 3 contains a number within string - then print the line and just the number as a new field. The source file is pipe delimited and looks something like 1|net|ABC Letr1|1530||| 1|net|EXP_1040 ABC|1121||| 1|net|EXP_TG1224|1122||| 1|net|R_North|1123|||... (5 Replies)
Discussion started by: Mudshark
5 Replies

3. Shell Programming and Scripting

Help with sort word followed by exponential number and numeric number at the same time

Input file: ID_34 2E-69 2324 ID_1 0E0 3254 ID_1 0E0 5434 ID_5 0E0 436 ID_1 1E-14 2524 ID_1 5E-52 46437 ID_3 65E-20 45467 ID_1 0E0 6578 ... Desired output file: ID_1 0E0 6578 ID_1 0E0 5434 ID_1 0E0 3254 ID_1 5E-52 46437 ID_1 1E-14 2524 ID_3 65E-20 45467 (5 Replies)
Discussion started by: cpp_beginner
5 Replies

4. Shell Programming and Scripting

The difference between end number in the early row and the start number in the next

Hi Power User, I'm trying to compute this kind of text file format: file1: jakarta 100 150 jakarta 170 210 beijing 220 250 beijing 260 280 beijing 290 320 new_york 330 350 new_york 370 420 tokyo 430 470 tokyo 480 ... (2 Replies)
Discussion started by: anjas
2 Replies

5. Shell Programming and Scripting

AWK print number of records, divide this number

I would like to print the number of records of 2 files, and divide the two numbers awk '{print NR}' file1 > output1 awk '{print NR}' file2 > output2 paste output1 output2 > output awl '{print $1/$2}' output > output_2 is there a faster way? (8 Replies)
Discussion started by: programmerc
8 Replies

6. Shell Programming and Scripting

changing number in bash (number is in form of string)

I have a txt file as database. when i run my program what it does is it ask me for 3 name and stored in the file as name1:name2:name3:1 when u enter 3 name it add those in file as above format and add 1 at the end. I what i want is if i enter same names again it changes that 1 to 2 and so... (3 Replies)
Discussion started by: Learnerabc
3 Replies

7. Shell Programming and Scripting

how to add the number of row and count number of rows

Hi experts a have a very large file and I need to add two columns: the first one numbering the incidence of records and the another with the total count The input file: 21 2341 A 21 2341 A 21 2341 A 21 2341 C 21 2341 C 21 2341 C 21 2341 C 21 4567 A 21 4567 A 21 4567 C ... (6 Replies)
Discussion started by: juelillo
6 Replies

8. Solaris

Red stop signs in Solaris Management Console 2.1

I've installed Solaris 10 (x86 8/07) on a Dell PowerEdge 2950. When I bring up the Solaris Management Console I get red stop sign looking icons on all areas (System Status, System Configuration, Services, Storage, and Devices and hardware) under This computer. Originally nothing was showing up... (1 Reply)
Discussion started by: kayroreality
1 Replies
Login or Register to Ask a Question