The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Perl how to move pointer to previous line in a txt file? tqlam Shell Programming and Scripting 5 01-27-2008 05:24 PM
How to retrieve a particular line from a file vinayap UNIX for Dummies Questions & Answers 2 07-11-2007 02:16 PM
how do we retrieve a line from a file in unix lmadhuri Shell Programming and Scripting 2 02-07-2007 08:07 AM
(cont) Retrieve line from a file based on a value in specific column efernandes UNIX for Dummies Questions & Answers 0 01-27-2007 01:00 PM
Retrieve line from a file based on a value in specific column efernandes UNIX for Dummies Questions & Answers 1 01-27-2007 11:04 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-23-2007
paulsew paulsew is offline
Registered User
  
 

Join Date: Nov 2003
Posts: 2
search and retrieve previous line in file

I've got a file with the following layout:

#STMP FSgroup
filename /filesysname1
filestatus 2

#STMP FSstatus
filename /filesysname1
ratio 30

#STMP FSgroup
filename /filesysname1
filestatus 2

#STMP FSstatus
filename /filesysname6
ratio 60

I'm trying to write a SHELL (ksh) script that'll search the file say e.g filename = /filesysname1 grouped under FSstatus and get everthing beneath FSstatus group.

Thus, I should be able search for anygroup, i.e. lines that start with an # and everything below it until the first blank line. I also need to know the group name, i.e. either FSstatus or FSgroup

Look really difficult for me, any help out there

tks
William
  #2 (permalink)  
Old 02-23-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,952
Will this be ok?

Code:
var=filesysname1
awk -F" " 'BEGIN{group=""; x=0} { if( x == 1 ) { print } if( $0 ~ /^#/) { group = $0; next } if( $0 ~ /'$var'/ ) { x=1; print group; print $0} else { x = 0 } }' file
  #3 (permalink)  
Old 02-23-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Quote:
I'm trying to write a SHELL (ksh) script that'll search the file say e.g filename = /filesysname1 grouped under FSstatus and get everthing beneath FSstatus group.
If all the groups are separated by blank line you can use this
Code:
awk -v RS="" -v FS="\n" -v ORS="\n\n" ' $1 ~ "FSstatus" && $0 ~ "/filesysname1" ' file
Quote:
Thus, I should be able search for anygroup, i.e. lines that start with an # and everything below it until the first blank line. I also need to know the group name, i.e. either FSstatus or FSgroup
If all the groups are separated by blank line you can use this to display group with group name "FSstatus"
Code:
awk -v RS="" -v FS="\n" -v ORS="\n\n" ' $1 ~ "FSstatus" ' file
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:03 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0