To read a particular line in a txt file and decision making
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.
To read a particular line in a txt file and decision making
Hi all,
I am new to Linux programming,and I am supposed to make a script which runs as shown below
We have a txt file
say abc.txt
with the following content
$23456781234567812345678123456781234567812345678
PSHELL x y z w s
where x,y,z,w and s are some numerical values.
The script should read the txt file and go to the line containing the word PSHELL and check for all the eight-field spaces in the line, whether a particular number is wriiten in a field .
for example script should check whther the value of x ,y, z,w or s is zero.it it is zero it should write in a seperate txt file.
Help us to help you. Provide a sample input file (10-15 lines will do). Construct a sample output file which corresponds to your sample input and post both samples here. With "Before and After" examples we can better understand your needs and also judge if our proposed solution fills those needs.
where the line in grey is just a meter to check the position. Moreover, it looks like the fields are separated by TAB (not blank spaces), is this correct?
More information is required and specifically:
. you are going to write a script... in which language?
. is this homework, as someone suggested?
. what have you tried so far and where did you stick?
Please help us to help you.
Is this something like what you're trying to accomplish?
Code:
read PSHELL x y z w s < <(grep PSHELL abc.txt)
if [[ $x = 0 ]]; then echo zero; else echo nonzero; fi
if [[ $y = 0 ]]; then echo zero; else echo nonzero; fi
if [[ $z = 0 ]]; then echo zero; else echo nonzero; fi
if [[ $w = 0 ]]; then echo zero; else echo nonzero; fi
if [[ $s = 0 ]]; then echo zero; else echo nonzero; fi
I would be explaining it again, I am happy dat with my bad explaination all of you could manage to understand it
Code:
a b c d e f
123456781234567812345678123456781234567812345678
PSHELL 1 4 23 4 0
the script should read the line contains PSHELL , and while checking each field of the line, the script returns the value under the each field say in this case
Code:
a = 1
b = 4
c = 23
d = 4
e = 0
for it should return no values are assigned The task is self assigned one.
Ok...so again, post what you've written/done/tried, and some more of the input file, and we can try to help, but we're not going to write the script for you. If you want a hint on how to do it, I'd read both lines into different variables, then just read the positions of what you like and output them.
Well, didn't you specify the language you need or did I miss something?
Code:
1. read file line by line
2. find the line that contains/starts with (depending on the input file possible variations) PSHELL
3. assign the line value to a string variable
4. split the string by spaces and/or tab character and assign the resulting values to an array-like object. Depending on the language chosen this might need string to int conversion
5. process the array items however you like
the above is written with Qt/C++ in mind. Many of scripting lanquages are designed to do several steps of this algorithm at once.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.