0

I want to search for a String by navigating to a particular line, How to do this in shell scripting?

For example, I have

this is the first line 
this is the Second line
This is the Third line

Now here i would want to look for string "Third" by going to 3rd line.

Any help is appreciated, Thank you.

tshiono
  • 21,248
  • 2
  • 14
  • 22
  • the text in the example is supposed to be line after line, i don't know why its coming in a single line –  Zeeshan Haris Jan 10 '20 at 06:54
  • It is always recommended on SO to show samples of input and samples of expected output in your question, so kindly do always add the same and let us know then. – RavinderSingh13 Jan 10 '20 at 07:16
  • @ZeeshanHaris : Please be more precise when asking: Do you want to know to verify, whether the 3rd line of the file contains this particular string? So the output should simply be a yes/no indication? – user1934428 Jan 10 '20 at 07:30
  • @user1934428 say i have a file with n number of lines in it, i always want to go to line no.3 of it and search for the sub-string, and return it if present –  Zeeshan Haris Jan 10 '20 at 08:59

1 Answers1

2

Try stringing together cat, sed, and grep.

sed '3!d' filename | grep Third

The unnamed or anonymous pipe (|) and redirection (<, >) are powerful features of many shells. They allow one to combine a set of commands to perform a more complex function.

In the case of this question there were two clear steps, 1) Operate on a specific line of a file (e.g. filter a file) 2) Search the output of the filter for a specific string

Recognizing that there were two steps is a strong indicator that two commands will need to be combined. Therefore, the problem can be solved by finding a solution to each step and then combining them in to one command with pipes and redirection.

If you know about the Stream Editor (sed), it may come to your mind when thinking about how to accomplish the first step of filtering the file. If not searching for, "linux get a specific line of a file" this OS question comes up high in the search results.

$ cat tmp.txt
this is the first line
this is the Second line
This is the Third. line

$ sed '3!d' tmp.txt
This is the Third. line

Knowing that grep can be search for lines with the string of interest the next challenge is to figure out how to get the output of sed as the input to grep. The pipe (|) solves this problem.

sed '3!d' filename | grep Third

Example output:

$ sed '3!d' tmp.txt | grep Third
This is the Third. line
$

Another powerful concept in shell scripting is the exit status. The grep command will set the exit status to 0 when a match is found and 1 when a match is not found. The shell stores the exit status in a special variable named $? (for bash). Therefore, one could use the exit status to conditionally determine the next step in the shell script. The example below does not implement conditions (like if, else). The example below shows the exit status value using the echo command.

$ sed '3!d' tmp.txt | grep Third
This is the Third. line
$ echo $?
0
$ sed '3!d' tmp.txt | grep third
$ echo $?
1
$
EricB
  • 468
  • 3
  • 9
  • can you explain what | does in this ? –  Zeeshan Haris Jan 10 '20 at 09:31
  • @EricB : The `cat` is unnecessary here. Just do a `sed ... < filename`. I also would add th `-F` option to grep, in case the OP wants to search one day for a string containing, say, a period. – user1934428 Jan 10 '20 at 13:24
  • 2
    No point to running both `sed` and `grep` (certainly not *three* separate executables, `sed`, `grep` and `cat`) when `awk` can do both parts. `awk 'NR == 3 && /Third/'` if one wants to emit any output only when the 3rd line contains the string `Third` – Charles Duffy Jan 10 '20 at 15:46