How to get the contents of a line by its number in shell

0

I need to get the contents of a line by its number I heard of grep and researched the whole forum but got no luck what is the command? E.g I enter a cmd like 'viewlinecontent linenumber file.php' and i will get the line contents.

Derek nichols

Posted 2018-02-13T20:28:56.723

Reputation: 1

Answers

0

sed works well here:

linenumber=42  # or whatever
sed -n "${linenumber}p" file

use -n to prevent printing every line.
${linenumber} specifies the "address range" (a single line), and
p to print that line.

sed manual online


If you want your specific syntax, create a shell function:

viewlinecontent() {
    sed -n "${1}p" "$2"
}
viewlinecontent 42 file.php

glenn jackman

Posted 2018-02-13T20:28:56.723

Reputation: 18 546

Someday I need to come to terms with sed... – Frank Thomas – 2018-02-13T20:53:48.670

sed has cryptic one-character commands, no variables, no arithmetic, etc. Most things can be done more readably/maintainably in awk or perl. I only use sed for very short filters: sed q == head -1; sed -n '$p' == tail -1; sed 's/search/replace/g'; sed -i 's/\r$//' == dos2unix – glenn jackman – 2018-02-13T21:17:51.843