Sponsored Content
Top Forums Shell Programming and Scripting search string in a file and retrieve 10 lines including string line Post 302446176 by ranjithpr on Wednesday 18th of August 2010 02:59:41 AM
Old 08-18-2010
Hope this is what you were expecting

Code:
$ cat 1.txt
word1 line1 word2
word1 line2 word2
word1 line3 word2
word1 line4 word2
word1 "line5" word2
word1 line6 word2
word1 line7 word2
word1 "line5" word2
word1 line8 word2
word1 line9 word2
$ awk '/\"line5\"/ {for(i=0;i<4;i++) {print;if(!getline) exit;} }' 1.txt > result.txt
$ cat result.txt
word1 "line5" word2
word1 line6 word2
word1 line7 word2
word1 "line5" word2
$

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Retrieve string from each line in a file based on a pattern in Unix

I have a file which contains several lines. Sample content of the file is as below. OK testmessage email<test@123> NOK receivemessage email<123@test> NOK receivemessage email(123@test123) NOK receivemessage email<abc@test> i would like to know by scripting will... (10 Replies)
Discussion started by: ramasar
10 Replies

2. Shell Programming and Scripting

replace (sed?) a single line/string in file with multiple lines (string) from another file??

Can someone tell me how I can do this? e.g: Say file1.txt contains: today is monday the 22 of NOVEMBER 2010 and file2.txt contains: the 11th month of How do i replace the word NOVEMBER with (5 Replies)
Discussion started by: tuathan
5 Replies

3. Shell Programming and Scripting

Grep a string from input file and delete next three lines including the line contains string in xml

Hi, 1_strings file contains $ cat 1_strings /home/$USER/Src /home/Valid /home/Review$ cat myxml <projected value="some string" path="/home/$USER/Src"> <input 1/> <estimate value/> <somestring/> </projected> <few more lines > <projected value="some string" path="/home/$USER/check">... (4 Replies)
Discussion started by: greet_sed
4 Replies

4. Shell Programming and Scripting

Search string and print the above line and below lines?.

if the first string matches then print the previous line and current line and also print the following lines if the other string search matches. Input ------ TranTime 2012 10 12 The Record starts here Accountnumber: 4632473431274 TxnCode 323 TranID 329473242834 ccsdkcnsdncskd... (7 Replies)
Discussion started by: laknar
7 Replies

5. Shell Programming and Scripting

Search several string and convert into a single line for each search string using awk command AIX?.

I need to search the file using strings "Request Type" , " Request Method" , "Response Type" and by using result set find the xml tags and convert into a single line?. below are the scenarios. Cat test Nov 10, 2012 5:17:53 AM INFO: Request Type Line 1.... (5 Replies)
Discussion started by: laknar
5 Replies

6. Shell Programming and Scripting

Remove lines between the start string and end string including start and end string Python

Hi, I am trying to remove lines once a string is found till another string is found including the start string and end string. I want to basically grab all the lines starting with color (closing bracket). PS: The line after the closing bracket for color could be anything (currently 'more').... (1 Reply)
Discussion started by: Dabheeruz
1 Replies

7. Shell Programming and Scripting

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

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 and add/replace... (1 Reply)
Discussion started by: suryanarayana
1 Replies

8. 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

9. Shell Programming and Scripting

Search string within a file and list common words from the line having the search string

Hi, Need your help for this scripting issue I have. I am not really good at this, so seeking your help. I have a file looking similar to this: Hello, i am human and name=ABCD. How are you? Hello, i am human and name=PQRS. I am good. Hello, i am human and name=ABCD. Good bye. Hello, i... (12 Replies)
Discussion started by: royzlife
12 Replies

10. UNIX for Dummies Questions & Answers

Search for a string,delete the line and replace with new string in a file

Hi Everyone, I have a requirement in ksh where i have a set of files in a directory. I need to search each and every file if a particular string is present in the file, delete that line and replace that line with another string expression in the same file. I am very new to unix. Kindly help... (10 Replies)
Discussion started by: Pradhikshan
10 Replies
IPFILTER-DEFS(5)						File Formats Manual						  IPFILTER-DEFS(5)

NAME
/etc/netscript/ipfilter-defs - netscript ipfilter-defs compile definitions directory. DESCRIPTION
This manual page documents briefly the compile definition files that are used by the netscript-compile(8) command from the netscript router/firewall network configuration package. This compiler creates a compiled iptables rules file in /etc/netscript/ipfilter-defs.conf (it is a shell script portion) that is sourced by the netscript netscript(8) command to configure the iptables(8) firewall rules in the kernel. STARTUP COMPILATION
The rules can be compiled and automatically loaded on boot by setting the IPV4_CONFIGURE_SWITCH switch in network.conf(5) to the value of the function used to configure the kernel. Net-compile(8) creates this function as Configure. If this switch is set, the netscript startup will run netscript-compile(8) to make sure everything is up to date and load the rules from /etc/netscript/ipfilter-defs.conf, and the relevant settings in network.conf(5) which are used to establish packet grooming and configure the built in kernel netfilter INPUT and FORWARD chains in the filter table. If compilation fails, the previous rule set is not replaced and it is used instead. See the netscript(8) manpage to see how to load and use backup copies of the rule set. CHAIN STRUCTURE
Each chain in the iptables(8) filter table is set up by a corresponding coonstruction function of the same name as the chain. The chains are laced into the iplcl (which is laced in to the INPUT chain) and ipfwd (laced into FORWARD) chains respectively, and the forwarding con- trol chains are set up to take traffic in both directions, with the destination network/interface and source network/interface being used in the lacing chain, and network protocol and port being tied down in each specific chain. For the new in kernel Linux IPSEC, traffic to and from the VPN can be controlled via the iptables policy match module, if you have it patched and compiled into your kernel and iptables. Future versions of the kernel and iptables should have this included in the distrib- uted source. FILE STRUCTURE
All the files defining the rules set are in the /etc/netscript/ipfilter-defs directory. The network-defs file is used to define the regions and network blocks used in the rest of the rules. The prototypes-defs file is used to define protoype rules that can be referenced elsewhere in the rule set. The prototypes.sh file is used to construct shell functions for the netscript-compile(8) command that can be used in the definitions files. DNAT and SNAT are set up in the dnat-defs and masq-defs files respectively. Any file ending in .def is taken as general rule set input for netscript-compile(8). The files generally take the form of tables, with the columns tab or space seperated. The '#' character is supported for commenting, and comments can be on a line by themselves, or at the end of a configuration line. Everything after the '#' is treated as a comment by the netscript-compile(8) compiler. RULE STRUCTURE
The structure of the rule sets is thus. Each chain is started by calling a shell compilation function, (generally ipv4_compile_chain) to create the chain, with the chain name and source/destination regions as arguments, and each rule in the chain by starting a fresh line with the chain name in the first column. Regions are defined as network interface tuples, and are set up in network-defs. They are syntactically the same as shell script vari- ables, and are used the same way in the .def rule set files. Technically this magic is achieved by using eval within the netscript-com- pile(8) shell script. Any interface name can have either of the keywords =clear or =ipsec tied to them by using the `=' character on the end of the interface name. This is used to specifically match IPSEC traffic, or non-IPSEC traffic going over the interface. Typically you would use this when defining a region, though the syntax is valid elsewhere as well. It is recommended that you use this feature to prevent packet injection from adjacent external sources when setting up iptables rules for VPN tunnel traffic. The regions are given as arguments to the compilation function, with the region always being 2 arguments in network/interface order to the function. Each chain rule in the chain is defined by giving first of all the chain name, then the rule type, and its direction. All columns after the 3rd one are specific to and are defined by the rule type. The direction may have a '-' in it. The rules produced by the compiler use the iptables connection based state tracking. Packet by packet rules will be added later. EXAMPLE
Here is an example of part of a .def file: # Access from Office to internet # - only allow outgoing tcp and UDP # and ping traffic - anything else is most # like a tunneling protocol. # We have VPNs for tunneling ipv4_compile_chain -p 90 offcInet droplog $OFFICE_REGN $INTERNET_REGN offcInet ACCEPT_EST BOTH offcInet ACCEPT_PING L2R offcInet ACCEPT_TCP L2R 1:65535 offcInet ACCEPT_UDP L2R 1:65535 The ACCEPT_EST line accepts packets for ESTABLISHED and RELATED connections to the new ones already accepted. New connections are accepted by the ACCEPT_PING, ACCEPT_TCP, and ACCEPT_UDP rules. Please see the iptables(8) manpage for the details on stateful filtering. COMPILE FUNCTIONS
Unless a function is defined in prototypes.sh, there is only one function provided. However this is not limiting as there is a facility for rule macros, as well as the ability to tell the function to use one of the default base rule sets. If you do define a function in prototypes.sh, be careful to handle all errors to function and command calls as otherwise netscript-com- pile(8) will break, as it runs with set -e set. The only defined compile function for IPv4 is: ipv4_compile_chain [-i] [-n] [-b base-chain] [-p priority] [-s slave-chain] <chain-name> <default-target> <from-net> <from-if> [<to-net> <to-if>] You can see the source region and destination region on the end of it. The default-target is one of RETURN, DROP, droplog, or log. The options to this function are as follows: -i Create an input chain for attaching to iplcl instead of the default forward chain for attaching to ipfwd. -n Don't lace the chain into iplcl or ipfwd. -b base-chain Specify an alternate ruleset chain to use. -s slave-chain Configure/deconfigure this chain as well as the one specified. Useful for adjusting input rule set when manipulating the access chain for an IPsec VPN. -p priority Specify the priority of the chain in the lacing rule set. Priority is between 00 and 99, with 00 at the top of the lacing chain, and 99 at the bottom. This is useful for making sure that host specific rule sets occur before more general network related ones, and for putting Internet related ones at the bottom of the lacing chain. DIRECTION STATEMENTS
The direction is as per FreeS/WAN - it uses left and right terminology. The possible directions are as follows: L2R|LEFT2RIGHT|INTERNAL2EXTERNAL|INTERN2EXTERN|I2E|INT2EXT Left to Right, Internal to External R2L|RIGHT2LEFT|EXTERNAL2INTERNAL|EXTERN2INTERN|E2I|EXT2INT Right to Left, External to Internal BOTH|- Both directions, aka none or '-'. AVAILABLE CHAIN RULES
Here are the valid chain rules, and the arguments they expect. COMMENT [word1] [word2] ... Insert a comment into the compile shell script. Fill the 3rd column direction in with '-'. MACRO <macro-name> Specify a macro rule set. Rule set must name start with `MACRO_'. Direction again should be `-'. LOG [word1] [word2] ... Insert a logging rule using the given log meesage, or if none given, using the curretlog message for the chain. LOG_MSG [word1] [word2] ... Set the log message for the chain away from the default of `Chain: <chain-name>' or from previous LOG_MSG setting. Up to 26 letters can be used until truncation limit is reached. RESET_LOG_MSG Reset log message to the default of `Chain: <chain-name>'. REJECT_SMB Jump to smb control chain. Creates smb chain if it does not already exist. DROP_MARTIANS Jump to martian source address control chain. Creates chain if it does not already exist. LOG_PORTSCAN Use the psd module to detect and log portscans. Creates portscan log chain (if not already there) which puts `PORTSCAN DETECTED - ' in the log. DROP_BROADCAST Drop ethernet broadcast packets. LOG_BROADCAST Log ethernet broadcast packets with the current log messages for the chain. ACCEPT_EST Accept ESTABLISH,RELATED packets via the iptables(8) state module. ACCEPT_RELATED Accept RELATED packets via the iptables(8) state module. Useful for ICMP type 3 packets used for maximum MTU detection. ACCEPT_PROTO <protocol> Accept NEW connections for a protocol. Accepts one argument in the 4th column which is the protocol name from /etc/protocols or the protocol number between 0 and 255. REJECT_PROTO <protocol> Reject NEW connections for a protocol with ICMP reject packets. Accepts one argument in the 4th column which is the protocol name from /etc/protocols or the protocol number between 0 and 255. DROP_PROTO <protocol> Drop all packets for a protocol with nothing in reply. Accepts one argument in the 4th column which is the protocol name from /etc/protocols or the protocol number between 0 and 255. LOG_PROTO <protocol> Log NEW connections for a protocol with the current log message for the chain. Accepts one argument in the 4th column which is the protocol name from /etc/protocols or the protocol number between 0 and 255. ACCEPT_TCP [src-port-range] <dst-port-range> Accept NEW TCP connections. If one argument given, it is the destinaion port (range). If 2 arguments, the first is the source port (range), and second the destination port (range). Port ranges are specified by separating them with a `:' character, and ports must be in the /etc/services file, or a number between 0 and 65535. REJECT_TCP [src-port-range] <dst-port-range> Reject NEW TCP connections with an ICMP REJECT packet. If one argument given, it is the destination port(range). If 2 arguments, the first is the source port (range), and second the destination port (range). Port ranges are specified by separating them with a `:' character, and ports must be in the /etc/services file, or a number between 0 and 65535. DROP_TCP [src-port-range] <dst-port-range> Drop all tcp packets, returning nothing at all. If one argument given, it is the destinaion port (range). If 2 arguments, the first is the source port (range), and second the destination port (range). Port ranges are specified by separating them with a `:' character, and ports must be in the /etc/services file, or a number between 0 and 65535. LOG_TCP [src-port-range] <dst-port-range> Log NEW TCP connections with the current log text for the chain. If one argument given, it is the destination port(range). If 2 arguments, the first is the source port (range), and second the destination port (range). Port ranges are specified by separating them with a `:' character, and ports must be in the /etc/services file, or a number between 0 and 65535. ACCEPT_UDP [src-port-range] <dst-port-range> Accept NEW UDP connections. If one argument given, it is the destinaion port (range). If 2 arguments, the first is the source port (range), and second the destination port (range). Port ranges are specified by separating them with a `:' character, and ports must be in the /etc/services file, or a number between 0 and 65535. REJECT_UDP [src-port-range] <dst-port-range> Reject NEW UDP connections with an ICMP REJECT packet. If one argument given, it is the destination port(range). If 2 arguments, the first is the source port (range), and second the destination port (range). Port ranges are specified by separating them with a `:' character, and ports must be in the /etc/services file, or a number between 0 and 65535. DROP_UDP [src-port-range] <dst-port-range> DROP all UDP packets, returning nothing at all. If one argument given, it is the destinaion port (range). If 2 arguments, the first is the source port (range), and second the destination port (range). Port ranges are specified by separating them with a `:' character, and ports must be in the /etc/services file, or a number between 0 and 65535. LOG_UDP [src-port-range] <dst-port-range> Log NEW UDP connections with the current log message for the chain. If one argument given, it is the destination port(range). If 2 arguments, the first is the source port (range), and second the destination port (range). Port ranges are specified by separating them with a `:' character, and ports must be in the /etc/services file, or a number between 0 and 65535. ACCEPT_PING Accept ICMP type 8 echo request packets for network diagnosis. DROP_PING Drop ICMP type 8 packets with no reply. LOG_PING Log an ICMP echo request with the current log message for the chain. ACCEPT_TCP_NET [src_network [src-port-range]] <dst-network> <dst-port-range> Accept NEW TCP connections from given source (optional) to destination. Network is given in IPv4 address/netmask or address/masklen format. Port ranges are specified by separating them with a `:' character, and ports must be in the /etc/services file, or a number between 0 and 65535. REJECT_TCP_NET [src_network [src-port-range]] <dst-network> <dst-port-range> Reject NEW TCP conections with an ICMP reject packet which come from a given source (optional), going to given destination. Network is given in IPv4 address/netmask or address/masklen format. Port ranges are specified by separating them with a `:' character, and ports must be in the /etc/services file, or a number between 0 and 65535. DROP_TCP_NET [src_network [src-port-range]] <dst-network> <dst-port-range> Drop all TCP packets which come from a given source (optional), going to given destination. Network is given in IPv4 address/net- mask or address/masklen format. Port ranges are specified by separating them with a `:' character, and ports must be in the /etc/services file, or a number between 0 and 65535. LOG_TCP_NET [src_network [src-port-range]] <dst-network> <dst-port-range> Log all NEW TCP connections from given source (optional) to destination, with the current log message for the chain. Network is given in IPv4 address/netmask or address/masklen format. Port ranges are specified by separating them with a `:' character, and ports must be in the /etc/services file, or a number between 0 and 65535. ACCEPT_UDP_NET [src_network [src-port-range]] <dst-network> <dst-port-range> Accept NEW UDP connections from given source (optional) to destination. Network is given in IPv4 address/netmask or address/masklen format. Port ranges are specified by separating them with a `:' character, and ports must be in the /etc/services file, or a number between 0 and 65535. REJECT_UDP_NET [src_network [src-port-range]] <dst-network> <dst-port-range> Reject NEW UDP conections with an ICMP reject packet which come from a given source (optional), going to given destination. Network is given in IPv4 address/netmask or address/masklen format. Port ranges are specified by separating them with a `:' character, and ports must be in the /etc/services file, or a number between 0 and 65535. DROP_UDP_NET [src_network [src-port-range]] <dst-network> <dst-port-range> Drop all UDP packets which come from a given source (optional), going to given destination. Network is given in IPv4 address/net- mask or address/masklen format. Port ranges are specified by separating them with a `:' character, and ports must be in the /etc/services file, or a number between 0 and 65535. LOG_UDP_NET [src_network [src-port-range]] <dst-network> <dst-port-range> Log all NEW UDP connections from given source (optional) to destination, with the current log message for the chain. Network is given in IPv4 address/netmask or address/masklen format. Port ranges are specified by separating them with a `:' character, and ports must be in the /etc/services file, or a number between 0 and 65535. ACCEPT_IFACE <interface> Accept all incoming NEW connections from an incoming interface. REJECT_IFACE <interface> Reject all incoming NEW conections with an ICMP reject packet, from an interface. DROP_IFACE <interface> Drop all incoming packets from an interface. LOG_IFACE <interface> Log all incoming NEW conections from an interface. ACCEPT_NET <network> Accept all NEW connections from network. Network is given in IPv4 address/netmask or address/masklen format. REJECT_NET <network> Reject all NEW conections from network with an ICMP reject packet. Network is given in IPv4 address/netmask or address/masklen for- mat. DROP_NET <network> Drop all packets from network. Network is given in IPv4 address/netmask or address/masklen format. LOG_NET <network> Log all NEW conections from network. Network is given in IPv4 address/netmask or address/masklen format. FILES
/etc/netscript/ipfilter-defs.conf, /etc/netscript/ipfilter-defs-compiled.conf, /etc/netscript/ipfilter-defs directory. SEE ALSO
netscript-compile(8), iptables(8), ip6tables(8), netscript(8). AUTHOR
This manual page was written by Matthew Grant <grantma@anathoth.gen.nz>, for the Debian GNU/Linux system (but may be used by others). BUGS
I wrote this manpage when I was not half asleep... Some things are missing from this manpage... Dnat documentation is missing but obvious from configuration file. SNAT documentation is missing but obvious from configuration file. March 25, 2003 IPFILTER-DEFS(5)
All times are GMT -4. The time now is 04:12 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy