AWK extraction


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK extraction
# 1  
Old 08-16-2007
AWK extraction

Hi all,
Can anyone please help me in parsing the following file. Suppose the file is called, example.lst, and has the following content in it.

(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS =
(PROTOCOL = TCP)
(Host = 192.168.2.2)
(Port = 1525)
)
)
(CONNECT_DATA = (SID = TESTDB1)
)
)


(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS =
(PROTOCOL = TCP)
(Host = 192.168.2.4)
(Port = 1525)
)
)
(CONNECT_DATA = (SID = TESTDB2)
)
)


I want to extract only the value of all Host (i.e, 192.168.2.4, 192.168.2.2) and value of all SID (i.e., TESTDB1, TESTDB2) from this file. Depending on the number of HOST and SID present in the file, i want to assign them to arrays, like

host[1]=192.168.2.2
host[2]=192.168.2.4
sid[1]=TESTDB1
sid[2]=TESTDB2 and so on....


I am trying this script in Korn shell. I am working with AWK.

Any help would be greatly appreciated.

Thanks,
Harris
# 2  
Old 08-16-2007
check the following one

Code:
awk '{ split($0, arr, "=" ); if ( match($0, "Host") ) { host[i++]=substr(arr[2], 2, length(arr[2]) - 2) } else if ( match($0, "SID") ) { db[j++]=substr(arr[3], 2, length(arr[3]) - 2); } }' filename


to display the values in array and test

Code:
awk '{ split($0, arr, "=" ); if ( match($0, "Host") ) { host[i++]=substr(arr[2], 2, length(arr[2]) - 2) } else if ( match($0, "SID") ) { db[j++]=substr(arr[3], 2, length(arr[3]) - 2); } }END{ for ( x=0; x<i; x++ ) { print host[x] } for ( x=0; x<j; x++ ) { print db[x] } }' filename

# 3  
Old 08-16-2007
nawk -f harris.awk example.lst

harris.awk:
Code:
BEGIN {
  RS=""
  FS="[() =]"
}
{
  for(i=1; i<=NF; i++) {
    if($i == "Host")
      host[FNR]=$(i+3)
    if($i == "SID")
      sid[FNR]=$(i+3)
  }
}
END {
  for(i in host)
    printf("host->[%s] sid->[%s]\n", host[i], sid[i])
}

# 4  
Old 08-17-2007
awk

Hi,
Hope this can help you.

Code:
awk 'BEGIN{
j=1
l=1
RS=""
FS="\n"
}
{
for (i=1;i<=NF;i++)
{
  if($i ~ /Host/)
  {
	host[j]=substr($i,8,length($i)-8)
  	j=j+1
  }
  if ($i ~ /SID/)
  {
  	sid[l]=substr($i,23,length($i)-23)
        l=l+1
  }
}
}
END{
for (k in host)
{
print host[k]
print sid[k]
}
}' a

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk for fits extraction in gnuplot

fitted parameters initialized with current variable values (0 Replies)
Discussion started by: kayak
0 Replies

2. Shell Programming and Scripting

awk - horizontal and vertical text extraction

Hi, I need some help in getting extracting the specific horizontal and vertical texts in a single line. I am trying to extract few of the parameters from a config file. Your help is appreciated. Desired Output ---------------- Pool members members ... (4 Replies)
Discussion started by: pratheeshp
4 Replies

3. UNIX for Dummies Questions & Answers

awk/sed match and extraction

Hi, I have a file like this- aa 12 23 34 aa 21 34 56 aa 78 45 56 I want to print out only the lines after the last aa. How do I do this? I tried using grep -A and sed -n, but both didnt work as I wanted to. Could someone help me out please.. (3 Replies)
Discussion started by: jamie_123
3 Replies

4. Shell Programming and Scripting

extraction

I have following input @xxxxxx@ I want to extract what's between @....@ that is : xxxx using SED command (6 Replies)
Discussion started by: xerox
6 Replies

5. Shell Programming and Scripting

Date and time range extraction via Awk or analysis script?

Hello does anyone know of an awk that will extract log file entries between a specific date and time range, eg: awk '/15\/Dec\/2010:16:10:00/, /15\/Dec\/2010:16:15:00/' access_log but one that works? Or a free command line log file analysis tool/script? I'd like to be able to view... (2 Replies)
Discussion started by: competitions
2 Replies

6. Shell Programming and Scripting

File extraction without awk

Hello everybody, Here is my problem : I cannot find a way to extract data from a particular file and more precisely I cannot extract the result of my awk script to an external file because I am currently working on HP-UX. I would like a simple script (without awk) which asks for a date like... (4 Replies)
Discussion started by: freyr
4 Replies

7. Shell Programming and Scripting

awk line extraction question

Hi, I am trying to extract the first and last lines for each unique item in column 2 of a large text file and then concatenate all extracted lines together in a new text file. So ... I want to go from this format: NEW 0088-BPM 1.042700e+04 877168.19 9718360.00 1496.00 NEW 0088-BPM... (5 Replies)
Discussion started by: jazthedribbler
5 Replies

8. Shell Programming and Scripting

Extraction of text using sed or awk command

Hi All, I need to extract 543 from the command below : # pvscan PV /dev/sdb1 VG vg0 lvm2 Total: 1 543.88 GB] / in use: 1 / in no VG: 0 I have the following command which does the job, but I think this could be achieved in a more simple way using sed or awk. Any help is... (7 Replies)
Discussion started by: nua7
7 Replies

9. Shell Programming and Scripting

AWK extraction

Hi all, I have a data file from which i would like to extract only certain fields, which are not adjacent to each other. Following is the format of data file (data.txt) that i have, which has about 6 fields delimited by "|" HARRIS|23|IT|PROGRAMMER|CHICAGO|EMP JOHN|35|IT|JAVA|NY|CON... (2 Replies)
Discussion started by: harris2107
2 Replies

10. Shell Programming and Scripting

Sed/awk gods, I need your Help! Fancy log extraction

Hi! I'm trying to find a way to extract a certain amount of lines from a log file. This would allow me to "follow" a web user through our log files. Here is a sample fake log file to explain what i want to accomplish : BEGIN REQUEST sessionID=123456 boatload of lines for thread-1 detailing... (8 Replies)
Discussion started by: gnagus
8 Replies
Login or Register to Ask a Question