Grep with special chars line by line


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Grep with special chars line by line
# 1  
Old 05-15-2014
Grep with special chars line by line

I have a file that includes strings with special characters, eg
file1
Code:
line:[] 1 - special 1
line: = 4
line; -3

etc

How can I grep the lines of file1 from file2, line by line?

I used fgrep and egrep to grep a particular line and worked fine, but when I used:
Code:
cat file1|while read line;do egrep $line file2

then I get errors.
Is there a way to do this properly?

Last edited by Don Cragun; 05-15-2014 at 02:57 PM.. Reason: Change QUOTE tags to CODE tags.
# 2  
Old 05-15-2014
put double quotes around $line in grep
Code:
cat file1|while read line;do egrep "$line" file2; done

# 3  
Old 05-15-2014
Quote:
Originally Posted by FelipeAd
I have a file that includes strings with special characters, eg
file1
Code:
line:[] 1 - special 1
line: = 4
line; -3

etc

How can I grep the lines of file1 from file2, line by line?

I used fgrep and egrep to grep a particular line and worked fine, but when I used:
Code:
cat file1|while read line;do egrep $line file2

then I get errors.
Is there a way to do this properly?
I can't figure out what you're trying to do.

If you are trying to look for fixed strings in a file when some of the strings you're looking for contain characters that are special in basic regular expressions (BREs) or extended regular expressions (EREs), then you need to use fgrep (or grep -F) and quote your arguments properly.

If you are trying to look for strings in a file matching BREs or EREs, then you need to use grep or egrep (or grep -E), respectively; quote your arguments proper; and modify the text in file1 to contain valid BREs or EREs, respectively. The text in the first line in file1 is not a valid BRE and is not a valid ERE.

SriniShoo already showed you how to properly quote your arguments and added the done needed to end your while loop. But, egrep won't work with your file1 contents. And, there is no reason to waste resources using cat to unnecessarily read and write the contents of file1 just so read can read the contents again.
Code:
while read line
do      fgrep "$line" file2
done < file1

or the single line equivalent:
Code:
while read line;do fgrep "$line" file2;done < file1

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell script to split data with a delimiter having chars and special chars

Hi Team, I have a file a1.txt with data as follows. dfjakjf...asdfkasj</EnableQuotedIDs><SQL><SelectStatement modified='1' type='string'><! The delimiter string: <SelectStatement modified='1' type='string'><! dlm="<SelectStatement modified='1' type='string'><! The above command is... (7 Replies)
Discussion started by: kmanivan82
7 Replies

2. Shell Programming and Scripting

Find Special/Null/Control Chars and Print Line Numbers

Hi All, This might be a basic question... I need to write a script to find all/any Speacial/Null/Control Chars and Print Line Numbers from an input file. Output something like Null Characters in File Name at : Line Numbers Line = Print the line Control Characters in File Name at : Line... (2 Replies)
Discussion started by: Kevin Tivoli
2 Replies

3. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

4. Shell Programming and Scripting

why am I losing line-end chars

Hello, I do not know why the output from these two methods differs. One method retains the newlines, the other method appears to ignore or lose the newlines. Writing a file with the redirection operator: egrep -e 'matchstring' infile.txt > outfile.txt The resulting outfile.txt contains... (3 Replies)
Discussion started by: duderonomy
3 Replies

5. Shell Programming and Scripting

sed - how to insert chars into a line

Hi I'm new to sed, and need to add characters into a specific location of a file, the fileds are tab seperated. text <tab> <tab> text <tab> text EOL I need to add more characters to the line to look like this: text <tab> <tab> newtext <tab> text <tab> text EOL Any ideas? (2 Replies)
Discussion started by: tangentviper
2 Replies

6. Shell Programming and Scripting

cat file1 read line-per-line then grep -A 15 lines down in fileb

STEP 1 # Set variable FILE=/tmp/mainfile SEARCHFILE =/tmp/searchfile # THIS IS THE MAIN FILE. cat /tmp/mainfile Interface Ethernet0/0 "outside", is up, line protocol is up Hardware is i82546GB rev03, BW 100 Mbps Full-Duplex(Full-duplex), 100 Mbps(100 Mbps) MAC address... (6 Replies)
Discussion started by: irongeekio
6 Replies

7. Shell Programming and Scripting

find 4 chars on 2nd line, 44 chars over

I know this should be simple, but I've been manning sed awk grep and find and am stupidly stumped :( I'm trying to use sed (or awk, find, etc) to find 4 characters on the second line of a file.txt 44-47 characters in. I can find lots of sed things for lines, but not characters. (4 Replies)
Discussion started by: unclecameron
4 Replies

8. Shell Programming and Scripting

insert new line at found chars

Hey gang, I have: XXZZXXZZXX 123 asdaffggh dfghyrgr ertyhdhh XXZZXXZZXX 234 sdg XXZZXXZZXX 456 gfg fggfd That is all on one line. Very simply put I want to do is something like: sed s'/XXZZXXZZXX /\n/g' or tr 'XXZZXXZZXX ' '/n' I have tried various things but can never get the desired... (6 Replies)
Discussion started by: crowman
6 Replies

9. Shell Programming and Scripting

Get multiple line content between two chars

Hello - I have a file that has the something like the following : REM CREATE TABLE lots of text REM table specifc creation text ; REM ALTER TABLE lots of text REM text specific to the the alter command REM could be more lines of text; What I need is to get all the lines for the ALTER... (2 Replies)
Discussion started by: Feliz
2 Replies

10. Shell Programming and Scripting

Using PERL to append chars to each line

I tried using SED to do this, but I'm not having any luck with it. See the previous thread here. I have a program called AMStracker (on OS X) that spits out the values of the motion sensor in the HDD. It has output that looks like this: . . 3 0 -75 3 0 -76 3 0 -77 . . I need to... (5 Replies)
Discussion started by: c0nn0r
5 Replies
Login or Register to Ask a Question