shell script to print particular column into text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shell script to print particular column into text file
# 1  
Old 11-13-2010
shell script to print particular column into text file

hi everyone,
i need a script which would just print column into a text file
Code:
nmap 10.226.112.222
PORT STATE SERVICE 
7/tcp open echo 
13/tcp open daytime 
22/tcp open ssh 
23/tcp open telnet 
37/tcp open time 
1100/tcp open unknown

above nmap command gives us open ports on that IP. can i get a script which would just print PORT column into one text file or just SERVICE column into a text file.

Last edited by Scott; 11-13-2010 at 01:43 PM.. Reason: Please use code tags
# 2  
Old 11-13-2010
Code:
nmap 10.226.112.222 | cut -d " " -f1 >file.txt

If you want service column to be printed you can use -f3 (3rd field)

Last edited by Scott; 11-13-2010 at 03:19 PM.. Reason: Code tags
# 3  
Old 11-13-2010
Code:
c=1   # print first column
nmap | awk -v c=$c '{ print $c}'

# 4  
Old 11-14-2010
thx guys for ur help

---------- Post updated at 11:14 PM ---------- Previous update was at 10:57 PM ----------

now i have a text file i.e file1.txt which shows open ports on particular system. i have another text file i.e file2.txt which shows a list of allowed ports on a system. for eg:
file2.txt
22/tcp ssh
23/tcp telnet.
can i have a script which would compare these text files ,file1 and file2 and close out all ports which r not allowed. i know only one way off turning off particular port or sevice i.e by commenting out particular service in etc/inetd.conf file on linux system.
# 5  
Old 11-14-2010
You can produce the list of ports that should not be open like this:
Code:
nmap 10.226.112.222 | awk 'NR==FNR{P[$1]=$0;next}!P[$1]' file2.txt -

With you samples this will produce the following output:
Code:
PORT STATE SERVICE
7/tcp open echo
13/tcp open daytime
37/tcp open time
1100/tcp open unknown

If you want to then shut down those services I would do that manually or in controlled batch runs, but not automatically by a script.. Where exactly stop/start of services is controlled depends on your platform. Often there is some form of tooling like chkconfig to make this easier..
After you shut down the service you can then further tighten the system by adjusting firewall rules and/or tcp-wrappers, etc..
# 6  
Old 11-14-2010
Code:
for i in `comm -23 1.txt 2.txt`
do 
  iptables -A INPUT --dport $i -j DROP
  iptables -A OUTPUT --dport $i -j DROP
done


Last edited by Franklin52; 11-14-2010 at 10:37 AM.. Reason: Please indent your code and use code tags, thank you
# 7  
Old 11-15-2010
closing open ports using scripts

hi rainboisterous, will u explain ur script a bit
here ,
for i in `comm -23 1.txt 2.txt`
here 1.txt contains all open ports on system by doing nmap
and 2.txt contains all allowed ports .
for e.g 1.txt might contain
7/tcp
13/tcp
22/tcp
23/tcp
37/tcp
and 2.txt contains
22/tcp
23/tcp
so ur script will commpare both text file and drop all ports apart from 23/tcp . please explain
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match text to lines in a file, iterate backwards until text or text substring matches, print to file

hi all, trying this using shell/bash with sed/awk/grep I have two files, one containing one column, the other containing multiple columns (comma delimited). file1.txt abc12345 def12345 ghi54321 ... file2.txt abc1,text1,texta abc,text2,textb def123,text3,textc gh,text4,textd... (6 Replies)
Discussion started by: shogun1970
6 Replies

2. UNIX for Beginners Questions & Answers

Print a python script down a list in a text file without printing a lot combinations

In a python script I have 2 files printing side by side on the same line. I want to have 1 of the files to be already displayed at once while the other file print down the list in the file and it still will produce new lines. I want to do it like that to reduce printing a lot of lines and... (0 Replies)
Discussion started by: bigvito19
0 Replies

3. UNIX for Beginners Questions & Answers

How to insert data into black column( Secound Column ) in excel (.XLSX) file using shell script?

Source Code of the original script is down below please run the script and try to solve this problem this is my data and I want it column wise 2019-03-20 13:00:00:000 2019-03-20 15:00:00:000 1 Operating System LAB 0 1 1 1 1 1 1 1 1 1 0 1 (5 Replies)
Discussion started by: Shubham1182
5 Replies

4. Shell Programming and Scripting

Need awk or Shell script to compare Column-1 of two different CSV files and print if column-1 matche

Example: I have files in below format file 1: zxc,133,joe@example.com cst,222,xyz@example1.com File 2 Contains: hxd hcd jws zxc cst File 1 has 50000 lines and file 2 has around 30000 lines : Expected Output has to be : hxd hcd jws (5 Replies)
Discussion started by: TestPractice
5 Replies

5. Shell Programming and Scripting

Print every alternate column in row in a text file

Hi, I have a comma separated file. I would like to print every alternate columns into a new row. Example input file: Name : John, Age : 30, DOB : 30-Oct-2018 Example output: Name,Age,DOB John,30,30-Oct-2018 (3 Replies)
Discussion started by: Lini
3 Replies

6. UNIX for Dummies Questions & Answers

Reading XML file and print the values in the text file using Linux shell script

hi guys, i want help... Reding XML file and print the values into the text file using linux shell script file as per below xml file <sequence> <Filename>aldorzum.doc</Filename> <DivisionCode>US</DivisionCode> <ContentType>Template</ContentType> <ProductCode>VIMZIM</ProductCode> </sequence>... (1 Reply)
Discussion started by: sravanreddy
1 Replies

7. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

8. Shell Programming and Scripting

comparing column of two different files and print the column from in order of 2nd file

Hi friends, My file is like: Second file is : I need to print the rows present in file one, but in order present in second file....I used while read gh;do awk ' $1=="' $gh'" {print >> FILENAME"output"} ' cat listoffirstfile done < secondfile but the output I am... (14 Replies)
Discussion started by: CAch
14 Replies

9. Shell Programming and Scripting

Print some results in a text file using script in linux

hello everyone, i really need your help to write a script which would just print following kind of result into a text file (result.txt) XYZ test Results ID: <unique-id> Date: <date> ------------------------------------------------- | Task | Result | Time |... (3 Replies)
Discussion started by: viriimind
3 Replies
Login or Register to Ask a Question