Grep multiple lines and redirect to file


 
Thread Tools Search this Thread
Operating Systems AIX Grep multiple lines and redirect to file
# 1  
Old 04-30-2009
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
address = 123
street = abc

name = thomas Train
last name = Train
address = 091
street= zxc

I used grep and awk, to grep the field and print the data using awk
but I am not knowing how to use grep and awk to print from dirrent files at the same time.. all these files begin with filename.dat and reside in one directory

What I did was :
grep "^last name" *filename.dat | awk -F ":" '{print $1 $2}' > tempfile1

this doesn't give what I want

what I am looking for is an output like

filename:last name:address
file1:kate:123

Any help is really appreciated
Thanks
# 2  
Old 04-30-2009
try this:

Code:
awk -F "=" '
$0 ~ /last name/ {n=$2;pn=1}
$0 ~ /address/ {a=$2;an=1};pn == 1 && an == 1{print FILENAME,n,a;pn=0;an=0}' OFS=":" file1 file2

op:
file1: kate: 123
file1: mark: 456
file2: kate: 123
file2: Train: 091


cheers,
Devaraj Takhellambam
# 3  
Old 04-30-2009
Thanks this helps
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Redirect script output to file after grep

i have simple program that generate log file 1 line every sec, i need to do grep for specific record then redirect to another file. #!/bin/bash for i in `seq 1 20`; do echo $i sleep 1 done ./test.sh |egrep "5|10|15" 5 10 15 r ./test.sh... (2 Replies)
Discussion started by: before4
2 Replies

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

3. Shell Programming and Scripting

Redirect grep error/result to file

I'm having trouble piping grep results/error to a file. Why can't I redirect these messages into a file? $ grep -r -o "pattern" * > log Binary file foo1 matches Binary file foo2 matches Binary file foo3 matches Binary file foo4 matches The log is created, but remains empty. ... (1 Reply)
Discussion started by: chipperuga
1 Replies

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

5. Shell Programming and Scripting

Redirect lines to another file

Hi, I have a file with a size of 10 mb and I need to redirect some specific lines to a new files. For eg. Executed Restore for 11227.EDCS.551.01.201110 from /tmp/bk/restore/CR81500/content/S24U15VA2.2010-10-29.16:49.EDT/ArchiveFile_11227.EDCS.551.01.201110.zip Operation output: <U+FEFF>Oct... (4 Replies)
Discussion started by: gsiva
4 Replies

6. UNIX and Linux Applications

How to redirect grep command output to same file

Hi Everyone, Can anyone please tell me, how can I redirect the grep command output to same file. I am trying with below command but my original file contains no data after executing the command. $grep pattern file1 > file1 Kind Regards, Eswar (5 Replies)
Discussion started by: picheswa
5 Replies

7. Shell Programming and Scripting

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.... (10 Replies)
Discussion started by: chssastry
10 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. Shell Programming and Scripting

Redirect grep output into file !!!!!

Hi, I am writing the following code in command prompt it is working fine. grep ',222,' SAPPCO_20080306.CSV_old > SAPPCO_20080306.CSV_new But the command is not working in the Shell Script... ########################################## #!/bin/sh #... (2 Replies)
Discussion started by: hanu_oracle
2 Replies

10. UNIX for Dummies Questions & Answers

How to redirect duplicate lines from a file????

Hi, I am having a file which contains many duplicate lines. I wanted to redirect these duplicate lines into another file. Suppose I have a file called file_dup.txt which contains some line as file_dup.txt A100-R1 ACCOUNTING-CONTROL ACTONA-ACTASTOR ADMIN-AUTH-STATS ACTONA-ACTASTOR... (3 Replies)
Discussion started by: zing_foru
3 Replies
Login or Register to Ask a Question