Output block of lines in a file based on grep result


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Output block of lines in a file based on grep result
# 1  
Old 03-04-2013
Output block of lines in a file based on grep result

Hi I would appreciate your help with this.
I have a output file from a command. It is broken based on initial of the users. Exmaple of iitials MN & SS. Under each section there is information pertaining to the user however each section can have different number of lines. MY challenge is to
Grep for a particular initials ( Example MN ) and print all the lines under this initial
and put them in to a new file. The problem is that uder each initial there could be any number of lines. So the number of lines per initial is not fixed. How can I do this? I put sample of input and output files.


Input File name >> myfile.txt
Content of the file

MN
11
22
33

SS
11
22
33
44

MN
44
55

SS
55
66
77
OUTPUT File >> Should look like this

MN
11
22
33

MN
44
55
# 2  
Old 03-04-2013
How about this, using awk:

Code:
awk '$1=="MN"' ORS="\n\n" RS= infile

This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 03-04-2013
Thanks! Worked like a charm
I need to figure out what these options do
so I can use it again
# 4  
Old 03-04-2013
RS is record separator, when it is blank an empty line is used a the record separator ORS it the output record separator "\n\n" causes the output to also have a blank line (two newline chars) between records
# 5  
Old 06-29-2013
Get the output

Hi All,

Can anyone please let me know how to achieve the below output.
My input file is like :

[Name1]
Address=abc
mob=1234

[Name2]
Address=bcd
mob=5678

Now from command line i need to supply [Name1] or [Name2] and according to that my i want the output. That means if input is [Name1] than output will be :
Address=abc
mob=1234

I have used the below commands :
awk '$1=="[Name1]"' ORS="\n\n" RS= input_file.txt

But when i am trying the below mentioned steps than I am not getting the output.
#!/bin/sh

value=Name1
name1_addr=`awk '$1=="[$value]"' ORS="\n\n" RS= input_file.txt|grep "^Address"|cut -d'=' -f2`
echo $name1_addr


Can anyone help me to resolve this issue ?

Thanks,
Monu

---------- Post updated 06-30-13 at 12:25 AM ---------- Previous update was 06-29-13 at 11:45 PM ----------

I have got the answer to my above question.
# 6  
Old 06-29-2013
A general solution with a state variable
Code:
awk '$1~/^\[/ {s=0} s==1 {print} $1=="["key"]" {s=1}' key="$value" input_file.txt

One can simply add a sub key
Code:
awk -F= '/^\[/ {s=0} s==1 && $1==subkey {print $2} $0=="["key"]" {s=1}' key="$value" subkey="Address" input_file.txt


Last edited by MadeInGermany; 06-29-2013 at 10:38 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search a multi-line shell command output and execute logic based on result

The following is a multi-line shell command example: $cargo build Compiling prawn v0.1.0 (/Users/ag/rust/prawn) error: failed to resolve: could not find `setup_panix` in `human_panic` --> src/main.rs:14:22 | 14 | human_panic::setup_panix!(); | ... (2 Replies)
Discussion started by: yogi
2 Replies

2. Shell Programming and Scripting

Grep output to file result not as expected

Hi Gurus, I run command grep ABC file1 > file2 against below file. I got all ABC_xxx in one line in file2. I expect to get multiple lines in file2. If I print result in screen, the result is expected. thanks in advance My os is SunOS 5.10 Generic_150400-64 sun4v sparc sun4v ABC_123 XXXXX... (2 Replies)
Discussion started by: green_k
2 Replies

3. UNIX for Dummies Questions & Answers

Grep -B used with -f? (Searching a file using a list of terms, output is lines before each match)

(1 Reply)
Discussion started by: Twinklefingers
1 Replies

4. Shell Programming and Scripting

How to sort grep result based on timestamp?

Hi, Trying to sort grep result based on timestamp of the filename. I have the following result and want to sort them on timestampgrep -i 'ERROR' *log*2013* s_m_xxx_xxx_xxx_xxx_xxxx.log.20130906092431:TRANSF_1_1_1> DBG_21216 Finished transformations for Source Qualifier . Total errors ... (5 Replies)
Discussion started by: bobbygsk
5 Replies

5. Shell Programming and Scripting

Read 2 lines from File, Run Command based off output

Okay, so I have a file containing line after line of three digit numbers. I need a script that does an action based on the last two numbers in this list. So.... To get the last two numbers, I can have the script do tail -2 filename.txt But where I run into trouble is as follows. If... (6 Replies)
Discussion started by: UCCCC
6 Replies

6. Shell Programming and Scripting

Selecting awk output depending on grep result

Hi, I don't script often enough to know how to do this, and I can't seem to find a right example online. I have a csv output from an old, old system (Win2K???), from which I want to extract only certain fields. Initially I came up with something like this: cat file1 | awk -F '"' '{print $8... (7 Replies)
Discussion started by: takada
7 Replies

7. Shell Programming and Scripting

Extracting specific lines of data from a file and related lines of data based on a grep value range?

Hi, I have one file, say file 1, that has data like below where 19900107 is the date, 19900107 12 144 129 0.7380047 19900108 12 168 129 0.3149017 19900109 12 192 129 3.2766666E-02 ... (3 Replies)
Discussion started by: Wynner
3 Replies

8. Shell Programming and Scripting

Grep between block of lines in a file

Hi Friends, I have a file which has many of the statements like below ******** MAKING > noun1 < cg_all statements statements statements ********* MAKING > noun2 < cg_all statements statements statements ********* MAKING > noun3 < all statements statements statements I would... (3 Replies)
Discussion started by: ganga.dharan
3 Replies

9. Shell Programming and Scripting

how to get surrounding lines of grep result

hi, if i have a file and i want to search for the word error using grep, i usually want to see the surrounding lines too as they contain info about the error. what would be a nice way to achieve this? thanks (6 Replies)
Discussion started by: JamesByars
6 Replies

10. Shell Programming and Scripting

Outputting formatted Result log file from old 30000 lines result log<help required>

Well I have a 3000 lines result log file that contains all the machine data when it does the testing... It has 3 different section that i am intrsted in 1) starting with "20071126 11:11:11 Machine Header 1" 1000 lines... "End machine header 1" 2) starting with "20071126 12:12:12 Machine... (5 Replies)
Discussion started by: vikas.iet
5 Replies
Login or Register to Ask a Question