sed - go to file and search a part and set at the end text


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed - go to file and search a part and set at the end text
# 1  
Old 09-02-2016
sed - go to file and search a part and set at the end text

Hello,

i hope the titel is okay.

I want to edit a line in
Code:
/etc/arno-iptables-firewall/firewall.conf

So the Line can contains:
Code:
OPEN_TCP="436, 25, 80, 110, 143, 443, 465, 587, 993, 995, 21, 20"

i want to search in this line:
Code:
OPEN_TCP="*, *, 80, *, *, 443, *, *, *, *"

and i want to insert at the end of this line
Code:
OPEN_TCP="*, *, 80, *, *, 443, *, *, *, *,$FTP_PORT"


anyone a idea to help me?
# 2  
Old 09-02-2016
So, there MUST be 8 stars in the line, plus the 80, plus the 443?
# 3  
Old 09-02-2016
No, the stars (*) are unknown content.

My line looks so:
Code:
OPEN_TCP="436, 25, 80, 110, 143, 443, 465, 587, 993, 995"


So the constant is
Code:
OPEN_TCP="

Is it possible to search in this file for:
Code:
OPEN_TCP="

and add some contant so:
Code:
OPEN_TCP="$FTP_PORT,

that the line after this looks so:
Code:
OPEN_TCP="21, 436, 25, 80, 110, 143, 443, 465, 587, 993, 995"

# 4  
Old 09-02-2016
This is NOT at line end as requested in post#1 ?! Try
Code:
sed "/OPEN_TCP=\"/ s//&$FTP_PORT, /" /etc/arno-iptables-firewall/firewall.conf

This User Gave Thanks to RudiC For This Post:
# 5  
Old 09-02-2016
Yes, because I noticed while writing that it is so simple



Thank you for helping me, i use this:
Code:
 sed -i "s/OPEN_TCP=\"/OPEN_TCP=\"$FTP_PORT,/g" /root/firewall.conf

but i like your code much more


but one Problem:
is it possible to say di this:
Code:
 sed -i "s/OPEN_TCP=\"/OPEN_TCP=\"$FTP_PORT,/g" /root/firewall.conf

,

Only for the last match? or "exact this string"

So i habe in my firewallconf Strings like this:
Code:
LAN_HOST_OPEN_TCP=""
LAN_INET_OPEN_TCP=""
LAN_INET_HOST_OPEN_TCP=""

After i use my bash script the lines look so:
Code:
LAN_HOST_OPEN_TCP="22"
LAN_INET_OPEN_TCP="22"
LAN_INET_HOST_OPEN_TCP="22"

Thats not what i want.

"my" OPEN_TCP is the last OPEN_TCP in the Document:
Code:
HOST_OPEN_TCP=""
HOST_OPEN_UDP=""
HOST_OPEN_IP=""
HOST_OPEN_ICMP=""

# Put in the following variables which hosts you want to DENY(DROP) for certain
# services (and logged).
# to DENY(DROP) for certain hosts.
# TCP/UDP port format (HOST_DENY_TCP & HOST_DENY_UDP):
#       "host1,host2~port1,port2 host3,host4~port3,port4 ..."
#
# IP protocol format (HOST_DENY_IP):
#       "host1,host2~proto1,proto2 host3,host4~proto4,proto4 ..."
#
# ICMP protocol format (HOST_DENY_ICMP):
#       "host1 host2 ...."
# -----------------------------------------------------------------------------
HOST_DENY_TCP=""
HOST_DENY_UDP=""
HOST_DENY_IP=""
HOST_DENY_ICMP=""

# Put in the following variables which hosts you want to DENY(DROP) for certain
# services but NOT logged.
# TCP/UDP port format (HOST_DENY_xxx_NOLOG):
#       "host1,host2~port1,port2 host3,host4~port3,port4 ..."
#
# IP protocol format (HOST_DENY_IP_NOLOG):
#       "host1,host2~proto1,proto2 host3,host4~proto4,proto4 ..."
#
# ICMP protocol format (HOST_DENY_ICMP_NOLOG):
#       "host1 host2 ...."
# -----------------------------------------------------------------------------
HOST_DENY_TCP_NOLOG=""
HOST_DENY_UDP_NOLOG=""
HOST_DENY_IP_NOLOG=""
HOST_DENY_ICMP_NOLOG=""

# Put in the following variables which hosts you want to REJECT (instead of
# DROP) for certain TCP/UDP ports.
# TCP/UDP port format (HOST_REJECT_xxx):
#       "host1,host2~port1,port2 host3,host4~port3,port4 ..."
# -----------------------------------------------------------------------------
HOST_REJECT_TCP=""
HOST_REJECT_UDP=""

# Put in the following variables which hosts you want to REJECT (instead of
# DROP) for certain services but NOT logged.
# TCP/UDP port format (HOST_REJECT_xxx_NOLOG):
#       "host1,host2~port1,port2 host3,host4~port3,port4 ..."
# -----------------------------------------------------------------------------
HOST_REJECT_TCP_NOLOG=""
HOST_REJECT_UDP_NOLOG=""

# Put in the following variables which services THIS machine is NOT
# permitted to connect TO (remote end-point) via the external (internet)
# interface. For example for blocking IRC (tcp 6666:6669).
# -----------------------------------------------------------------------------
DENY_TCP_OUTPUT=""
DENY_UDP_OUTPUT=""
DENY_IP_OUTPUT=""

# Put in the following variables to which hosts THIS machine is NOT
# permitted to connect TO for certain services (remote end-point)
# via the external (internet) interface. In principle you can also
# use this to put your machine in a "virtual-DMZ" by blocking all traffic
# to your local subnet.
# TCP/UDP port format (HOST_DENY_TCP_OUTPUT & HOST_DENY_UDP_OUTPUT):
#       "host1,host2~port1,port2 host3,host4~port3,port4 ..."
#
# IP protocol format (HOST_DENY_IP_OUTPUT):
#       "host1,host2~proto1,proto2 host3,host4~proto4,proto4 ..."
# -----------------------------------------------------------------------------
HOST_DENY_TCP_OUTPUT=""
HOST_DENY_UDP_OUTPUT=""
HOST_DENY_IP_OUTPUT=""

# Enable (1) to make the default policy allow for IPv4 ICMP (ping) for INET access
# Note: Other ICMP variables apply to both IPv4 and IPv6 unless otherwise noted.
# -----------------------------------------------------------------------------
OPEN_ICMP="1"

# Disable (0) to make the default policy drop IPv6 ICMPv6 for INET access
# Note: Other ICMP variables apply to both IPv4 and IPv6 unless otherwise noted.
# -----------------------------------------------------------------------------
OPEN_ICMPV6=1

# Enable (1) to make the default policy allow IPv6 ICMPv6
# Multicast Listener Discovery (RFC 2710, 3810) for INET access
# Note: Requires setting OPEN_ICMPV6=1 to apply.
# -----------------------------------------------------------------------------
OPEN_ICMPV6_MLD=0

# Put in the following variables which ports or IP protocols you want to leave
# open to the whole world.
# -----------------------------------------------------------------------------
OPEN_TCP="22,$FTP_PORT,436, 25, 80, 110, 143, 443, 465, 587, 993, 995, 21, 20"
OPEN_UDP=""
OPEN_IP=""

# Put in the following variables the TCP/UDP ports you want to DENY(DROP) for
# everyone (and logged). Also use these variables if you want to log connection
# attempts to these ports from everyone (also trusted/full access hosts).
# In principle you don't need these variables, as everything is already blocked
# (denied) by default, but just exists for consistency.
# -----------------------------------------------------------------------------
DENY_TCP=""
DENY_UDP=""

# Put in the following variables which ports you want to DENY(DROP) for
# everyone but NOT logged. This is very useful if you have constant probes on
# the same port(s) over and over again (code red worm) and don't want your logs
# flooded with it.
# -----------------------------------------------------------------------------
DENY_TCP_NOLOG=""
DENY_UDP_NOLOG=""

# Put in the following variables the TCP/UDP ports you want to REJECT (instead
# of DROP) for everyone (and logged).
# -----------------------------------------------------------------------------
REJECT_TCP=""
REJECT_UDP=""

# Put in the following variables the TCP/UDP ports you want to REJECT (instead
# of DROP) for everyone but NOT logged.
# -----------------------------------------------------------------------------
REJECT_TCP_NOLOG=""
REJECT_UDP_NOLOG=""

# Put in the following variable which hosts you want to block (blackhole,
# dropping every packet from the host).
# -----------------------------------------------------------------------------
BLOCK_HOSTS=""

# Blocked Hosts are by default blocked in both Inbound and Outbound directions.
# If only Inbound blocking is desired, set to 0 to disable bidirectional blocking.
# -----------------------------------------------------------------------------
BLOCK_HOSTS_BIDIRECTIONAL=1

# Uncomment & specify here the location of the file that contains a list of
# hosts(IPs) that should be BLOCKED. IP ranges can (only) be specified as
# w.x.y.z1-z2 (eg. 192.168.1.10-15). Note that the last line of this file
# should always contain a carriage-return (enter)!
# -----------------------------------------------------------------------------
BLOCK_HOSTS_FILE="/etc/arno-iptables-firewall/blocked-hosts"


Last edited by tJAdASwIRDESSEI; 09-02-2016 at 09:57 AM..
# 6  
Old 09-02-2016
Only for the last matching line, this is difficult.
Exact match, this is easy: just put the ^ anchor
Code:
sed -i "s/^OPEN_TCP=\"/&$FTP_PORT,/" /root/firewall.conf

Also changed: & re-inserts the matching portion, g (retry) modifier not needed.
The following would avoid a duplicate insertion
Code:
sed -i "/\<$FTP_PORT\>/ "\!"s/^OPEN_TCP=\"/&$FTP_PORT,/" /root/firewall.conf

# 7  
Old 09-08-2016
he brakes the line after this code:
Code:
BIG_PORTS="111,222,333,444,555,666,777,888,999"
sed -i "/\<$BIG_PORTS\>/ "\!"s/^OPEN_TCP=\"/&$BIG_PORTS,/" /etc/arno-iptables-firewall/firewall.conf

result:
Code:
OPEN_TCP="111,222,333,444,555,666,777,888,999
,436, 25, 80, 110, 143, 443, 465, 587, 993, 995, 21, 20"
OPEN_UDP=""
OPEN_IP=""

why he do that? How i can get this result:
Code:
OPEN_TCP="111,222,333,444,555,666,777,888,999,436, 25, 80, 110, 143, 443, 465, 587, 993, 995, 21, 20"
OPEN_UDP=""
OPEN_IP=""


Last edited by tJAdASwIRDESSEI; 09-08-2016 at 06:07 PM.. Reason: additional code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read in search strings from text file, search for string in second text file and output to CSV

Hi guys, I have a text file named file1.txt that is formatted like this: 001 , ID , 20000 002 , Name , Brandon 003 , Phone_Number , 616-234-1999 004 , SSNumber , 234-23-234 005 , Model , Toyota 007 , Engine ,V8 008 , GPS , OFF and I have file2.txt formatted like this: ... (2 Replies)
Discussion started by: An0mander
2 Replies

2. Shell Programming and Scripting

How to add a line to the end of a set of files without using sed command?

I understand that the SED command reads all the lines in the file before adding a required line to the end of the file. Is there another command that adds a line to the end of files without reading the entire file.... SED is increasing the processing time as the number of lines in each of the... (1 Reply)
Discussion started by: Kanch
1 Replies

3. Shell Programming and Scripting

Search from a line to end of list using sed

Dear Unix Experts :), Below is a small section of a large file with the following list: 1. Starts with string " interest" as the heading 2. Followed by a list of activities 3. Ends with a blank line before starting with another different list. E.g. Sporting interest football... (13 Replies)
Discussion started by: gjackson123
13 Replies

4. Shell Programming and Scripting

Search a string in a text file and add another string at the end of line

Dear All I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB... (5 Replies)
Discussion started by: suryanarayana
5 Replies

5. Shell Programming and Scripting

deleting the part of the file(overwrite) using start and end point

here is the contents of bigfile.sql CREATE TABLE `Table11` ( `id` int(11) NOT NULL , `entityName` enum('Lines','EndUsers') COLLATE utf8_unicode_ci NOT NULL, `parentAllianceMigrationProjectId` varchar(255) NOT NULL ) ENGINE=InnoDB AUTO_INCREMENT=2000 DEFAULT CHARSET=utf8... (30 Replies)
Discussion started by: vivek d r
30 Replies

6. Shell Programming and Scripting

Using sed to put text end of line

how to use sed to put .txt end of line..my input file below file1 make=^bak12^". DEV=LONG^cmd/usr/bak/ade4^" ..................................... file 2 make=^and_LONG/bak12^". DEV=LONG^cmd/usr/bak/ban3^" .......................................... file 3... (6 Replies)
Discussion started by: zulabc
6 Replies

7. Shell Programming and Scripting

search needed part in text file (awk?)

Hello! I have text file: From aaa@bbb Fri Jun 1 10:04:29 2010 --____OSPHWOJQGRPHNTTXKYGR____ Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline My code '234565'. ... (2 Replies)
Discussion started by: candyme
2 Replies

8. Shell Programming and Scripting

Append text from one file to another based on a search from the end of a document

Hi all, I have output files that are all text files with various different extensions. So, if I submit the input file "job_name.inp", when it finishes I get an output file "job_name.dat". A typical input file looks something like this: $CONTRL SCFTYP=RHF RUNTYP=ENERGY MAXIT=199 MULT=1... (4 Replies)
Discussion started by: marcozd
4 Replies

9. UNIX for Dummies Questions & Answers

using sed to append text to the end of each line

Anyone know how to use SED to append a comma to the end of each line example: field1,field2,field3,field4 If i Cat /textfile ---- How can i append the end of /textfile with a comman? (8 Replies)
Discussion started by: Redg
8 Replies

10. Shell Programming and Scripting

How do you search and replace a text with markerA that end with markerB

Hi all, How do you replace all lines that begin with markerA and end with markerB? Ex: I have a file with a list of lines. /sbin/fsck -> $(SEC_CRIT) ; /sbin/fsck.ext2 -> $(SEC_CRIT) ; How can I replace all "->$....;" with a blank space... (1 Reply)
Discussion started by: drone
1 Replies
Login or Register to Ask a Question