In Linux, how can I extract a particular line from a text file and put it into a variable?
Ivan A
2010-03-02 09:40:29 UTC
Say I have a text file and I need to put a particular line as a variable in a script. What is the command that would do so? Thanks.
Three answers:
Pete S
2010-03-02 10:01:03 UTC
use sed
#!/bin/sh
vara=`sed -n "10 p" index.html`
Brian
2010-03-02 10:45:27 UTC
Depends what you mean by "a particular line". If you want a number, say the 10th line, then use 'sed' as Pete suggests. If you want a line that matches a certain pattern, use grep. You can also use awk.
LN=`grep pattern filename`
or
LN=`grep pattern filename | head -1`
to just get the first one
or
LN=`awk '$1 == "pattern" ' '{print $2}' filename`
if you only want the second field of the line
There's lots of ways to do it.
anonymous
2016-11-08 03:20:33 UTC
in case you have the PDF Reader, you are able to extract the text cloth and photos in maximum situations (some information do no longer enable it.) yet to no longer reinsert it or create a PDF. some HP printer application, does assist you to Print to a PDF. decide on output as PDF quite of a printer. It saves in My information folder. you additionally can print off the rfile and then test it back in utilising an OCR software (maximum Scanners comprise a elementary version.)
ⓘ
This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.