1

I am currently trying to evaluate txt-files in a directory using bash. I want to know if the third line of the txt-file matches a certain string. The file starts with two empty lines, then the target string. I tested the following one liner:

if [[ $(head -n 3 a_txt_file.txt) == "target_string" ]]; then echo yes; else echo no; fi

I can imagine that since head -n 3 also prints out the two empty lines, I have to add them to the if condition. But "\n\ntarget_string" and "\n\ntarget_string\n" also don't work.

How would one do this correctly (And I guess it can be done more elegantly as well)?

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
aldorado
  • 4,394
  • 10
  • 35
  • 46
  • 1
    possible duplicate of [bash tool to get nth line from a file](http://stackoverflow.com/questions/6022384/bash-tool-to-get-nth-line-from-a-file) – Boris the Spider Dec 20 '13 at 15:51
  • 1
    `sed -n '3{p;q;}'` should give just the third line. Or use `$'\n\n'"target_string"` to embed two newlines at the start of the string. – Jonathan Leffler Dec 20 '13 at 15:52
  • Or also `awk 'NR==3 && /pattern/' file`. And some `exit` around to avoid keeping reading. – fedorqui Dec 20 '13 at 15:54
  • `\n` is two characters, not a newline, in a quoted string. `bash` does supply a non-standard quoting mechanism, `$'\n\ntarget_string'`, that may work for you though. – chepner Dec 20 '13 at 17:06

5 Answers5

3

If you just need to remove the top two lines:

head -n 3 | tail -1
PaulProgrammer
  • 16,175
  • 4
  • 39
  • 56
3

Try this instead - it will print only the third line:

sed -n 3p file.txt
joews
  • 29,767
  • 10
  • 79
  • 91
  • 6
    That's good, but will read all 4 GiB of a 4 GiB file. You could upgrade it to quit too: `sed -n '3{p;q;}'`, thus not reading 3.999+GiB of the 4 GiB. – Jonathan Leffler Dec 20 '13 at 15:53
3

Besides sed you can try awk to print 3rd line

awk 'NR==3'
ray
  • 4,109
  • 1
  • 17
  • 12
  • 4
    Nice, but to avoid reading the entire file I suggest `awk 'NR==3 { print; exit }'`. – mklement0 Dec 20 '13 at 16:02
  • I modified it to awk. Exiting immediately after 3rd line is faster, especially for big file, thanks for pointing out that. – ray Dec 20 '13 at 16:07
  • My pleasure - I suggest you upvote my commment (hover to the left and click the up arrow - note that I will _not_ gain any reputation from this) - it'll help others spot useful information more quickly. (Apologies if you know all that already.) – mklement0 Dec 20 '13 at 16:21
3

You'll want to use sed instead of head. This gets the third line, tests if it matches, and then you can do whatever you want with it if it does match.

if [[ $(sed '3q;d' test_text.txt ) == "target_string" ]]; then echo yes; else echo no; fi
Sean Lynch
  • 2,852
  • 4
  • 32
  • 46
1

A pure bash solution:

if { read; read; read line; } < test_text.txt
   [[ $line = target_string ]]
then
    echo yes
else
    echo no
fi < test_text.txt

This takes advantage of the fact that the condition of the if statement can be a sequence of commands. First, read twice from the file to discard the empty lines; the third sets line to the 3rd line. After that, you can test it against the target string.

chepner
  • 497,756
  • 71
  • 530
  • 681