LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-20-2012, 02:46 AM   #1
pradeepdee6
Member
 
Registered: Jul 2012
Posts: 30

Rep: Reputation: Disabled
Printing all the lines Before the match


Hi,

Im trying to print all the lines before the matching lines.

Conditions i have are:
1. Dont know how many lines i have to print. So cant use grep -B option :-(
2. should use variable to store the pattern.

I tried using sed, But sed doesnot recognize variable as it needs string as pattern.

Please help me.

Thanks In advance :-)
 
Old 07-20-2012, 03:23 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Without an actual example of the in and output you want/need I'm forced to give a general answer. This should work:
Code:
SEARCHSTRING="string"
sed -n "1,/${SEARCHSTRING}/p" infile
 
1 members found this post helpful.
Old 07-20-2012, 03:39 AM   #3
pradeepdee6
Member
 
Registered: Jul 2012
Posts: 30

Original Poster
Rep: Reputation: Disabled
Smile Thank you !! Its working :-)

Thanku very much for the answer. It has solved my Issue. It was very helpful..
 
Old 07-20-2012, 06:42 AM   #4
pradeepdee6
Member
 
Registered: Jul 2012
Posts: 30

Original Poster
Rep: Reputation: Disabled
Hi Druuna,

Thanks for the code, But the code is printing even the matching pattern.

Ex:
abc1.txt
rgc1.txt
dfc1.txt
vdc1.txt
adf1.txt
drc1.txt
pres.txt

I want it to print all the lines before the pattern.

SEARCHSTRING="vdc1.txt"
sed -n "1,/${SEARCHSTRING}/p" infile


I want to print:
abc1.txt
rgc1.txt
dfc1.txt


Can you please help me.

Thanks in advance..
 
Old 07-20-2012, 08:07 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Do you understand how that code works?---If so, you can modify it to do what you need.

The variation is to tell sed to print everything, but to DELETE all lines in a range---e.g.:
Code:
sed '/<pattern>,$/d' filename
sed '/<pattern>/,$d' filename

Last edited by pixellany; 07-20-2012 at 08:37 AM. Reason: Corrected code
 
1 members found this post helpful.
Old 07-20-2012, 08:23 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Minor correction to pixellany's code:
Code:
sed '/<pattern>/,$d' filename
 
1 members found this post helpful.
Old 07-20-2012, 08:35 AM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
OOPS---yes: search strings inside the //, line numbers and things like end of file ($) outside
 
Old 07-20-2012, 01:03 PM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Please use ***[code][/code] tags*** around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, bolding, colors, or other fancy formatting.


Another option in sed is to use a nested command and eliminate that last, unwanted line.

Code:
searchstring="string"
sed -n "1,/$searchstring/ { /$searchstring/d ; p}" infile
(Since environment variables are generally all upper-case, it's good practice to keep your own user variables in lower-case or mixed-case to help differentiate them.)


Here are a few useful sed references:
http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/grabbag/
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt


Yet another option is to use ed, which has a useful negative line indexing ability:
Code:
printf '%s\n' "1,/${searchstring//\//\/}/-1p" | ed -s infile
It will fail if the matching string occurs on the first line, however.

Notice that I also added a change to the variable. If the string happens to contain a "/", then it will conflict with the matching delimiter. The "${SEARCHSTRING//\//\/}" parameter substitution will add backslashes to any that exist, making it safe for all strings.

How to use ed:
http://wiki.bash-hackers.org/howto/edit-ed
http://snap.nlc.dcccd.edu/learn/nlc/ed.html
(also read the info page)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
delete lines between match csegau Programming 15 05-16-2011 11:25 AM
diplay all lines before a match is found csegau Programming 3 09-09-2010 01:08 PM
sed match last x lines of a file bradvan Programming 12 03-19-2009 11:18 PM
REGEXP Match * through multiple lines ? ALInux Linux - Software 12 08-14-2007 07:39 AM
printer printing vertical lines at beginning and end of lines makhand Linux - Hardware 0 09-02-2005 02:03 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration