0

I have text file that have multi lines I want to search for lines containing some text and add # at the start of those lines.

ex: file1 that I want to change it

acnet 6801/tcp # ACNET Control System Protocol acnet 6801/udp # ACNET Control System Protocol dlip 7201/tcp # DLIP dlip 7201/udp # DLIP ssp-client 7801/tcp # Secure Server Protocol - client ssp-client 7801/udp # Secure Server Protocol - client 

I want to find ports located in inputfile1. input file one contains:

6801 7801 

so the final output of file1 will be like this:

#acnet 6801/tcp # ACNET Control System Protocol #acnet 6801/udp # ACNET Control System Protocol dlip 7201/tcp # DLIP dlip 7201/udp # DLIP #ssp-client 7801/tcp # Secure Server Protocol - client #ssp-client 7801/udp # Secure Server Protocol - client 

I tried

cat /etc/services |grep -f ports.txt | awk '{ print"#" $g}'; 

but this give me the output to screen. How to change them in the file?

3 Answers 3

0

Try something like:

awk -F"[ \t]*|/" 'NR==FNR{A[$1]; next} $2 in A { printf "#" }1' ports.txt /etc/services > newfile 

If the output is correct, you can copy it to /etc/services

0
sed '/6801\|7801/ {s/^/#/}' input #acnet 6801/tcp # ACNET Control System Protocol #acnet 6801/udp # ACNET Control System Protocol dlip 7201/tcp # DLIP dlip 7201/udp # DLIP #ssp-client 7801/tcp # Secure Server Protocol - client #ssp-client 7801/udp # Secure Server Protocol - client 
2
  • thank you for your reply ..its not only just these two ports there maybe 20 i just give an example Commented Apr 9, 2016 at 7:57
  • you could wrap the whole sed script in a loop which reads the port one at a time and does the commenting for that port - but it would do one port at a time. alternatively, add all the ports in the above script. Commented Apr 9, 2016 at 9:52
0
REGEXP=$(cat ports.txt | xargs | sed -e 's/ /|/g') sed -E -i -e "/[[:space:]]$REGEXP\// s/^/#/" inputfile1 

This constructs an extended regular expression from the contents of ports.txt, and then uses them to select particular addresses (lines) in inputfile1 to have a # character pre-pended.

The regex is "anchored" with a space at the beginning and a / at the end so that you don't match more than you specify - e.g. without the anchors, a 25 in ports.txt would not only match port 25, it would match ANY port containing the number 25 somewhere in it, including 2501, 1025, 250, and many more.

BTW, if you want to be able to have comments in the ports.txt, use this:

REGEXP=$(sed -e 's/#.*//' ports.txt | xargs | sed -e 's/ /|/g') sed -E -i -e "/[[:space:]]$REGEXP\// s/^/#/" inputfile1 

The first version above will ONLY work correctly if ports.txt contains nothing but port numbers, spaces and empty lines - any extraneous characters (especially those that have special meaning in regexps) will cause unpredictable and almost-certainly undesirable results.

The second version deletes comments (i.e. everything after, and including, any # character on a line) so also allows (ignores) any character in a comment.

It will also fail if the ports listed in ports.txt causes the sed command to exceed the maximum command line length. As this is typically around 128KB, this is very unlikely to be a problem in this case, but it's worth noting in case you use this technique for other tasks.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.