LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-25-2013, 04:11 AM   #1
bal_nair1
Member
 
Registered: Apr 2012
Location: Bangalore,India
Posts: 41

Rep: Reputation: Disabled
Smile 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.

Hope you understand the problem.

Thank you in advance
 
Old 03-25-2013, 04:53 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
I do not really understand it, but it looks like a homework.
I would use awk or perl, that would be easy. What do you prefer? What have you tried?
 
Old 03-25-2013, 06:58 AM   #3
bal_nair1
Member
 
Registered: Apr 2012
Location: Bangalore,India
Posts: 41

Original Poster
Rep: Reputation: Disabled
Sorry it needs more explanation
123456781234567812345678123456781234567812345678
PSHEll 1 4 23 4 0

Now I want script to check any 0 value is present in the line Pshells, but forevery 8 chara field

In this case zero is present in the 6th field

Hope u got
 
Old 03-25-2013, 07:14 AM   #4
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
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.

Daniel B. Martin
 
Old 03-25-2013, 07:41 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
@bal_nair1: it would have been more clear if you used CODE tags to embed data or lines of code. I interpret your input as
Code:

123456781234567812345678123456781234567812345678
PSHELL  1       4       23      4       0
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.
 
Old 03-25-2013, 09:22 AM   #6
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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
--- rod.
 
Old 04-04-2013, 07:26 AM   #7
bal_nair1
Member
 
Registered: Apr 2012
Location: Bangalore,India
Posts: 41

Original Poster
Rep: Reputation: Disabled
I would be explaining it again, I am happy dat with my bad explaination all of you could manage to understand it

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

a = 1
b = 4
c = 23
d = 4
e = 0
for it should return no values are assigned

The task is self assigned one.

Thank you in advance.
 
Old 04-04-2013, 09:32 AM   #8
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by bal_nair1 View Post
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.
 
Old 04-04-2013, 02:44 PM   #9
redfox2807
Member
 
Registered: Jul 2012
Distribution: Debian testing/stable, Gentoo, CentOS 7, Sailfish OS, Android
Posts: 167

Rep: Reputation: 31
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Import a txt file and read line by line, polyline by polyline C++ 924199952 Programming 2 02-16-2012 05:35 AM
How can read from file.txt C++ where can save this file(file.txt) to start reading sam_22 Programming 1 01-11-2007 05:11 PM
Read a line in a txt file with bash orgazmo Programming 5 05-03-2005 07:10 AM
Read a line in a txt file with bash orgazmo Linux - Newbie 3 05-03-2005 04:16 AM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 10:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration