printing lines to a file from a particular string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting printing lines to a file from a particular string
# 1  
Old 02-24-2010
printing lines to a file from a particular string

Hi,
A very Good Evening to All,
I am writing a script for my application. I have a file with 1000 lines. Among that 1000 lines i am searching for a particular string. And from that string i need to pull all the data in to a seperate file.

For example the contents of my file is as below.

1. raju
2. raju
3. raju
4. Wed Mar 16 20:15:22 2005
5. hello
6. i am very good person.
7. Wed Mar 16 20:15:22 2005
8. hello
9. Hi
10. Wed Mar 17 20:15:22 2005
11. hello
12. hi.

There will be no line numbers in my file i have give just for our reference.
Now i am searching for a string using the below command.

grep "Wed Mar 16.*2005" raju.log | head -1. So the command output will be the 4th line. So from now from the 4th line i need to pull all the lines from 5 to 12 in to file.

Please let me know how to achieve this.

Thanks,
Raju
# 2  
Old 02-24-2010
Code:
awk '/Wed Mar 16.*2005/ && !f {c=8;f=1;next}c-->0' file > newfile

# 3  
Old 02-24-2010
This command delete from the first line to the line having pattern "Wed Mar 16.*2005"
Code:
sed "1,/Wed Mar 16.*2005/d" file

This command prints along with your pattern "Wed Mar 16.*2005" to the end of file
Code:
sed "/Wed Mar 16.*2005/,$ !d" file

# 4  
Old 02-24-2010
Try:

Code:
awk '/Wed Mar 16/ { c=1; } c==1  && f++<9 && f>1' < infile >outfile

# 5  
Old 02-24-2010
hi Anbu23,
thank you so much. it worked like a charm.

Thanks,
Raju
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help in printing n number of lines if a search string matches in a file

Hi I have below script which is used to grep specific errors and if error string matches send an email alert. Script is working fine , however , i wish to print next 10 lines of the string match to get the details of error in the email alert Current code:- #!/bin/bash tail -Fn0 --retry... (2 Replies)
Discussion started by: neha0785
2 Replies

2. Shell Programming and Scripting

Printing the lines of the string with highest value

Please help . The script need to first grep for all lines with "C:" as it contains a value Here the value is 0 1,00: This , is a good script c:0 and then give output of the lines with top 3 highest value for c: 1,00: This , is a nice script c:9999 1,00: This , is a... (3 Replies)
Discussion started by: necro98
3 Replies

3. Shell Programming and Scripting

Printing the lines that appear in an other file, and the three lines after them

Hi ! I need some help with a script I am writing. I am trying to compare two files, the first file being in this format : Header1 Text1-1 Text1-2 Text1-3 Header2 Text2-1 etc... For each header, I want to check if it appears in the second file, and if it is the case print the header... (4 Replies)
Discussion started by: jbi
4 Replies

4. Shell Programming and Scripting

Trouble printing multiple lines to a new file

Hi, I'm trying to auto generate some php files with a default preamble at the top which is a block comment. The problem is that my output has no new lines and it looks like the output from "ls" is being printed after everyline This is my code #!/bin/bash read -d '' pre_amble... (1 Reply)
Discussion started by: racshot65
1 Replies

5. Shell Programming and Scripting

Printing all lines before a specific string and a custom message 2 lines after

Hello all, I need to print all the lines before a specific string and print a custom message 2 lines after that. So far I have managed to print everything up the string, inclusively, but I can't figure out how to print the 2 lines after that and the custom message. My code thus far is:... (4 Replies)
Discussion started by: SEinT
4 Replies

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

7. Shell Programming and Scripting

Printing 10 lines above and below the search string: help needed

Hi, The below code will search a particular string(say false in this case) and return me 10 lines above and below the search string in a file. " awk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r;print("***********************************");print;c=a;}b{r=$ 0}' b=10 a=10 s="false" " ... (5 Replies)
Discussion started by: vimalm22
5 Replies

8. UNIX for Dummies Questions & Answers

Counting no. of lines and printing it at the start of the file

Dear users, I need to count the number of lines in a simple text file and print the number on the first line of that same file. I know I can count the lines using wc -l file.txt this gives for example 100 file.txt I need the number 100 to be printed at the very top of file.txt... (2 Replies)
Discussion started by: jenjen_mt
2 Replies

9. Shell Programming and Scripting

Printing the lines which proceeds the particular string

Hi, We are facing some issues while finding the particular string. Our file is: cat 1.txt Node Name(s) Preparation fragment Partition: Transformation instance: Transformation: Applied rows: Affected rows: Rejected rows: Throughput(Rows/Sec): Throughput(Bytes/Sec): Last... (3 Replies)
Discussion started by: Amey Joshi
3 Replies

10. Shell Programming and Scripting

printing first n lines in a file without using head

i have to print first n lines of a file. how can i do that without using head command. for some reason i do not want to use Head. is there a way to get that result using awk or sed?. i an using this on korn shell for AIX Thanks.. (7 Replies)
Discussion started by: dareman123
7 Replies
Login or Register to Ask a Question