1

I have a text file in which thousands of data is there. eg.

id=row_value
profile=row_value
name=row_value
email=row_value
address=row_value

id=row_value2
profile=row_value2
name=row_value2
email=row_value2
address=row_value2

.       .       .
.       .       .
.       .       .

I want to Extract all name and email from that file. eg.

name=row_value
email=row_value

name=row_value2
email=row_value2

.       .       .
.       .       .
.       .       .

NOTE : Note the new line after each name and email pairs ..

I Know it will be done in some linux commands like grep, sed, awk etc .. I'm Noob in linux commands and please help me regarding this ..

1

2 Answers 2

4

Try this

grep "name\|email" <Path to your File> 
5
  • great ... but how to append a new blank line after name=row_value2 email=row_value2 .. thanx for ur help Aug 7, 2014 at 12:32
  • 1
    grep '^name\|^email\|^$' file will preserve the empty lines as well Aug 7, 2014 at 12:37
  • it is not working .. @steeldriver Aug 7, 2014 at 13:04
  • @user3710243 is your file exactly as shown in your original post? Aug 7, 2014 at 13:30
  • ... what I mean is, if the "blank" lines actually contain whitespace, you will need to change the last group from ^$ to something like ^\s*$ Aug 7, 2014 at 13:36
0

To keep the conscience clean, here's an awk version:

$ awk '$0~/name/||/email/ {if ($0~/email/) $3="\n"; print $0}' testerfile.txt 
name=row_value
email=row_value  

name=row_value2
email=row_value2  

And this one, will append the dots:

awk '$0~/name/||/email/ {if ($0~/email/) $3="\n"; print $0}; $0~/^.[[:blank:]].[[:blank:]]./ {print} ' testerfile.txt

Output:

$awk '$0~/name/||/email/ {if ($0~/email/) $3="\n"; print $0}; $0~/^.[[:blank:]].[[:blank:]]./ {print} ' testerfile.txt 
name=row_value
email=row_value  

name=row_value2
email=row_value2  

.       .       .
.       .       .
.       .       .

A much simpler version of the first version:

awk '/^name/; /^email/ {$3="\n"; print}' testerfile.txt

Anchored regex will prevent for finding words "name" and "email" elsewhere in the text, and only in the beginning of a line

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .