- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- script output
Operating System - HP-UX
1822678
Members
3765
Online
109644
Solutions
Forums
Categories
Company
Local Language
юдл
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Forums
Discussions
юдл
back
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
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Go to solution
Topic Options
- 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
тАО10-23-2003 10:52 PM
тАО10-23-2003 10:52 PM
Hi,
Ive been having this problem doing a script that when it encounters xxx in a particular line it prints n number of line below it. Hope you can help me thanks.
Example cat myfile
xxx
asdfghjkl
aaa
poiuyuiyequye
bbb
opfdpop[o[pop[o
xxx
qwerty
poiu
pdiofgshf
produce output when n=1 (1 line below it)
asdfghjkl
qwerty
produce output when n=2 (print 2 lines below it)
asdfghjkl
aaa
qwerty
poiu
Ive been having this problem doing a script that when it encounters xxx in a particular line it prints n number of line below it. Hope you can help me thanks.
Example cat myfile
xxx
asdfghjkl
aaa
poiuyuiyequye
bbb
opfdpop[o[pop[o
xxx
qwerty
poiu
pdiofgshf
produce output when n=1 (1 line below it)
asdfghjkl
qwerty
produce output when n=2 (print 2 lines below it)
asdfghjkl
aaa
qwerty
poiu
Solved! Go to Solution.
3 REPLIES 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-23-2003 11:03 PM
тАО10-23-2003 11:03 PM
Solution
You could use awk, and pass in the number of lines to print, as follows.
awk -v lines=1 '
/xxx/ {
for (i=1;i<=lines;i++) {
getline
print
}
} ' myfile
--
Use lines=1, lines=2, or whatever.
/xxx will match "xxx" anywhere on the line, if you only want beginning of line, use /^xxx/
--
Graham
awk -v lines=1 '
/xxx/ {
for (i=1;i<=lines;i++) {
getline
}
} ' myfile
--
Use lines=1, lines=2, or whatever.
/xxx will match "xxx" anywhere on the line, if you only want beginning of line, use /^xxx/
--
Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-23-2003 11:09 PM
тАО10-23-2003 11:09 PM
Re: script output
Ferdie,
#!/usr/bin/ksh
# Filename: script.sh
awk -v string=$1 -v lines=$2 '{
tmplines=lines;
if ($0 ~ string)
{
while (tmplines-- > 0)
{
getline;
print $0;
}
}
}' /tmp/a.txt
# where /tmp/a.txt is your text file with the strings.
# eof
Call this as follows -
# script.sh "xxx" 1
asdfghjkl
qwerty
# script.sh "xxx" 2
asdfghjkl
aaa
qwerty
poiu
HTH.
- ramd.
#!/usr/bin/ksh
# Filename: script.sh
awk -v string=$1 -v lines=$2 '{
tmplines=lines;
if ($0 ~ string)
{
while (tmplines-- > 0)
{
getline;
print $0;
}
}
}' /tmp/a.txt
# where /tmp/a.txt is your text file with the strings.
# eof
Call this as follows -
# script.sh "xxx" 1
asdfghjkl
qwerty
# script.sh "xxx" 2
asdfghjkl
aaa
qwerty
poiu
HTH.
- ramd.
HPE Software Rocks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-25-2003 09:16 AM
тАО10-25-2003 09:16 AM
Re: script output
A much similar problem was recently asked and answered in:
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=237288
My reply there adapted (but untested) for your problem:
awk '//{i=; while (i--) {getline; print $0}}' <
or PERL (assuring a re-start every occurence:
cat | perl -e 'while (<>){print if ($i-->0); $i= if (//)}'
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=237288
My reply there adapted (but untested) for your problem:
awk '/
or PERL (assuring a re-start every occurence:
cat
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
Company
Learn About
News and Events
Support
© Copyright 2025 Hewlett Packard Enterprise Development LP