3

I need to read a text file line by line and the lines are not constant it may vary.For example

$ cat file1.txt
syntax error at or near "?" at character 1
syntax error at or near "[" at character 1
syntax error at or near "," at character 1
syntax error at or near "desc" at character 1
syntax error at or near "describtion" at character 1 `

After reading that line it must get automatically passed to the grep command and give me the result set. Since the number of lines & errors captured in the text file is not predictable. Everything needs to be done automatically

$ find file1.txt | xargs grep 'syntax error at or near "?" at character 1' | tail -1

How can I put the 2nd line,3rd line...nth line & automate it in the shell script.

  • 1
    When doing line-by-line operations, awk is often the best tool for the job. – HalosGhost Jul 24 '14 at 12:00
  • I wrote an answer but deleted because I'm a little dubious about why grep doesn't work...? – mikeserv Jul 24 '14 at 12:20
  • 1
    I'm not sure I understand the question. Do you mean you want to pass certain line numbers only to grep? – Joseph R. Jul 24 '14 at 12:38
  • @JosephR. Yeah you are right..I need to pass each line only to the grep – user3359124 Jul 24 '14 at 12:47
  • see - that's what's left me dubious - it's usually grep that passes the lines. – mikeserv Jul 24 '14 at 12:50
1

You can use read command to read the file line by line. For example:

while read -r line
 do 
 [[ $line = 'syntax error at or near "?" at character 1' ]] &&  printf '%s\n' "$line"
 done < file1.txt
1

If I understand you correctly, you want grep to operate on lines 1 to n only. For this purpose, you can use the head tool:

head -n 20 your_file | grep ...

This will make grep operate on the first 20 lines of your_file. Replace 20 with the n relevant to you.

An alternative interpretation and answer

It just occurred to me that you might mean that file1.txt contains patterns that would like grep to try and match. If that is the case, you want the -f option to grep:

grep -f file1.txt files_you_want_to_search

The -f option tells grep to obtain its patterns from file1.txt where it assumes one pattern per line (as you show in your post). In such cases, you may also want to add the -F option to ensure that any regex metacharacters in the file aren't given their special meaning but rather interpreted literally:

grep -Ff file1.txt files_you_want_to_search
  • Still you didn't get me correctly. What i need is a text file contains n no of rows,may be tomorrow it can be n+1 or n+10.But my script should read each line individually irrespective of number of rows it has. Also each & every row should pass to the grep command and give you the result set. So number of rows & the type of errors captured in the text file is not constant, it varies..So the script should automatically do the process Thanks again – user3359124 Jul 24 '14 at 13:02
  • 1
    @user3359124 By no. of rows, do you mean number of lines? If so, that's what grep -f ... does. It interprets file1.txt as containing many patterns with one pattern per line and it tries to run the match against each pattern in turn. – Joseph R. Jul 24 '14 at 13:08
  • Yeah i agree with you but as you mentioned in the codings files_you_want_to_search must be done automatically through a script, we can't manually keep on putting the lines in the grep command & check. – user3359124 Jul 24 '14 at 13:15
  • @user3359124 Well that can be accomplished via find <pattern> -exec grep -Ff file1.txt' {} + or similar as you have originally posted. – Joseph R. Jul 24 '14 at 13:18
1

Based on your clarifications in various comments, it sounds like you should be using grep's -f option to read a list of patterns directly from the file

   -f FILE, --file=FILE
          Obtain  patterns  from  FILE,  one  per  line.   The  empty file
          contains zero patterns, and therefore matches nothing.   (-f  is
          specified by POSIX.)

You can add -F to force interpretation as literal ('fixed') strings unless the file contains regular expression patterns i.e.

grep -Ff file_of_lines file_to_search_for_those_lines

The result will be a list of all the lines in file_to_search_for_those_lines that match any of the strings in file_of_lines.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.