output problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting output problem
# 1  
Old 05-05-2010
output problem

Code:
#!/bin/gawk -f
BEGIN{}
            
{if ($0~/<a/ || $0~/<img/)
    link=link+1
}
{if ($0~/<a/ || $0~/<img/)
    cnt[$i]++

}
{if ($0~/<a/ && $0~/http/ )
    weblink[$i]++
}
{if ($0~/<img/)
    filelink[$i]++
}
{if ($0~/<a/ && $0!~/http/)
    downloadlink[$i]++
}

END{print "File Name and Path = "FILENAME 
{print "Number of links in file = ",link,"\n"}
for (name in cnt) {print name cnt[name]}
print(" ")
print("The links to other websites, images, and download files: ","\n")
for (name in weblink) {print(name)}
print(" ")
for (name in filelink) {print(name)}
print(" ")
for (name in downloadlink) print(name)
}

ok, guys i have this code, that searches for links in a html file, and, i am using an associative array, for recording the appearance frequency of a link.
but im having difficulties printing the array.
the output is:

3br/> <a href="http://www.facebook.com">Facebook. </a>

as you can see the number is at the begging and not at the end onf the line.

i want the output to be like this:

<br/> <a href="http://www.facebook.com">Facebook. </a> 3
# 2  
Old 05-05-2010
Post an example of the input file and the desired output.
# 3  
Old 05-05-2010
input file:

Code:
<html>
<body>

<h1>Welcome to Page1 Website</h1>
<p1>Hello and welcome to Page1 Website</p1>

<br/> <a href="http://www.youtube.com">Youtube. </a>
<br/> <a href="http://www.facebook.com">Facebook. </a>
<br/> <a href="http://www.facebook.com">Facebook. </a>
<br/> <a href="http://www.facebook.com">Facebook. </a>
<br/> <a href="http://www.youtube.com">Youtube. </a>

<br/> <img src="25061.jpg" width="100" height="100" />
<br/> <img src="25064.jpg" width="200" height="200" />

<br/> <a href="/sample.pdf">pdf document1 </a>
<br/> <a href="/booklet09.pdf">pdf document2 </a>

</body>
</html>

desired output:

Code:
<br/> <a href="http://www.facebook.com">Facebook. </a> 3
<br/> <a href="http://www.youtube.com">Youtube. </a> 2
<br/> <img src="25061.jpg" width="100" height="100" /> 1
<br/> <img src="25064.jpg" width="200" height="200" /> 1
<br/> <a href="/sample.pdf">pdf document1 </a> 1
<br/> <a href="/booklet09.pdf">pdf document2 </a> 1



---------- Post updated at 09:59 AM ---------- Previous update was at 09:54 AM ----------

yeah.. sorry for not using tag codes :S

Last edited by Franklin52; 05-05-2010 at 11:58 AM.. Reason: Please use code tags!
# 4  
Old 05-05-2010
Try and adapt this AWK program :
Code:
#!/bin/gawk -f
{
   if ($0 !~ /<a|<img/) next;
   link++;
   cnt[$0]++;

   if ($0  ~ /<a/) {
      if ($0 ~ /http/)
         weblink[$0]++
      else
         downloadlink[$0]++
   } else
      filelink[$0]++;
}

END {
   print "File Name and Path =",FILENAME;
   print "Number of links in file = ",link,"\n";
   for (name in cnt) print name, cnt[name];
   print("\nThe links to other websites:\n")
   for (name in weblink) print name;
   print("\nThe links to images:\n")
   for (name in filelink) print name;
   print("\nThe links to download files:\n")
   for (name in downloadlink) print name;
}

Output:
Code:
File Name and Path = pantelis.txt
Number of links in file =  9 

<br/> <img src="25061.jpg" width="100" height="100" /> 1
<br/> <img src="25064.jpg" width="200" height="200" /> 1
<br/> <a href="http://www.facebook.com">Facebook. </a> 3
<br/> <a href="/booklet09.pdf">pdf document2 </a> 1
<br/> <a href="http://www.youtube.com">Youtube. </a> 2
<br/> <a href="/sample.pdf">pdf document1 </a> 1

The links to other websites:

<br/> <a href="http://www.facebook.com">Facebook. </a>
<br/> <a href="http://www.youtube.com">Youtube. </a>

The links to images:

<br/> <img src="25061.jpg" width="100" height="100" />
<br/> <img src="25064.jpg" width="200" height="200" />

The links to download files:

<br/> <a href="/booklet09.pdf">pdf document2 </a>
<br/> <a href="/sample.pdf">pdf document1 </a>

Jean-Pierre.
# 5  
Old 05-05-2010
Something like this:

Code:
 
awk '$0 ~ /<a / || $0 ~ /<img / {a[$0]++} END{for(i in a) print i , a[i]}' input_file

# 6  
Old 05-05-2010
Quote:
Originally Posted by aigles
Try and adapt this AWK program :
Code:
#!/bin/gawk -f
{
   if ($0 !~ /<a|<img/) next;
   link++;
   cnt[$0]++;

   if ($0  ~ /<a/) {
      if ($0 ~ /http/)
         weblink[$0]++
      else
         downloadlink[$0]++
   } else
      filelink[$0]++;
}

END {
   print "File Name and Path =",FILENAME;
   print "Number of links in file = ",link,"\n";
   for (name in cnt) print name, cnt[name];
   print("\nThe links to other websites:\n")
   for (name in weblink) print name;
   print("\nThe links to images:\n")
   for (name in filelink) print name;
   print("\nThe links to download files:\n")
   for (name in downloadlink) print name;
}

Output:
Code:
File Name and Path = pantelis.txt
Number of links in file =  9 

<br/> <img src="25061.jpg" width="100" height="100" /> 1
<br/> <img src="25064.jpg" width="200" height="200" /> 1
<br/> <a href="http://www.facebook.com">Facebook. </a> 3
<br/> <a href="/booklet09.pdf">pdf document2 </a> 1
<br/> <a href="http://www.youtube.com">Youtube. </a> 2
<br/> <a href="/sample.pdf">pdf document1 </a> 1

The links to other websites:

<br/> <a href="http://www.facebook.com">Facebook. </a>
<br/> <a href="http://www.youtube.com">Youtube. </a>

The links to images:

<br/> <img src="25061.jpg" width="100" height="100" />
<br/> <img src="25064.jpg" width="200" height="200" />

The links to download files:

<br/> <a href="/booklet09.pdf">pdf document2 </a>
<br/> <a href="/sample.pdf">pdf document1 </a>

Jean-Pierre.

thx, but it seems that i get the first output, that is the

3r/> <a href="http://www.facebook.com">Facebook. </a>

i can't make the number appear at the end of the line.

like this

<br/> <a href="http://www.facebook.com">Facebook. </a> 3

---------- Post updated at 10:18 AM ---------- Previous update was at 10:17 AM ----------

Quote:
Originally Posted by panyam
Something like this:

Code:
 
awk '$0 ~ /<a / || $0 ~ /<img / {a[$0]++} END{for(i in a) print i , a[i]}' input_file

same thing... i get the wrong output... Smilie
# 7  
Old 05-05-2010
wondering , how that can be ..as i teseted and getting the proper output.

Try something like this:

Code:
 
awk '$0 ~ /<a / || $0 ~ /<img / {a[$0]++} END{for(i in a) printf("%s %s\n",i,a[i])}' input_file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Output formatting problem

Hello; I have a simple loop filtering a log: for LU in $(< LU-list-Final) do OUT=$(grep -B1 $LU cibc-src-ip.cap |egrep 'IP 16|IP 19|IP 15' |awk -F">" '{print $1}') if ; then echo " LU $LU was accessed by ===============> $OUT " echo "" fi done The current output snippet looks like... (2 Replies)
Discussion started by: delphys
2 Replies

2. Shell Programming and Scripting

Problem getting the required output

I need a Korn shell script which does the folllowing:- If there is one "|" (pipe) delimited file so, the script should check the 5th field to be blank or not. if it is a blank tht entire line of the file should be redirected to another file. if the 5th field is not blank it should pass that. ... (4 Replies)
Discussion started by: Juhi Kashyap
4 Replies

3. UNIX for Dummies Questions & Answers

Grep output problem

Hi all, Here is my problem: I'm testing a login script for our mac Machines. This script output's in a csv file. The login script looks like this: echo "${computername}|${data1}|${data2}|${data3}" >> file.csv The csv file looks like this: Computername1|data1|data2|data3... (4 Replies)
Discussion started by: mattiasvdm
4 Replies

4. UNIX for Dummies Questions & Answers

Problem adjusting the output

Hi i m running a command watch -n 1 -d netstat -i to see the packet drops every 1 second. but the problem is the output is so long(Due to large number of virtual interfaces) it doesn't fit into the putty prompt. I dont need to monitor each and every network interface I m more interested in... (5 Replies)
Discussion started by: pinga123
5 Replies

5. Homework & Coursework Questions

problem with output.

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: something is wrong with my output 2. Relevant commands, code, scripts, algorithms: it has something to do... (1 Reply)
Discussion started by: brooksie91
1 Replies

6. Programming

Problem with output

Hey guys, i am having problem with displaying my text. my text file is displayed in such a way and is called test....... Sam Worthington ... Jake SullyasZoe Saldana ... NeytiriasSigourney Weaver ... Dr. Grace AugustineasStephen Lang ... Colonel Miles QuaritchasJoel Moore ... Norm Spellman (as... (1 Reply)
Discussion started by: gregarion
1 Replies

7. Shell Programming and Scripting

output formatting problem

I would like to keep the complete lines in the output, but my script adds carriage returns for each space (e.g. keep BRITISH AIRWAYS on one line in the output): File1= BAW BRITISH AIRWAYS RYR RYAN AIR for i in $(cat File1) do echo $i done Output: BAW BRITISH AIRWAYS RYR... (4 Replies)
Discussion started by: barny
4 Replies

8. Shell Programming and Scripting

Problem in output

When i use the following command to get only the ip address from a file then i get a whole sentence and not only the ip address which i want. here is the script i am using: grep '^\{1,3\}\.\{1,3\}\.\{1,3\}\.\{1,3\}$' for example if this the file content: Many blogs provide commentary or... (2 Replies)
Discussion started by: snake450
2 Replies

9. UNIX for Dummies Questions & Answers

Output Problem

What do I have to do to get the startup and shutdown Info to display on my Graphical Monitor. As of right now, all i get is the log in screen for X windows on HP-UX. I want to see the boot process (3 Replies)
Discussion started by: Eric_Bakken
3 Replies

10. UNIX for Advanced & Expert Users

problem with who -u output

I am using sco unix ver 5. who -u output lists the user name, tty,idle time and PID of the process. When I kill the idle process using kill -9 PID, the process is killed and the who -u output doesn't contain the user which is killed. This out of who -u is correct and desirable. But for past week, I... (0 Replies)
Discussion started by: V.V.KUMAR
0 Replies
Login or Register to Ask a Question