print pattern between two variables awk sed

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting print pattern between two variables awk sed
# 1  
Old 08-15-2011
print pattern between two variables awk sed

I am trying to print text between two variables in a file

I have tried the following things but none seem to work:
awk ' /'$a'/ {flag=1;next} /'$b'/{flag=0} flag { print }' file

and also
sed  "/$a/,/$b/p" file

But none seem to work

Any Ideas?
Thanks in Advance

Last edited by Franklin52; 08-15-2011 at 03:39 PM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 08-15-2011
awk ' /'"$a"'/ {flag=1;next} /'"$b"'/{flag=0} flag { print }' file

This User Gave Thanks to yazu For This Post:
# 3  
Old 08-15-2011
Thanks Yazu
This works!!!
# 4  
Old 08-15-2011
i guess, you need to use -v in awk for define the variable and its value and use inside the awk
# 5  
Old 08-15-2011
Hi itkamaraj can you give an example of how to use -v in awk?
# 6  
Old 08-15-2011
Like this:
awk  -va="$a" -vb="$b" 'a {flag=1;next} b {flag=0} flag { print }' file

It's more safe and less cumbersome. Of course the names of these variables (in -vVAR="$SHELL_VAR") may be any.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

2. Shell Programming and Scripting

How to print the lines between the pattern using awk/grep/sed?

Hi, I need a help to search a pattern and print the multiple lines between them. Input file: Tue May 29 12:30:33 EDT 2012:threadWebContainer : 357:com.travimp.hotelierlinks.abba.service.RequestHandler.requestService(String, ITICSDataSet): hotelCancelReservation request: ... (4 Replies)
Discussion started by: aroragaurav.84
4 Replies

3. UNIX for Dummies Questions & Answers

sed print matching pattern

Hi, i have data file like: START1 a b STOP c d START2 e STOP f START3 g STOP When one of the START<count> variable is passed, i should print all lines matching this until the first 'STOP' for example if 'START2' is provided for match, i should get the result as: START2 (1 Reply)
Discussion started by: ysrini
1 Replies

4. Shell Programming and Scripting

To print variables using awk

Can anyone help me with how to print the variable using a awk statement. for i in ` cat serverlist.txt ` ; do my command | awk '{print $1 $2 $i}' done It should print like below but it is not XXXXX YYYYY Servername XXXXX YYYYY Servername XXXXX YYYYY Servername XXXXX YYYYY... (6 Replies)
Discussion started by: rrb2009
6 Replies

5. Shell Programming and Scripting

Text pattern with AWK and variables

How to to pass variable to the below awk command I would like to pass variables to the awk command instead of the constants "a/cc" but I don't know exactly the correct syntax awk '/a/,/cc/' temp1.out file.txt a b s cc g d output a b s (8 Replies)
Discussion started by: ahmedamro
8 Replies

6. Shell Programming and Scripting

Awk/Sed: Search Pattern from file and Print

Hi, I want to search for patterns (from a file) in a file and print the line matching the patterns and the line before it. I have to search for 100s of patterns from a file. Any help with AWK or Sed. Thanks! (2 Replies)
Discussion started by: saint2006
2 Replies

7. Shell Programming and Scripting

sed print all lines after pattern match

HiCan someone show me how to print all lines from a file after a line matching a pattern using sed?Thanks (13 Replies)
Discussion started by: steadyonabix
13 Replies

8. Shell Programming and Scripting

Use to awk to match pattern, and print the pattern

Hi, I know how to use awk to search some expressions like five consecutive numbers, , this is easy. However, how do I make awk print the pattern that is been matched? For example: input: usa,canada99292,japan222,france59664,egypt223 output:99292,59664 (6 Replies)
Discussion started by: grossgermany
6 Replies

9. Shell Programming and Scripting

sed pattern matching or passing variables

I need sed to add a "/>" to the end of a line that contains/starts with <meta. current line is <meta name="keywords" content="kayword 1, kwyword2"> and should be <meta name="keywords" content="kayword 1, kwyword2 " /> i need something like this? find . -name "*.html" -print0 | xargs... (6 Replies)
Discussion started by: sky_rivers
6 Replies

10. Shell Programming and Scripting

pattern matching and print with sed

Hi It is possible with sed to print a pattern within a line matching regexp? So, the line looks like : 19:00:00 blablablabla jobid 2345 <2> the regexp is "jobid 2345" and the pattern is 56434. That the code for find... (2 Replies)
Discussion started by: nymus7
2 Replies
Login or Register to Ask a Question