GREP Formatting Question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting GREP Formatting Question
# 1  
Old 08-27-2014
GREP Formatting Question

I have the following nmap file output with the multiple IP’s listed in the format below. Is there a way that GREP can format the output to just display IP and any ports that contain 'http' for that IP on its own line?

file:
Code:
Host: 192.168.1.xxx ()	Ports: 80/open/tcp//http///, 139/open/tcp//netbios-ssn///, 515/open/
Host: 192.168.1.xxx ()	Ports: 23/open/tcp//telnet///	Ignored State: closed (99)Host: ....................

desired output
I want the output to look like this:
Code:
192.168.1.xxx Ports: 80/open/tcp//http
192.168.1.xxx ....................


Last edited by Corona688; 08-27-2014 at 10:43 PM..
# 2  
Old 08-27-2014
grep matches lines, it's not a language that can assemble bits and pieces. That's what awk's for.

Code:
awk '{ print $2, $5 }' inputfile

# 3  
Old 08-28-2014
Or
Code:
cut -f2,5- file

# 4  
Old 08-28-2014
You might want to try something like:
Code:
awk -F',*[ \t]+' '
{	printf("%s %s", $2, $4)
	for(i = 5; i <= NF; i++)
		if($i ~ "/http/")
			printf(" %s", $i)
	print ""
}' file

which produces the output:
Code:
192.168.1.xxx Ports: 80/open/tcp//http///
192.168.1.xxx Ports:

with the supplied sample input.

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

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Formatting Question

Good Morning- I've been cloning a Solaris 9 SPARC machine using ufsrestoreand have successfully restored drive 0. Now I'm trying to set up the mirror drive. To label and format the new drive, I used#format -e ->select c0t1d0 ->label ->select SMI ->select no ->select no ->select yes and... (3 Replies)
Discussion started by: Stellaman1977
3 Replies

2. Shell Programming and Scripting

Formatting grep and awk output

Hi there. I have a very large file and I am trying to format it so that I can pull out certain pieces of data/info and report it in spreadsheet format/style. The file has ###### which will separate each line that will be listed in the spreadsheet. Whenever I find "No" at the end of a line I want... (7 Replies)
Discussion started by: kieranfoley
7 Replies

3. Shell Programming and Scripting

Output formatting of grep

hi, I am trying to find the word "root" from the follwoing sample file: #12.12.12.2222 echo "Hai........" 11.11.1.1111 3.23.AS.AA ab.cd.df.rf /usr/bin jhhh 12.12.AF.12 /urf/sss/kk kkk su sh.s exec su root; ,root sudo -sh cosrootex (3 Replies)
Discussion started by: flamingo_l
3 Replies

4. UNIX Desktop Questions & Answers

Grep result loses formatting

I am searching for a string in a file and then redirecting the contents in another file... however the formatting is not preserved.. Can you please help me on this ... (5 Replies)
Discussion started by: blackeyed
5 Replies

5. UNIX for Dummies Questions & Answers

Question about formatting the output

I need to ask a question on how to format the output in a csv format. Right now i am running a shell script which executes a command and the following output is append through a unix script in a .csv file. So the output of xyz.csv is as follow :- 1. Number = 25 Amount $84,132.22 2.... (1 Reply)
Discussion started by: chris1234
1 Replies

6. Shell Programming and Scripting

Text formatting question

How can i change the display of a text file containing 4980 167 187 4980 167 187 4980 167 180 4980 167 180 4980 167 179 4980 272 174 (7 Replies)
Discussion started by: aliaa2a
7 Replies

7. Shell Programming and Scripting

grep sorting/formatting

Hi, I currently have many log files for different webpages inside the folder /log/. The program allows the user to enter a month and then the grep function searches for it, the command i used is below: grep -c "$month" ./log/*.log This works and i can ascertain the number of matches, but I... (10 Replies)
Discussion started by: Jaken
10 Replies

8. UNIX for Dummies Questions & Answers

Question about formatting results

OK, I have a command that is getting a result, that I am trying to format using awk. I think it's pretty ugly, and there is probably a better way to do it, but this is sorta working for me. Anyway, the command: cat /var/log/cups/page_log | grep testuser | grep My_office_printer | awk '{gsub... (5 Replies)
Discussion started by: TheCrunge
5 Replies

9. Shell Programming and Scripting

Multiple Grep Results - Formatting

Hello, Perhaps someone here can help with this. I'd like to grep a plain text file for a word and output each line containing a word found to a seperate line instead of back to back. Examples: Basic command: cat file.txt > grep -i CAT > results.txt file.txt: The cat said meow The... (7 Replies)
Discussion started by: sysera
7 Replies

10. UNIX for Dummies Questions & Answers

disk formatting question

Currently I have a box that I am dual-booting Win98 & Linux on. I have an unformatted 3 gig slice that I would like to install Soloris 8 x86 on. Are there any issues I should be aware of? How close is the x86 install to the sparc install? The Linux partition will be going away but I need to reatain... (1 Reply)
Discussion started by: 98_1LE
1 Replies
Login or Register to Ask a Question