Sponsored Content
Top Forums Shell Programming and Scripting search string in a file and retrieve 10 lines including string line Post 302445937 by ranjithpr on Tuesday 17th of August 2010 08:01:01 AM
Old 08-17-2010
Solution using awk

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
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
String::Errf(3pm)					User Contributed Perl Documentation					 String::Errf(3pm)

NAME
String::Errf - a simple sprintf-like dialect VERSION
version 0.006 SYNOPSIS
use String::Errf qw(errf); print errf "This process was started at %{start}t with %{args;argument}n. ", { start => $^T, args => 0 + @ARGV }; ...might print something like: This process was started at 2010-10-17 14:05:29 with 0 arguments. DESCRIPTION
String::Errf provides "errf", a simple string formatter that works something like "sprintf". It is implemented using String::Formatter and Sub::Exporter. Their documentation may be useful in understanding or extending String::Errf. DIFFERENCES FROM SPRINTF
The data passed to "errf" should be organized in a single hashref, not a list. Formatting codes require named parameters, and the available codes are different. See "FORMATTING CODES" below. As with most String::Formatter formatters, "%" is not a format code. If you want a literal "%", do not put anything between the two percent signs, just write "%%". FORMATTING CODES "errf" formatting codes require a set of arguments between the "%" and the formatting code letter. These arguments are placed in curly braces and separated by semicolons. The first argument is the name of the data to look for in the format data. For example, this is a valid use of "errf": errf "The current time in %{tz}s is %{now;local}t.", { tz => $ENV{TZ}, now => time, }; The second argument, if present, may be a compact form for multiple named arguments. The rest of the arguments will be named values in the form "name=value". The examples below should help clarify how arguments are passed. When an argument appears in both a compact and named form, the named form trumps the compact form. The specific codes and their arguments are: s for string The "s" format code is for any string, and takes no arguments. It just includes the named item from the input data. errf "%{name}s", { name => 'John Smith' }; # returns "John Smith" Remember, "errf" does not have any of the left- or right-padding formatting that "sprintf" provides. It is not meant for building tables, only strings. i for integer The "i" format code is used for integers. It takes one optional argument, "prefix", which defaults to the empty string. "prefix" may be given as the compact argument, standing alone. "prefix" is used to prefix non-negative integers. It may only be a plus sign. errf "%{x}i", { x => 10 }; # returns "10" errf "%{x;+}i", { x => 10 }; # returns "+10" errf "%{x;prefix=+}i", { x => 10 }; # returns "+10" The rounding behavior for non-integer values is not currently specified. f for float (or fractional) The "f" format code is for numbers with sub-integer precision. It works just like "i", but adds a "precision" argument which specifies how many decimal places of precision to display. The compact argument may be just the prefix or the prefix followed by a period followed by the precision. errf "%{x}f", { x => 10.1234 }; # returns "10"; errf "%{x;+}f", { x => 10.1234 }; # returns "+10"; errf "%{x;.2}f", { x => 10.1234 }; # returns "10.12"; errf "%{x;+.2}f", { x => 10.1234 }; # returns "+10.12"; errf "%{x;precision=.2}f", { x => 10.1234 }; # returns "10.12"; errf "%{x;prefix=+;precision=.2}f", { x => 10.1234 }; # returns "+10.12"; t for time The "t" format code is used to format timestamps provided in epoch seconds. It can be given two arguments: "type" and "tz". "type" can be either date, time, or datetime, and indicates what part of the timestamp should be displayed. The default is datetime. "tz" requests that the timestamp be displayed in either UTC or the local time zone. The default is local. The compact form is just "type" alone. # Assuming our local time zone is America/New_York... errf "%{x}t", { x => 1280530906 }; # "2010-07-30 19:01:46" errf "%{x;type=date}t", { x => 1280530906 }; # "2010-07-30" errf "%{x;type=time}t", { x => 1280530906 }; # "19:01:46" errf "%{x;type=datetime}t", { x => 1280530906 }; # "2010-07-30 19:01:46" errf "%{x;tz=UTC}t", { x => 1280530906 }; # "2010-07-30 23:01:46 UTC" errf "%{x;tz=UTC;type=date}t", { x => 1280530906 }; # "2010-07-30 UTC" errf "%{x;tz=UTC;type=time}t", { x => 1280530906 }; # "23:01:46 UTC" errf "%{x;tz=UTC;type=datetime}t", { x => 1280530906 }; # "2010-07-30 23:01:46 UTC" n and N for numbered The "n" and "N" format codes are for picking words based on number. It takes two of its own arguments, "singular" and "plural", as well as "prefix" and "precision" which may be used for formatting the number itself. If the value being formatted is 1, the singular word is used. Otherwise, the plural form is used. errf "%{x;singular=dog;plural=dogs}n", { x => 0 }; # 0 dogs errf "%{x;singular=dog;plural=dogs}n", { x => 1 }; # 1 dog errf "%{x;singular=dog;plural=dogs}n", { x => 2 }; # 2 dogs errf "%{x;singular=dog;plural=dogs}n", { x => 1.4 }; # 1.4 dogs errf "%{x;singular=dog;plural=dogs;precision=1}n", { x => 1.4 }; # 1.4 dogs errf "%{x;singular=dog;plural=dogs;precision=0}n", { x => 1.4 }; # 1 dog If "N" is used instead of "n", the number will not be included, only the chosen word. errf "%{x;singular=is;plural=are}N", { x => 0 }; # are errf "%{x;singular=is;plural=are}N", { x => 1 }; # is errf "%{x;singular=is;plural=are}N", { x => 2 }; # are errf "%{x;singular=is;plural=are}N", { x => 1.4 }; # 1.4 are errf "%{x;singular=is;plural=are;precision=1}N", { x => 1.4 }; # 1.4 are errf "%{x;singular=is;plural=are;precision=0}N", { x => 1.4 }; # 1 is The compact form may take any of the following forms: word - equivalent to singular=word word+suffix - equivalent to singular=word;plural=wordsuffix word1/word2 - equivalent to singular=word;plural=word2 If no singular form is given, an exception is thrown. If no plural form is given, one will be generated according to some basic rules of English noun orthography. AUTHOR
Ricardo Signes <rjbs@cpan.org> COPYRIGHT AND LICENSE
This software is copyright (c) 2010 by Ricardo Signes. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. perl v5.12.3 2010-10-29 String::Errf(3pm)
All times are GMT -4. The time now is 12:59 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy