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.
Hi,
I wrote this script to read each line of a text file and than do something with the line content:
Code:
for i in 'cat text.txt'
do
......
done
Is there a way to read only specific lines of text files?
For example I have 10 lines and I want to read only
line 3
line 5
line 9
Thanks in advance for help
ok, but i have to integrate it in a for loop.
I don't know the exact number of lines in the text file, and I have to read lines jumping some of them. To be clear I need to do something like this:
start to read the third line of the file
then jump one line and read the next one
do this to the end of the lines
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195
Rep:
Yes, I understood. There are a zillion ways to do that. One of them could be:
Code:
lines_to_skip=0
while read line
do
echo $line
if [ $lines_to_skip -gt 0 ]
then
<do nothing>
let "lines_to_skip--"
else
<process $line>
<set lines_to_skip if necessary>
fi
done <mytextfile
Note that <this code> is not syntactically correct.
If you want to skip line without actually reading them, that is not possible in the loop AFAIK. You could use an external program for that, but is that really worth doing?
ok, Thanks to schneidz suggestion I solved in this way:
I set a variable to the number of line I want to start from
Then in the loop I increase this variable to skip one line in sed command
Code:
I=3
for j in 1 2 3 ....
do
sed -n "$I"p file.txt
I='expr $I + 2'
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.