9.11. Reading specific lines from one or more files.

Print the first line of file1.csv In order to print the first line of the file we first read it using the cat command, and then we select the first line from the previous output using head -n 1.

$ cat file1.csv | head -n 1
"Anonymized ID","Subject Group","HASCONDITION","CONDITION"

Print the first two lines of file1.csv In order to print the first two lines of the file we first read it using the cat command, and then we select those lines from the previous output using head -n 2.

$ cat file1.csv | head -n 2
"Anonymized ID","Subject Group","HASCONDITION","CONDITION"
"B33199522","Group1","0",""

Print the first three lines of file1.csv

$ cat file1.csv | head -n 3
"Anonymized ID","Subject Group","HASCONDITION","CONDITION"
"B33199522","Group1","0",""
"B33199603","Group3","0",""

Print the last line of file1.csv In order to print the last line of the file we first read it using the cat command, and then we select the last line from the previous output using tail -n 1.

$ cat file1.csv | tail -n 1
"B33199522","Group1","0",""

Print the last two lines of file1.csv

$ cat file1.csv | tail -n 2
"C11137159","Group3","9","mTBI"
"B33199522","Group1","0",""

Print the last three lines of file1.csv

$ cat file1.csv | tail -n 3
"B11141503","Group3","0",""
"C11137159","Group3","9","mTBI"
"B33199522","Group1","0",""

Print the last three lines of file1.csv in reverse As we learned previously, the flag -r of tail command can be used to print things in reversed order.

$ cat file1.csv | tail -r -n 3
"B33199522","Group1","0",""
"C11137159","Group3","9","mTBI"
"B11141503","Group3","0",""

Print from the beginning until the second line of file1.csv (same result as printing the first two lines)

$ cat file1.csv | head -n+2
"Anonymized ID","Subject Group","HASCONDITION","CONDITION"
"B33199522","Group1","0",""

Print from the beginning until the third line of file1.csv (same result as printing the first three lines)

$ cat file1.csv | head -n+3
"Anonymized ID","Subject Group","HASCONDITION","CONDITION"
"B33199522","Group1","0",""
"B33199603","Group3","0",""

Print from the second line until the last line of file1.csv

$ cat file1.csv | tail -n+2
"B33199522","Group1","0",""
"B33199603","Group3","0",""
"B11137879","Group1","0",""
"B11144410","Group2 b","0",""
"B11110455","Group2 b","0",""
"B11135291","Group3","0",""
"B11153927","Group1","0",""
"B11177579","Group2 b","0",""
"B11177806","Group1","MD",""
"B11157958","Group3","0",""
"B11110690","Group3","0",""
"B11152799","Group1","0",""
"B11154358","Group1","0",""
"B11110925","Group1","0",""
"B11135291","Group3","9","mTBI"
"B11135072","MISSING","0",""
"B33199603","Group3","0",""
"B11137879","Group1","0",""
"B11110603","Group1","0",""
"B11110927","Group1","0",""
"B11147712","Group1","0",""
"B33191224","Group2 b","0",""
"B11131290","Group2 b","0",""
"B11157974","Group1","0",""
"B33191224","Group2 b","0",""
"B11141503","Group3","0",""
"C11137159","Group3","9","mTBI"
"B33199522","Group1","0",""

Print from the third line until the last line of file1.csv

$ cat file1.csv | tail -n+3
"B33199603","Group3","0",""
"B11137879","Group1","0",""
"B11144410","Group2 b","0",""
"B11110455","Group2 b","0",""
"B11135291","Group3","0",""
"B11153927","Group1","0",""
"B11177579","Group2 b","0",""
"B11177806","Group1","MD",""
"B11157958","Group3","0",""
"B11110690","Group3","0",""
"B11152799","Group1","0",""
"B11154358","Group1","0",""
"B11110925","Group1","0",""
"B11135291","Group3","9","mTBI"
"B11135072","MISSING","0",""
"B33199603","Group3","0",""
"B11137879","Group1","0",""
"B11110603","Group1","0",""
"B11110927","Group1","0",""
"B11147712","Group1","0",""
"B33191224","Group2 b","0",""
"B11131290","Group2 b","0",""
"B11157974","Group1","0",""
"B33191224","Group2 b","0",""
"B11141503","Group3","0",""
"C11137159","Group3","9","mTBI"
"B33199522","Group1","0",""