- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: To get one line and the first in one file
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
Forums
Discussions
Discussions
Discussions
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
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
08-09-2004 10:05 PM
08-09-2004 10:05 PM
I know how to get one line and the next,
#perl -ne '/text to match/ || next;print $_;if(not eof){$_=<>;print}' file
but how I do to get one line and the previous???
Thanks very much!!
Carmen.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-09-2004 10:13 PM
08-09-2004 10:13 PM
Re: To get one line and the first in one file
sed -n -e '/text_to_match/{=;x;1!p;g;$!N;p;D;}' -e h
will give you the previous and subsequent lines.
Pete
Pete
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-09-2004 10:28 PM
08-09-2004 10:28 PM
Re: To get one line and the first in one file
I would like get for exemple:
1
caracol
2
3
4
caracol
5
6
7
caracol
8
9
caracol
If I look for 'caracol', I would like to get
1
caracol
4
caracol
7
caracol
9
caracol
Thanks!!
Carmen.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-09-2004 10:35 PM
08-09-2004 10:35 PM
Re: To get one line and the first in one file
# cat > forum.log
1
caracol
2
3
4
caracol
5
6
7
caracol
8
9
caracol
# sed -n 'N;/caracol/p' forum.log
1
caracol
4
caracol
7
caracol
It will give your prev. line with respect to the pattern to you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-09-2004 11:28 PM
08-09-2004 11:28 PM
Re: To get one line and the first in one file
But if I have this file, the sed doesn't works ok??
#more forum.log
hola
1
hola
6
2
hola
6
3
hola
4
6
7
8
# sed -n 'N;/hola/p' forum.log
hola
1
hola
6
2
hola
hola
4
??
Thanks!,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-10-2004 12:49 AM
08-10-2004 12:49 AM
Solutiontry this
awk '
BEGIN {line=""}
$0 ~ /pattern/ {print line}
{ line = $0 }
' < file
you can arrange the line whith the check and the print to reflect which kind of control you want. In the example it looks for a pattern in all the read line, but you can check only one field delimited by space ($1, $2, $3 etc.) or more of them or .... see man awk ;-))
hope it helps
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-10-2004 02:31 AM
08-10-2004 02:31 AM
Re: To get one line and the first in one file
Carmen,
Please consider using 'search' before entering a new topic, as many 'obvious' questions have been asked an answerred (many times :-) before.
For this case, 'more options' + forum=hpux + words="previous lines" woudl quikly point to:
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=356066
Admittedly I had a hard time finding a reply backthat I recalled having solved a much similar problem:
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=524659
Here is my AWK 'one-liner' from there looking back for 'window' sized W-lines from a search pattern
awk 'BEGIN {W=4} {line[i++%W]=$0} /textt/{for (j=i-W;j
in sticking with your original question and PERL, one possible solution is:
perl -ne 'if (/text to match/) {print "$last$_"} else {$last=$_}' file
hth,
Hein.