- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- using sed or grep print 5 lines of context before ...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-21-2005 09:38 PM
тАО04-21-2005 09:38 PM
using sed or grep print 5 lines of context before and after regexp
Using sed or grep, print 5 lines of context before and after regexp in hp ux.
As -A option is not available in hp ux.
rgds
Ankur
- Tags:
- grep
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-21-2005 09:55 PM
тАО04-21-2005 09:55 PM
Re: using sed or grep print 5 lines of context before and after regexp
# print 1 line of context before and after regexp, with line number
# indicating where the regexp occurred (similar to "grep -A1 -B1")
sed -n -e '/regexp/{=;x;1!p;g;$!N;p;D;}' -e h
Pete
Pete
- Tags:
- sed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-21-2005 10:32 PM
тАО04-21-2005 10:32 PM
Re: using sed or grep print 5 lines of context before and after regexp
i do have that , but how do i have 5 lines before the regex.
rgds
Ankur
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-21-2005 11:47 PM
тАО04-21-2005 11:47 PM
Re: using sed or grep print 5 lines of context before and after regexp
what grep do you use?
With gnu grep it's:
grep -A5 -B5 data file.lis
Regards
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-22-2005 12:25 AM
тАО04-22-2005 12:25 AM
Re: using sed or grep print 5 lines of context before and after regexp
As of now, HP-UX grep(1) doesn't support the context printing like 5 lines + and -.
It will be sometime before HP supports such an option.
BTW, which version of HP-UX you are using?
Regards,
Anupam Anshu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-22-2005 12:45 AM
тАО04-22-2005 12:45 AM
Re: using sed or grep print 5 lines of context before and after regexp
I've been trying to decypher exactly how the example I gave you works and it appears to me that there's no way to go back 5 lines. It just swaps current and previous, meaning it can only go back 1 line.
I guess gnu grep would be the answer then:http:
//hpux.cs.utah.edu/hppd/hpux/Gnu/grep-2.5.1/
Pete
Pete
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-22-2005 04:24 AM
тАО04-22-2005 04:24 AM
Re: using sed or grep print 5 lines of context before and after regexp
http://www.cpan.org/scripts/text-processing/cgrep
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-22-2005 06:56 AM
тАО04-22-2005 06:56 AM
Re: using sed or grep print 5 lines of context before and after regexp
number of lines to print (5 in your example), input
file name and search pattern at set at the top so
that you can change them. Consider modifying the
script to use command line parameters. The script
will print 11 lines if pattern matches (i.e 5 previous
and 5 following lines and the line itself that matches.
NOTE : The script does not behave nicely if the
patter matches first or last 5 lines. Modifying the
script to do that is left as an exercise to original
poster ;-)
------ Start -----
#!/usr/bin/ksh
# Set the following parameters
integer LINES_TO_PRINT=5
INPUT_FILE_NAME="testfile"
SEARCH_PATTERN="stu"
# Executation starts from here
LINES_TO_READ=$(echo \
"2*$LINES_TO_PRINT+1" | bc )
exec 3<$INPUT_FILE_NAME
integer i=1
while [ /bin/true ]
do
if [ $i -gt $LINES_TO_READ ]
then
break
fi
read -ru3 line[$i]
i=$i+1
done
integer MIDDLE_LINE=$LINES_TO_PRINT+1
while [ /bin/true ]
do
integer j=1
echo ${line[$MIDDLE_LINE]} | grep -q \
$SEARCH_PATTERN
if [ $? -eq 0 ]
then
while [ $j -le $LINES_TO_READ ]
do
echo ${line[$j]}
j=$j+1
done
fi
j=1
while [ $j -lt $LINES_TO_READ ]
do
line[$j]=${line[$j+1]}
j=$j+1
done
read -ru3 line[$j]
if [ $? -ne 0 ]
then
exit
fi
done
- Biswajit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-22-2005 08:46 PM
тАО04-22-2005 08:46 PM
Re: using sed or grep print 5 lines of context before and after regexp
the attached script may do it: try running it like this:
# print_before_after.sh "a quoted string" 5
The script prints out line numbers as well. To to avoid that, change the printing line (looks almost like the one below!) to:
cat -n $3 | awk -v lineb=$LINE_BEFORE -v linea=$LINE_AFTER '$1==lineb,$1
==linea {$1="";print $0}'
regards,
John K.
- Tags:
- cat
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-24-2005 04:15 PM
тАО04-24-2005 04:15 PM
Re: using sed or grep print 5 lines of context before and after regexp
will be tryin all ur scripts ( debugging them to know them better)
Will do whatever i can to help u all.
thanks once again.
rgds
Ankur
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-24-2005 10:25 PM
тАО04-24-2005 10:25 PM
Re: using sed or grep print 5 lines of context before and after regexp
rgds
Ankur