grep within certain part of file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers grep within certain part of file
# 1  
Old 11-26-2007
grep within certain part of file

Hi,

Is it possible to grep only on a certain part of a file? Say I have file in.txt which contains below:

11/16 13:07:19.5436 --- ERROR 123 detected.
11/16 13:08:19.5436 --- Generating a <reading> event
11/16 13:08:19.7784 ---- Sending a <writing> event
11/16 14:08:37.4516 --- Generating a <reading> event
11/16 14:08:37.9199 ---- Sending a <writing> event
11/16 14:09:19.5436 --- ERROR 123 detected.
11/16 14:09:49.8452 --- Generating a <reading> event
11/16 14:09:50.0868 ---- Sending a <writing> event
11/16 16:10:51.6707 --- Generating a <reading> event
11/16 16:10:52.3232 ---- Sending a <writing> event
11/16 16:08:19.5436 --- Generating a <reading> event
11/16 16:08:19.7784 ---- Sending a <writing> event
11/16 16:08:19.9436 --- ERROR 123 detected.
11/16 16:08:37.4516 --- Generating a <reading> event
11/16 16:08:37.9199 ---- Sending a <writing> event
11/16 16:09:49.8452 --- Generating a <reading> event
11/16 16:09:50.0868 ---- Sending a <writing> event
11/16 16:10:51.6707 --- Generating a <reading> event
11/16 16:10:52.3232 ---- Sending a <writing> event


Now, I only want to get a certain part on the file.. say the part

11/16 14:09:49.8452 --- Generating a <reading> event
11/16 14:09:50.0868 ---- Sending a <writing> event
11/16 16:10:51.6707 --- Generating a <reading> event
11/16 16:10:52.3232 ---- Sending a <writing> event
11/16 16:08:19.5436 --- Generating a <reading> event
11/16 16:08:19.7784 ---- Sending a <writing> event

which is after the 2nd "ERROR 123 detected." line. Then put it in a file.

Also, is it possible to cut this file into chunk. The delimiter would be the "ERROR 123 detected."
Each chunk of file will be put into a new file. Im just new to unix and still learning and I have many ideas in mind that I could not execute due to insufficient knowledge in the language. Thanks.
# 2  
Old 11-26-2007
Quote:
Also, is it possible to cut this file into chunk. The delimiter would be the "ERROR 123 detected."
Each chunk of file will be put into a new file.
Something like this:

Code:
awk '/ERROR [0-9]* detected/{close(f);f=($4"_"$5"_"++c".out")}f{print>f}' filename

Use nawk on Solaris.


Quote:
Now, I only want to get a certain part on the file.. say the part

11/16 14:09:49.8452 --- Generating a <reading> event
11/16 14:09:50.0868 ---- Sending a <writing> event
11/16 16:10:51.6707 --- Generating a <reading> event
11/16 16:10:52.3232 ---- Sending a <writing> event
11/16 16:08:19.5436 --- Generating a <reading> event
11/16 16:08:19.7784 ---- Sending a <writing> event

which is after the 2nd "ERROR 123 detected." line. Then put it in a file.

Code:
awk '/ERROR [0-9]* detected/{++c};c==2' filename>second_error.out

Use nawk or /usr/xpg4/bin/awk on Solaris.

Last edited by radoulov; 11-26-2007 at 06:12 AM.. Reason: need more coffee :)
# 3  
Old 11-26-2007
You could also use the split command to cut the file into chunks.

Code:
split -p "ERROR 123 detected"  filename outfileprefix

The 2nd outfile (ending in ab) contain the lines for the 2nd "ERROR 123 detected"
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to make a loop to read the input from a file part by part?

Hi All, We've a VDI infrastructure in AWS (AWS workspaces) and we're planning to automate the process of provisioning workspaces. Instead of going to GUI console, and launching workspaces by selecting individual users is little time consuming. Thus, I want to create them in bunches from AWS CLI... (6 Replies)
Discussion started by: arun_adm
6 Replies

2. UNIX for Beginners Questions & Answers

Grep a section from an UNIX file obtaining only part of the data

Hello, I have a log file that has several sections "BEGIN JOB, End of job" like in the following example: 19/06/12 - 16:00:57 (27787398-449294): BEGIN JOB j1(27787398-449294) JOB1 19/06/12 - 16:00:57 (27787398-449294): DIGIT: 0 number of present logs : 1 19/06/12 - 16:00:57... (4 Replies)
Discussion started by: mvalonso
4 Replies

3. UNIX for Dummies Questions & Answers

Grep a part of a line from a file

Hi, I have a file with thousands of lines as below INSERT INTO T_DIM_CLNT(CLNT_KY,CLNT_OBJ_ID,ISI_CLNT_ID,OPERN_ID,CLNT_NM,PRMRY_SIC_CD,PRMRY_SIC_DSC,RET_AGE_NBR,REC_CRT_TS,REC_DATA_EXTRC_TS,ETL_LOG_KY) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)... (5 Replies)
Discussion started by: sudhakar T
5 Replies

4. Shell Programming and Scripting

Grep a part of file based on string identifiers

consider below file contents cat myOutputFIle.txt 8 CCM-HQE-ResourceHealthCheck: Resource List : No RED/UNKNOWN resource Health entries found ---------------------------------------------------------- 9 CCM-TraderLogin-Status: Number of logins: 0... (4 Replies)
Discussion started by: vivek d r
4 Replies

5. Shell Programming and Scripting

[Solved] Printing a part of the last line of the specific part of a file

Hi, I have 80 large files, from which I want to get a specific value to run a Bash script. Firstly, I want to get the part of a file which contains this: Name =A xxxxxx yyyyyy zzzzzz aaaaaa bbbbbb Value = 57 This is necessary because in a file there are written more lines which... (6 Replies)
Discussion started by: wenclu
6 Replies

6. Shell Programming and Scripting

[awk] grep a part of filename as entry file

hi all, i need to combine these files into one csv file. Bounce_Mail_Event_Daily_Report_01_Jul_2012.csv Bounce_Mail_Event_Daily_Report_02_Jul_2012.csv Bounce_Mail_Event_Daily_Report_03_Jul_2012.csv Bounce_Mail_Event_Daily_Report_04_Jul_2012.csv... (10 Replies)
Discussion started by: makan
10 Replies

7. UNIX for Dummies Questions & Answers

can I grep a part of one line ?

<exp code="12556a" message="ok, fine4" displayMessage="jksdfj ksd" \> <exp code="123456a" message="ok, 2fine" displayMessage="jksdfj ksd" \> <exp code="12dfgda" message="1ok, fine" displayMessage="jksdfj ksd" \> now I want to cut code attribute and message attribute, such as ... (2 Replies)
Discussion started by: vincent_W
2 Replies

8. Shell Programming and Scripting

How to get a part of the line(need help in using grep)

Hi, Suppose, DBconnection=jdbc: oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=x.x.x.x)(PORT=YYYY))(LOAD_BALANCE=yes)(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=project_db1))) This is a part of a file <filename> . I Need to get the value of SERVICE_NAME from this line… The... (4 Replies)
Discussion started by: Dpu
4 Replies

9. UNIX for Dummies Questions & Answers

How to use grep to get only part of a line...

Hello All. I have an output file which contains the phrase "Total DFT Energy =" and then a number. This occurs many times in the output file, and what I want is to pipe the numbers (which are all different) to a file so I can plot them. How do I grep "Total DFT Energy =" and then get the numbers... (3 Replies)
Discussion started by: EinsteinMcfly
3 Replies

10. Shell Programming and Scripting

grep part of file name

Hi, I have a problem to use grep in my script . If I want to grep the file for example : PTWO9089.txt The code below works . grep ^PONE But, I dont know on how to grep the file like this 9066PTWO.txt I'll try to use this code : grep PTWO^ ,but it doesn't work. For your info, the... (4 Replies)
Discussion started by: badbunny9316
4 Replies
Login or Register to Ask a Question