printing lines before and after a record


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting printing lines before and after a record
# 1  
Old 09-18-2012
printing lines before and after a record

Hello Everyone,


I want to print out the records after and before a certain record. I am able to figure out how to print that particular record but not the ones before and after. Looking for some advice

Thank you
# 2  
Old 09-18-2012
What OS are you on? Some versions of grep support:
Code:
grep -A 1 -B 1 [pattern]  filename

give you one line before and one line after. Is this what you want?
# 3  
Old 09-18-2012
thanks for the reply jim

im using hp ux 11.3 and the sad part is, i think, grep doesnt support the linux options Smilie
# 4  
Old 09-18-2012
Code:
 
sed -n -e '/pattern/{x;1!p;g;$!N;p;D;}' -e h infile

# 5  
Old 09-18-2012
I don't have a pattern..just a condition.

for eg;

when so and so is true then print the lines above and below it..
# 6  
Old 09-18-2012
Code:
 
awk -f d.awk infile

where d.awk:

Code:
 
#/bin/ksh
{ if ($2 == 105) {
    print lr;
    print $0;
    lr=$0;
    getline;
    print $0;
  } else {
    lr=$0;
  }
}

The example condition in the script is $2==105.
# 7  
Old 09-18-2012
Quote:
Originally Posted by danish0909
I don't have a pattern..just a condition.

for eg;

when so and so is true then print the lines above and below it..
If rdrtx1's suggestion isn't sufficient, stop being vague and specify your condition.

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Help in printing records where there is a 'header' in the first record ???

Hi, I have a backup report that unfortunately has some kind of hanging indent thing where the first line contains one column more than the others I managed to get the output that I wanted using awk, but just wanting to know if there is short way of doing it using the same awk Below is what... (2 Replies)
Discussion started by: newbie_01
2 Replies

2. Shell Programming and Scripting

Selective printing based on matched record

Hi, I have file with thousands of lines somewhat similar to the below 6 lines 06MXXXXXXXXXXXXXXXX 0328 003529 J27300022 MICROSOFT *MSN 06<1 000000001344392 JPN151-85830 MSBILL.INFO F 06<A17087454000328651551 MSBILL.INFO ... (16 Replies)
Discussion started by: dsid
16 Replies

3. Shell Programming and Scripting

Printing the lines that appear in an other file, and the three lines after them

Hi ! I need some help with a script I am writing. I am trying to compare two files, the first file being in this format : Header1 Text1-1 Text1-2 Text1-3 Header2 Text2-1 etc... For each header, I want to check if it appears in the second file, and if it is the case print the header... (4 Replies)
Discussion started by: jbi
4 Replies

4. UNIX for Dummies Questions & Answers

awk script printing each record twice

i have a find command piped to an awk script, I'm expecting it printing the matching record one time but it's doing it twice: the following is my code: find directoryname | awk 'BEGIN { FS="/" } /.*\.$/ || /.*\.$/ { printf "%s/%s\n", $(NF-1), $NF } it gave me the correct output, but just... (2 Replies)
Discussion started by: ymc1g11
2 Replies

5. Shell Programming and Scripting

Printing all lines before a specific string and a custom message 2 lines after

Hello all, I need to print all the lines before a specific string and print a custom message 2 lines after that. So far I have managed to print everything up the string, inclusively, but I can't figure out how to print the 2 lines after that and the custom message. My code thus far is:... (4 Replies)
Discussion started by: SEinT
4 Replies

6. UNIX for Dummies Questions & Answers

syntax for counting & printing record count

Hi I have a complex script which outputs a text file for loading into a db. I now need to enhance this script do that I can issue an ‘lp' command to show the count of the number of records in this file. Can anybody give me the necessary syntax ? (2 Replies)
Discussion started by: malts18
2 Replies

7. Shell Programming and Scripting

printing space after a record in awk

friends, I am running iostat command in linux. Following is the output iostat -d Linux 2.6.18-128.el5 (btovm725.ind.hp.com) 04/16/2010 Device: tps Blk_read/s Blk_wrtn/s Blk_read Blk_wrtn sda 21.49 5.32 255.61 2871221 138010414 sda1 ... (8 Replies)
Discussion started by: achak01
8 Replies

8. Shell Programming and Scripting

AWK record in multiple lines

Hi everyboby this is my problem I Have this input 1111;222 222 2;333 3333;4444 111; 22222;33 33; 444 and I need this output 1111;2222222;3333333;4444 (15 Replies)
Discussion started by: agritur
15 Replies

9. Shell Programming and Scripting

PERL: Extract random record which has 4 lines each

Hi, I have a data file with millions of record (N). Each record was saved in 4 lines. So there are total of NX4 lines in the data file. For Example: Host1 a b c d Host2 e f g h Host3 i j k (2 Replies)
Discussion started by: phoeberunner
2 Replies

10. Shell Programming and Scripting

How to add record separator after certain lines

How to add record separator after certain lines? I am faicing issue where some lines have result as successive line & some are not having. how can I add record separator after every record here is example of data I have (line numbers are not present in data): 1. <enabled="true" name="dSuite1"... (7 Replies)
Discussion started by: sach253
7 Replies
Login or Register to Ask a Question