Cutting rows after a selected point


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cutting rows after a selected point
# 1  
Old 09-05-2003
Cutting rows after a selected point

I have a log file from which I want to cut out the ten lines assoictiated to my search requirment (cust_ref #).

The cust_ref number will be on te first line and the update information will be on the following ten lines (which dosen't linking data to grep on).

Using a script I would like to search to the cust_ref and cut the 11 lines I need.

Columns, characters I can cut but don't know where to start on cutting whole lines!!!! Smilie

Any usefull tips?

Any Fanks,

Neil
# 2  
Old 09-05-2003
Hi,

Can you give an example of the file format?

Cheers
Helen
# 3  
Old 09-05-2003
This is an example, would take too longto remove customer details.


2003-09-04 12:20:00 INFO [CUST_REF ] blah blah
2003-09-04 12:20:00 INFO [ John ] blah blah
2003-09-04 12:20:00 INFO [ Doe ] bah blah blah
2003-09-04 12:20:00 INFO [ Gender ]
2003-09-04 12:20:00 INFO [ DOB ]
2003-09-04 12:20:00 INFO [ Postal code ]
2003-09-04 12:20:00 INFO [ Limit 100 ] blah blah
2003-09-04 12:20:00 INFO [ API Value ]
2003-09-04 12:20:00 ERROR [ Error Element ]
2003-09-04 12:20:00 INFO [ Record num ]
2003-09-04 12:20:00 INFO [ status ]
several line of mixed information lines before the next lot of details.

Each lot of details in the square brackets will be different, the x number lines after the CUST_REF will be for that customer. The date/time stirng is not an option because you get more than one customer load in the same second and I don't nesacery want all customers. Just the ones I specify.
# 4  
Old 09-05-2003
Try this:

sed -n '/CUST_REF/{N;N;N;N;N;N;N;N;N;N;p;}' < your_file
# 5  
Old 09-05-2003
I like oombera's sed solution, but I wouldn't have chosen sed because it's a bit mysterious to me. Here's an awk solution.
Code:
awk '/CUST_REF/ { i = 11 } i-- > 0' < your_file

When "CUST_REF" is seen on a line, a counter is set for 11 lines. The next pattern selects all lines for which the counter is bigger than 1 and decrements the counter (after the comparison is made --- this is important), but because there is no action associated with it, the default action of "print the current line" is taken.

The perl is similar:
Code:
perl -lne '$i = 11 if /CUST_REF/; print if $i-- > 0' < your_file

The perl has the advantage of not having a line length limit (which may not affect your case).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Reading and copying a selected rows

Dear All, I have a data file input.res like below. (Only six column shown here for example.) Sequence of first column starting from 1 to 148. Input file 1 Q0 9_August_2014_Entertainment2 0 20.14967806339729 BM25b1.0 1 Q0 13_October_2012_Page323 1 20.134224346765738 BM25b1.0 1 Q0... (2 Replies)
Discussion started by: imranrasheedamu
2 Replies

2. Shell Programming and Scripting

Combine multiple rows based on selected column keys

Hello I want to collapse a file with multiple rows into consolidated lines of entries based on selected columns as the 'key'. Example: 1 2 3 Abc def ghi 1 2 3 jkl mno p qrts 6 9 0 mno def Abc 7 8 4 Abc mno mno abc 7 8 9 mno mno abc 7 8 9 mno j k So if columns 1, 2 and 3 are... (6 Replies)
Discussion started by: linuxlearner123
6 Replies

3. Shell Programming and Scripting

To extract selected rows

Hi, I have two files Input1.txt and Input2.txt and i need to have a output with selected lines starting with the values present in Input2.txt. For example: Input1.txt:... (5 Replies)
Discussion started by: bhas
5 Replies

4. Shell Programming and Scripting

Shell to remove a newline char from selected rows in a file.

Greetings! Can we automate the process of removing a newline char from selected rows in a fixed width file using a shell? Input is like abcd1234 xyzd1234 abcd a1b2c3d4 abcd1234 xyzd1234 xx abcd1234 Expected output - abcd1234xyzd1234 abcda1b2c3d4abcd1234xyzd1234 xxabcd1234 ... (3 Replies)
Discussion started by: mailme0205
3 Replies

5. UNIX for Dummies Questions & Answers

Remove a newline char from selected rows.

Greetings! Can we automate the process of removing a newline char from selected rows in a fixed width file using a shell? Input is like abcd1234 xyzd1234 abcd a1b2c3d4 abcd1234 xyzd1234 xx abcd1234 Expected output - abcd1234xyzd1234 abcda1b2c3d4abcd1234xyzd1234 xxabcd1234 ... (2 Replies)
Discussion started by: mailme0205
2 Replies

6. Shell Programming and Scripting

Cutting rows at specific length

Hi, i have a file containing nrows and 3cols. i want to cut it in specific length and save output to individual files. 1 2 3 4 5 6 5 8 9 10 11 12 13 14 15 16 17 18 i need to cut the file say every 2 rows and save it in individual file. 01.dat contains 1 2 3 4 5 6 02.dat 7 8 9... (10 Replies)
Discussion started by: ida1215
10 Replies

7. Shell Programming and Scripting

combining cat output and cutting rows

I have a file that contain the following. -D HTTPD_ROOT="/usr/local/apache" -D SERVER_CONFIG_FILE="conf/httpd.conf" I want a shell script, so that after cat filename and apply the shell script I should get the output as follows. /usr/local/apache/conf/httpd.conf ie cat filename |... (7 Replies)
Discussion started by: anilcliff
7 Replies

8. Shell Programming and Scripting

print selected rows with awk

Hi everybody: Could anybody tell me how I can print from a file a selected rows with awk. In my case I only want print in another file all the rows from NR=8 to NR=2459 and the increment each 8 times. I tried to this: awk '{for (i=8; i=2459; i+=8); NR==i}' file1 > file2 But doesn't... (6 Replies)
Discussion started by: tonet
6 Replies

9. Shell Programming and Scripting

return number of rows selected

Hi all, i am doing a perl script to read from a db. I am able to retrieve the rows, but i am unable to return the number of rows selected. i tried $selectedrows = $sth->numrows; i got the error msg: Can't locate object method "numrows" via package "DBI::st" i changed it to $selectedrows =... (7 Replies)
Discussion started by: new2ss
7 Replies

10. Shell Programming and Scripting

flags to suppress column output, # of rows selected in db2 sql in UNIX

Hello, I am new to db2 SQL in unix so bear with me while I try to explain the situation. I have a text file that has the contents of the where condition that I am using for a db2 SQL in UNIX ksh. Here is the snippet. if ; then echo "Begin processing VALUEs" ... (1 Reply)
Discussion started by: jerardfjay
1 Replies
Login or Register to Ask a Question