Grep multiple lines and save to a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep multiple lines and save to a file
# 1  
Old 08-31-2009
Grep multiple lines and save to a file

Sir


I have a data file e.g. DATA31082009. This file consists of several data files appended to that file. The size of each data file is different. The first line of each file starts with "44". I want to grep data from "44" to the preceding line of next "44" and save it as a individual file. Thus I want to divide the main DATA31082009 into multiple files giving multiple names.
I am new to unix and shell scripting and highly grateful if anybody help me.
# 2  
Old 08-31-2009
Hi.

Can you post a sample of your input data, and what convention you want for the output filenames (meaning, how should the output filenames be named)?
# 3  
Old 08-31-2009
what have you tried and what problems have you had? (Hint: grep won't split your files)
# 4  
Old 08-31-2009
Code:
446003117FA&CAO/ICF, PERAMBUR,CHENNAI-38         ICFPF-  141207         6000010002320000005        0000006500000000001565000030122007                       
2260002500610            023037JOHN ASIRVATHAM    S                    6000010006003117FA&CAO/ICF/PER-CH-38122007815365 0000002410000                       
2260001904310   000000465483840HARIDOSS     T                          6000010006003117FA&CAO/ICF/PER-CH-38122007612111 0000006500000                       
2260002501210            028692SIVAKUMAR     PG                        6000010006003117FA&CAO/ICF/PER-CH-38122007599295 0000000300000                       
2260001505010   9173           BASKARAN     S                          6000010006003117FA&CAO/ICF/PER-CH-38122007673678 0000000120000                       
2260002501310            040558MOHANRAJ     A                          6000010006003117FA&CAO/ICF/PER-CH-38122007652990 0000000820000                       
2260002501510            072469GAJENDRA BABU    MT                     6000010006003117FA&CAO/ICF/PER-CH-38122007717061 0000000700000                       
2260002800610            015219VIJAYACHANDAR     T                     6000010006003117FA&CAO/ICF/PER-CH-38122007781118 0000004000000                       
2260001101410            030438SIGHAMANI     S                         6000010006003117FA&CAO/ICF/PER-CH-38122007699333 0000000800000

This is one sample file consists 8 records and called an individual file. Other files may have much more or less records. For every file the differentiator is "44" at the beginning.

I have used
Code:
grep "^44" data31082009 (combined file name pl.) | cut -c 3-9,126-133 > myfile

to obtain no. of files in the combined file.

With the help of your forum, I could come upto this. From now on I am not progressing.
I need to grep each file startiang with 44 and ending with preceding line of next file which starts again with 44. The name of the file should be 3-9 (number in "44" line followed by 126-133 (date in "44" field.
Sorry for lengthy reply - question.

---------- Post updated at 08:21 PM ---------- Previous update was at 08:21 PM ----------

Code:
446003117FA&CAO/ICF, PERAMBUR,CHENNAI-38         ICFPF-  141207         6000010002320000005        0000006500000000001565000030122007                       
2260002500610            023037JOHN ASIRVATHAM    S                    6000010006003117FA&CAO/ICF/PER-CH-38122007815365 0000002410000                       
2260001904310   000000465483840HARIDOSS     T                          6000010006003117FA&CAO/ICF/PER-CH-38122007612111 0000006500000                       
2260002501210            028692SIVAKUMAR     PG                        6000010006003117FA&CAO/ICF/PER-CH-38122007599295 0000000300000                       
2260001505010   9173           BASKARAN     S                          6000010006003117FA&CAO/ICF/PER-CH-38122007673678 0000000120000                       
2260002501310            040558MOHANRAJ     A                          6000010006003117FA&CAO/ICF/PER-CH-38122007652990 0000000820000                       
2260002501510            072469GAJENDRA BABU    MT                     6000010006003117FA&CAO/ICF/PER-CH-38122007717061 0000000700000                       
2260002800610            015219VIJAYACHANDAR     T                     6000010006003117FA&CAO/ICF/PER-CH-38122007781118 0000004000000                       
2260001101410            030438SIGHAMANI     S                         6000010006003117FA&CAO/ICF/PER-CH-38122007699333 0000000800000

This is one sample file consists 8 records and called an individual file. Other files may have much more or less records. For every file the differentiator is "44" at the beginning.

I have used
Code:
grep "^44" data31082009 (combined file name pl.) | cut -c 3-9,126-133 > myfile

to obtain no. of files in the combined file.

With the help of your forum, I could come upto this. From now on I am not progressing.
I need to grep each file startiang with 44 and ending with preceding line of next file which starts again with 44. The name of the file should be 3-9 (number in "44" line followed by 126-133 (date in "44" field.
Sorry for lengthy reply - question.
# 5  
Old 08-31-2009
Code:
#!/bin/sh
while read line
do
  if echo "$line" | grep "^44" > /dev/null
  then
    filename=`echo "$line" | cut -c 3-9,126-133`
  fi
  echo "$line" > "${filename}.txt"
done

Untested - try it someplace safe and expect to do a little debugging Smilie
# 6  
Old 08-31-2009
yes, the same shorter:
Code:
#!/bin/sh

while read line; do
   [ ${line:0:2} = 44 ] && filename=`cut -c 3-9,126-133 <<<"$line"`
   echo "$line" > "${filename}.txt"
done < data31082009

# 7  
Old 09-02-2009
Question

Quote:
Originally Posted by daPeach
yes, the same shorter:
Code:
#!/bin/sh
...
   [ ${line:0:2} = 44 ] && filename=`cut -c 3-9,126-133 <<<"$line"`
...

Is that bourne syntax?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing multiple lines from input file, if multiple lines match a pattern.

GM, I have an issue at work, which requires a simple solution. But, after multiple attempts, I have not been able to hit on the code needed. I am assuming that sed, awk or even perl could do what I need. I have an application that adds extra blank page feeds, for multiple reports, when... (7 Replies)
Discussion started by: jxfish2
7 Replies

2. Shell Programming and Scripting

Remove duplicate lines, sort it and save it as file itself

Hi, all I have a csv file that I would like to remove duplicate lines based on 1st field and sort them by the 1st field. If there are more than 1 line which is same on the 1st field, I want to keep the first line of them and remove the rest. I think I have to use uniq or something, but I still... (8 Replies)
Discussion started by: refrain
8 Replies

3. UNIX for Dummies Questions & Answers

How to grep multiple lines from a text file using another text file?

I would like to use grep to select multiple lines from a text file using a single-column text file. Basically I want to only select lines from the first text file where the second column of the first text file matches the second text file. How do I go about doing that? Thanks! (5 Replies)
Discussion started by: evelibertine
5 Replies

4. Shell Programming and Scripting

How to remove blank lines in a file and save the file with same name?

I have a text file which has blank lines. I want them to be removed before upload it to DB using SQL *Loader. Below is the command line, i use to remove blank lines. sed '/^ *$/d' /loc/test.txt If i use the below command to replace the file after removing the blank lines, it replace the... (6 Replies)
Discussion started by: vel4ever
6 Replies

5. Shell Programming and Scripting

cut lines from log file and save it another file

Dears, i want cut the lines from a log file. Example of the log file as follows.. May 27, 2011 5:54:51 PM com.huawei.ivas.utilities.sm.client.SMDeliverContrUtil isDeliverSM FINE: May 27, 2011 5:54:51 PM com.huawei.ivas.utilities.sm.client.SMUtil addSysUpMsgLog INFO: . The message content... (1 Reply)
Discussion started by: tonypalokkaran
1 Replies

6. Shell Programming and Scripting

Pulling x number of lines from one file and save into another

Hi, I have a log file (updates.log), and I want to hunt the file for any errors. Here's an example of the log file: SQL> update <table1> set <value1> = '*****'; update <table1> set <value1> = '*****' * ERROR at line 1: ORA-00942: table or view does not exist Elapsed:... (4 Replies)
Discussion started by: dbchud
4 Replies

7. UNIX for Dummies Questions & Answers

grep command to find multiple strings in multiple lines in a file.

I want to search files (basically .cc files) in /xx folder and subfolders. Those files (*.cc files) must contain #include "header.h" AND x() function. I am writing it another way to make it clear, I wanna list of *.cc files that have 'header.h' & 'x()'. They must have two strings, header.h... (2 Replies)
Discussion started by: ritikaSharma
2 Replies

8. Shell Programming and Scripting

Grep multiple lines from a file

Hi, I would like to ask if there is any method to grep a chuck of lines based on the latest file in a directory. E.g Latest file in the directory: Line 1: 532243 Line 2: 123456 Line 3: 334566 Line 4: 44567545 I wanted to grep all the line after line 2 i.e. Line 3 and line 4 and... (5 Replies)
Discussion started by: dwgi32
5 Replies

9. AIX

Grep multiple lines and redirect to file

I have setof files with data and with same fields multiple times in each of the files. for example: file 1 name = mary kate last name = kate address = 123 street = abc name = mary mark last name = mark address = 456 street = bcd file 2 name = mary kate last name = kate... (2 Replies)
Discussion started by: relearner
2 Replies

10. Shell Programming and Scripting

Grep on multiple lines

I 'm trying to grep 2 fieldds on 2 differnt lines. Like this: psit > file egrep -e '(NS|ES)' $file. Not working. If this succeeds then run next cmd else exit. Pls Help Gundu (13 Replies)
Discussion started by: gundu
13 Replies
Login or Register to Ask a Question