how to grep a variable pattern

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to grep a variable pattern
# 1  
Old 11-07-2007
how to grep a variable pattern

I have a file that looks like this:
2 xxxxxx
3 xxxxxxx xxx
(tab followed after the line number)

I need to add a range of lines to the file but not the ones existing already. So in a while loop I have a variable named LINE,

I'd need something like
grep "$LINE " file || echo "$line $othercontent" >> file

but it doesn't work; this works:
grep $LINE file || echo "$line $othercontent" >> file

However the pattern here is, say, "34 ", not "34".
How to use variable in grep /sed?
# 2  
Old 11-07-2007
Oooh, regex patterns can be tricky.

There's nothing wrong with using grep "$pat", so I'm guessing that's not really a tab (either in the file or in the pattern string).

I think you can settle the issue with:

grep -qw "^$LINE" file

-q : quiet (the matching lines are not printed)
-w: word-matching (whole words only)

The carat "^" in the pattern will force $LINE to be at the beginning of the line, not just anywhere in the data.
# 3  
Old 11-07-2007
Thanks gus2000 for your response. But this doesn't work (with the variable quoted:
grep -w '^$line' file
(or "^$line")

It works however:
grep -w $line file

^ is nice to have in this case, but again, how to make it all inside '...'? I used \$line & no luck as well.
# 4  
Old 11-07-2007
Well, single-quotes will keep the variable from being expanded. Try:

echo "$LOGIN"
echo '$LOGIN'

Not quite the same! Strange that it doesnt work with the doublequotes. I think you can include the carat outside quotes like this:

grep -w ^$line file

but will probably splatter if "$line" is null.
# 5  
Old 11-07-2007
you are right about the doube/single quoate!!

grep -w "$line" does work
- so does grep -w $line

however, grep -w "^$line" doesn't find the pattern. not sure why

Also, how to remove all the lines from the file with line range, say 1-150?

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep/awk using a begin search pattern and end search pattern

I have this fileA TEST FILE ABC this file contains ABC; TEST FILE DGHT this file contains DGHT; TEST FILE 123 this file contains ABC, this file contains DEF, this file contains XYZ, this file contains KLM ; I want to have a fileZ that has only (begin search pattern for will be... (2 Replies)
Discussion started by: vbabz
2 Replies

2. Shell Programming and Scripting

How to delete all lines before a particular pattern when the pattern is defined in a variable?

I have a file Line 1 a Line 22 Line 33 Line 1 b Line 22 Line 1 c Line 4 Line 5 I want to delete all lines before last occurrence of a line which contains something which is defined in a variable. Say a variable var contains 'Line 1', then I need the following in the output. ... (21 Replies)
Discussion started by: Soham
21 Replies

3. UNIX for Dummies Questions & Answers

Grep -v lines starting with pattern 1 and not matching pattern 2

Hi all! Thanks for taking the time to view this! I want to grep out all lines of a file that starts with pattern 1 but also does not match with the second pattern. Example: Drink a soda Eat a banana Eat multiple bananas Drink an apple juice Eat an apple Eat multiple apples I... (8 Replies)
Discussion started by: demmel
8 Replies

4. Shell Programming and Scripting

Grep lines before a pattern having some other pattern

Hi All, I am trying to fetch lines before a pattern, I got to know about -B flag in grep but we have to pass the number to get those lines before some pattern say (X), now what if I want to get line/s with some other pattern say (Y) before X pattern? How to get about it? please help. Input:... (5 Replies)
Discussion started by: dips_ag
5 Replies

5. Shell Programming and Scripting

sed and awk usage to grep a pattern 1 and with reference to this grep a pattern 2 and pattern 3

Hi , I have a file where i have modifed certain things compared to original file . The difference of the original file and modified file is as follows. # diff mir_lex.c.modified mir_lex.c.orig 3209c3209 < if(yy_current_buffer -> yy_is_our_buffer == 0) { --- >... (5 Replies)
Discussion started by: breezevinay
5 Replies

6. Shell Programming and Scripting

How to Grep than scan line below grep pattern

Hello Colleagues, I have a file that looks like below. 6-12731913-12731913 9230760143480 410018547148230 20131002193434+0500 20131002193434+0500 ;20131002T161031000-, ver: 0 20131009 92220056296730 CC0P abc Core_Context_R1A SMS 6-12726796-12726796... (14 Replies)
Discussion started by: umarsatti
14 Replies

7. Shell Programming and Scripting

Pattern match exclusive return pattern/variable

I have an application(Minecraft Server) that generates a logfile live. Using Crontab and screen I send a 'list' command every minute. Sample Log view: 2013-06-07 19:14:37 <Willrocksyea1> hello* 2013-06-07 19:14:41 <Gromden29> hey 2013-06-07 19:14:42 Gromden29 lost connection:... (1 Reply)
Discussion started by: gatekeeper258
1 Replies

8. Shell Programming and Scripting

Grep the word from pattern line and update in subsequent lines till next pattern line reached

Hi, I have got the below requirement. please suggest. I have a file like, Processing Item is: /data/ing/cfg2/abc.txt /data/ing/cfg3/bgc.txt Processing Item is: /data/cmd/for2/ght.txt /data/kernal/config.klgt.txt I want to process the above file to get the output file like, ... (5 Replies)
Discussion started by: rbalaj16
5 Replies

9. Shell Programming and Scripting

Want to grep for a pattern and display the content above that pattern

Hi, When we have a failure, sometimes we just step restart the job from the next step. Later when we open the log for analysis of the failure, it is becoming difficult to go to the failure part. For eg., if it is a 1000 line log, the failure may be at 500th line. so wat i want to do is, grep... (6 Replies)
Discussion started by: ajayakunuri
6 Replies

10. UNIX for Dummies Questions & Answers

How to grep a particular pattern using a variable

Hi all, I am searching for the files with a pattern which is stored in a variable. eg: the day is taken from the date format into a variable "day" I am trying to search with the pattern in that variable. ls -ltr | grep "$day" I am not getting the filtered output. Please help me... (8 Replies)
Discussion started by: ladtony
8 Replies
Login or Register to Ask a Question