Reading files using grep/sed/awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading files using grep/sed/awk
# 1  
Old 06-30-2008
Reading files using grep/sed/awk

After pouring over my LTKS and Unix in a nutshell, I'm stuck!

I have a large (BMC Report File) that has breaks on DM (district managers).

After a report header, there is a DM header like:
DM: DBP AARON ROBERTS
At the end of each DM break, there is:
** END OF REPORT **

There are also DM's in the report that I am not interested in and must be skipped.

What I grep on is the 3 character DM code, in this instance it is "DBP'. Once I find this line, I write each line into a new file called:
MONTHLY_PLAN_REPORT_XXX where XXX is DBP
until I reach ** END OF REPORT **, after which I move on to the next needed DM in my FOR loop.

I had thought to read each line until I found what I needed and then start all over again. LTKS states on pg 226 (The authors have gone for years without writing a script with read in it). What other way is there?

Please Help!

Jody
# 2  
Old 06-30-2008
One way,

Code:
awk '$2~/DBP|AAA|BBB/{f=1;rep="MONTHLY_PLAN_REPORT_" $2} f{print >> rep}/\*\* END OF REPORT \*\*/{f=0;close(rep)}' BMC_report

Replace the patterns AAA, BBB,... with your actual codes, divided by the pipe | symbol.
# 3  
Old 06-30-2008
Quote:
Originally Posted by rubin
One way,

Code:
awk '$2~/DBP|AAA|BBB/{f=1;rep="MONTHLY_PLAN_REPORT_" $2} f{print >> rep}/\*\* END OF REPORT \*\*/{f=0;close(rep)}' BMC_report

Replace the patterns AAA, BBB,... with your actual codes, divided by the pipe | symbol.
What about this one:
Code:
awk '$1=="DM:" && $2 !~ /EXCLUDE|AAA|BBB/{f=1;rep="MONTHLY_PLAN_REPORT_" $2} f{print >> rep}/\*\* END OF REPORT \*\*/{f=0;close(rep)}' BMC_report

# 4  
Old 06-30-2008
Smilie What happens if the "EXCLUDE" list is longer than the "INCLUDE" one ...? In absence of a real input, we most of the time rely on guessing.... Either way the OP has now options to choose from...Smilie
# 5  
Old 07-01-2008
Thanks guys. I see I have to do a little more awk study. The inclusion list is much smaller than the exclusion list. About 15 out of 75. As soon as I can get logged in at work, I'll let you know how it went.

Thanks again!

Jody

I have the following shell script so far:

for x in DBG DBH DBT DCP DCR DCV DDM DDN DDO DDX DDZ DEA DEN DEO DFR DFZ DGA DHR DIC
do
make_empty_file_func $IMSDATA/monthly_plan_report_$x # this creates a new file if needed or zeros out an existing file
print $header1 >> $IMSDATA/monthly_plan_report_$x
print $header2 >> $IMSDATA/monthly_plan_report_$x

### Fancy awk stuff here

done

Is there a way to pass in $x to the awk script so it will use it instead of hardcoding the $2~/DBP|AAA|BBB/ ?

Thanks

Jody

Last edited by Jodyman; 07-01-2008 at 12:49 AM..
# 6  
Old 07-01-2008
Quote:
Originally Posted by rubin
Smilie What happens if the "EXCLUDE" list is longer than the "INCLUDE" one ...? In absence of a real input, we most of the time rely on guessing.... Either way the OP has now options to choose from...Smilie
Always take the short way Smilie
# 7  
Old 07-01-2008
Quote:
Originally Posted by Jodyman
I have the following shell script so far:

for x in DBG DBH DBT DCP DCR DCV DDM DDN DDO DDX DDZ DEA DEN DEO DFR DFZ DGA DHR DIC
do
make_empty_file_func $IMSDATA/monthly_plan_report_$x # this creates a new file if needed or zeros out an existing file
print $header1 >> $IMSDATA/monthly_plan_report_$x
print $header2 >> $IMSDATA/monthly_plan_report_$x

### Fancy awk stuff here

done

Is there a way to pass in $x to the awk script so it will use it instead of hardcoding the $2~/DBP|AAA|BBB/ ?

Thanks

Jody
Yes there is, see below, but you're still using some hard coding in your command, because you have the codes inside the script.
I'd put all of your codes in a codes_file, one code per line, and update the file when needed. Also awk can overwrite/create files and insert headers itself:

Code:
$ cat reports

#!/bin/ksh

 while read x 
 do 
   awk -v s="$x" 'BEGIN{rep="MONTHLY_REPORT_" s; print "-Header 1-\n-Header 2-" >rep}$1=="DM:" && $2~s{f=1}f{print> rep}/\*\* END OF REPORT \*\*/{f=0}' BMC_report
 done < codes_file

$ cat codes_file
DBG
DBH
DBT
DCP
DBP
DCV
DDM
DGA
DHR
...


Or run the script with all the codes in the command line, which are passed as positional parameters to the command:

Code:
./reports  DBG DBH DBT DCP DBP DCV DDM DGA DHR DIC ...


$cat reports

#!/bin/ksh
# set -x
 
 for x in $*
  do
   awk -v s="$x" 'BEGIN{rep="MONTHLY_REPORT_" s; print "-Header 1-\n-Header 2-">rep}$1=="DM:"&&$2~var{f=1}f{print> rep}/\*\* END OF REPORT  \*\*/{f=0}' BMC_report
  done

Use full paths for the new file names ( /full/path/to/MONTHLY_PLAN...) inside awk , not $IMSDATA/..., if the command is run from a different directory. Also I see
that you don't have too many symbols, so close(file) might not be needed here.

Last edited by rubin; 07-01-2008 at 10:12 PM.. Reason: so close(file) might not be needed here.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Reading and appending a row from file1 to file2 using awk or sed

Hi, I wanted to add each row of file2.txt to entire length of file1.txt given the sample data below and save it as new file. Any idea how to efficiently do it. Thank you for any help. input file file1.txt file2.txt 140 30 200006 141 32 140 32 200006 142 33 140 35 200006 142... (5 Replies)
Discussion started by: ida1215
5 Replies

2. Shell Programming and Scripting

awk - sed / reading from a data file and doing algebraic operations

Hi everyone, I am trying to write a bash script which reads a data file and does some algebraic operations. here is the structure of data.xml file that I have; 1 <data> 2 . 3 . 4 . 5 </data> 6 <data> 7 . 8 . 9 . 10</data> etc. Each data block contains same number of lines (say... (4 Replies)
Discussion started by: hayreter
4 Replies

3. Shell Programming and Scripting

Using grep with multiple loops in reading files

I am trying to read a file line by line and then search that line in another file and get a particular column from the second file. I have written this code.but its not working properly #!/bin/sh while read keyword in duplicate.txt do echo $keyword while read line do ... (7 Replies)
Discussion started by: Prachi Gupta
7 Replies

4. Shell Programming and Scripting

Help with reading two input files in awk

Hello, I'm trying to write an awk program that reads two files inputs. example, file 1: 0.00017835 0.000176738 0.00018811 0.000189504 0.000188155 0.000180065 0.000178991 0.000178252 0.000182513 file 2: 1.7871769E-05 1.5139576E-16 1.5140196E-16 1.5139874E-16 1.7827407E-04 ... (5 Replies)
Discussion started by: joseamck
5 Replies

5. Shell Programming and Scripting

reading files using awk

Hi , I have a awk script to read a file using getline, but when its not able to recognize the path of the file. I am working on solaris unix. nawk -F'|' ' BEGIN{ FILE1="/input_files/temp.txt" } I have to place my script in the same folder i.e /input_files/ for my script to work. ... (2 Replies)
Discussion started by: rashmisb
2 Replies

6. UNIX for Dummies Questions & Answers

Reading compressed files during a grep search

All, The bottom line is that im reading a file, storing it as variables, recursively grep searching it, and then piping it to allow word counts as well. I am unsure on how to open any .zip .tar and .gzip, search for keywords and return results. Any help would be much appreciated! Thanks (6 Replies)
Discussion started by: ryan.lee
6 Replies

7. Shell Programming and Scripting

Reading files using AWK or SED

hi Friends, Please help me to give a try for writing a shell script either with awk or SED for the below requirement. i have file with 3 lines, each of size 3200 chars, wanted to read this file and divide eachline with 200 columns with differensizes for each column value by keeping seperator.... (3 Replies)
Discussion started by: balireddy_77
3 Replies

8. Shell Programming and Scripting

reading from 2 files using awk

hi, Is it possible to read and compare 2 files which have different Field separators at the same time using awk??? file1: 1,dayal,maruti,Z-234,bangalore,KA,........ 2,yash,esteem,Y-007,delhi,DL,........... . . . fill 2: Z-234|Registered|Bangalore Y-007|Registered|Bangalore . . . ... (2 Replies)
Discussion started by: VGR
2 Replies

9. Shell Programming and Scripting

Reading Two files at the same time using awk

Hi All, I am very new to awk script writing. I have two files(file1 and file2) containing some numbers. I want divide the numbers in file1 with those in file 2 line wise. is it possible to simultaneousely read the information from both files and carry put the division. Thanks a lot in... (6 Replies)
Discussion started by: suneeldutt
6 Replies

10. Shell Programming and Scripting

Reading an Input file and searching for occurrences WIHOUT SED or AWK

Hi people. I am new to shell scripting, so I need a little help. I want to create a script named that takes an argument as a file, Read the input file and look for occurrences of the current username (say abc.xyz) who is executing the script. On finding an occurrence of the username take that line... (2 Replies)
Discussion started by: kartikkumar84@g
2 Replies
Login or Register to Ask a Question