ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I can extract line one from a text file and place it into the string $LINE using:
Code:
LINE=$(sed -n 1p myfile.txt)
In my script I want to iterate through all the lines and extract one line at a time to do some work on them.
I can't figure out how to enter a string holding a variable into this bit of code. I was expecting something like the line below to work but it fails.
However, if you want to iterate over all line in a file, it is not the best way to do it. Here's a much cleaner and faster way to iterate over all lines of a file:
Code:
while read line ; do
echo "== work on this this line: $line"
done < myfile.txt
Are you sure LINENUMBER has a value? Using ${LINENUMBER} just means that the variable name is $LINENUMBER and not $LINENUMBERp, so the result will be something like 1p and not 1 p.
Put the command "set -x" in the beginning of your script, and you will get debug information and can see what is happening.
Copy-Paste into your editor.
Save as poc.sh.
Make executable: chmod +x poc.sh
Run it: ./poc.sh
Code:
#!/bin/bash
cat <<EOF >myfile.txt
line one
line two
line three
EOF
LINENUMBER=2
LINE=$(sed -n ${LINENUMBER}p myfile.txt)
echo "LINENUMBER = $LINENUMBER"
echo "LINE = $LINE"
Output is:
Code:
LINENUMBER = 2
LINE = line two
If it is not, check your operating system, and bash shell etc...
No. I understand whats going wrong now.
Originally I had a loop from 1-64, as the file always has 64 lines in it. Then was trying to get each one of the lines extracted using sed.
So I was passing this number to SED in the "sed -n $line p" which did not work. However I think at that point "sed -n ${line}p" would have worked. However I followed an earlier suggestion from Hko to use "while read line ; do" command.
Now this command was sending the actual line to that value and so sed was receiving a line of text and not a number.
Each call to sed must open the file.
The smart alternative was stated in post#4:
The file is opened once for the while-do-done block, and each call to read reads a new line into a variable.
Code:
while read line
reads a line into the variable $line, tests the exit status, and ends the loop if there is no more line (eof).
The whole loop is redirected from the file.
If you do not want a loop, you can redirect a { code block } like this
Code:
{
for demo in 1 2 3
do
if read line
then
echo "line#$demo is $line"
else
echo "eof"
fi
done
} < myfile.txt
You can take an explicit file descriptor 3 or 4 or ... to avoid a conflict with stdin
Code:
{
...
read line <&3
...
} 3< myfile.txt
If you do not want to have a code block then you can associate a descriptor with exec
Code:
exec 3< myfile.txt
...
for demo in 1 2 3
do
if read line <&3
then
echo "line#$demo is $line"
else
echo "eof"
fi
done
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.