LinuxQuestions.org
Visit Jeremy's Blog.
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 11-03-2010, 02:27 PM   #1
anotherunixnoob
LQ Newbie
 
Registered: Nov 2010
Posts: 4

Rep: Reputation: 0
Bash script help needed


I have a script that starts to read through a txt file and finds a key word, as it is doing this it counts each line until it gets to its desired line(line y). Then it starts to re-read the file from the beginning until it gets line y+x where it performs some operations on that line to get the data I need and from there I can export the variable when needed.

The script does this for every piece of data that I need and I am looking for a way to do this and use considerably less code. What I need is a point in the right direction on how to do it so I can implement it in the rest of my script.

My code and a sample of the data I am reading are below.

Thanks in advance.

Code:
Code:
for i in `cat hardware`
do
		let "getnum++"
		if [[ $i == \<Processors\> ]]
			then
				break				
		fi
done

let "getnum = $getnum + 5"

for i in `cat hardware`
do
		let "counter++"
		if [[ $counter == $getnum ]]
			then
				cpuspeed=${i:7:7}
		fi
done
Data:
Code:
(line y)<Processors>
        <Processor is64bitCapable="yes">
          <Manufacturer>Intel</Manufacturer>
          <Version/>
(line y+x)<Speed>3000MHz</Speed>
 
Old 11-03-2010, 03:11 PM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Hi,

sounds like homework. Anyway, you could start by doing this all with just one loop.
 
Old 11-03-2010, 03:15 PM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
How about something like (not tested)
Code:
while read line
do
    if [[ [[ $line == \<Processors\> ]]; then
        for (( i=0; i<5; i++ ))
        do
             read line
        done
        <do what you want with the line 5 after Processors>
    fi 
done < input.txt
 
Old 11-03-2010, 03:23 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
How about using something that understands xml:

Code:
cpuspeed=$(xmlstarlet sel -t -v //Speed data.xml)
 
Old 11-03-2010, 04:27 PM   #5
anotherunixnoob
LQ Newbie
 
Registered: Nov 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by ntubski View Post
How about using something that understands xml:

Code:
cpuspeed=$(xmlstarlet sel -t -v //Speed data.xml)
I put 'bash script' in the title for a reason. Thanks for the suggestion anyway though.

And its not homework for school its homework for work.
 
Old 11-03-2010, 06:28 PM   #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
Quote:
Originally Posted by anotherunixnoob View Post
I put 'bash script' in the title for a reason. Thanks for the suggestion anyway though.
If someone asked you how to build a house with a toothbrush, you would probably mention that it isn't the right tool for the job (even if you did know how to accomplish such a task). Your problem is clearly looking for a solution using awk or Perl. Here's a shell script that uses Perl.
Code:
#! /bin/sh 
perl -e '@data=<>;foreach $i (1..@data){ if($data[$i]=~m/<Processors>/){print $data[$i+5];}}' YourInputData.File
--- rod.
 
Old 11-03-2010, 11:07 PM   #7
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by anotherunixnoob View Post
I put 'bash script' in the title for a reason. Thanks for the suggestion anyway though.
What makes it not a bash script? Is this better?

Code:
x=$(grep -n '<Processors>' hardware | grep -o '^[0-9]*')
let 'y = x + 4'
cpuspeed=$(sed -n "${y}s/<[^>]*>\| *//gp" hardware)
 
Old 11-04-2010, 09:29 AM   #8
anotherunixnoob
LQ Newbie
 
Registered: Nov 2010
Posts: 4

Original Poster
Rep: Reputation: 0
I am using a tooth brush to build my house because there are only tooth brushes on site. I'm writing this script to be used on a system that doesn't have perl installed. In fact it has very little installed because its a shell that we use during the boot process to update firmware on new machines that are being put into production. Could I install perl and then rebuild the boot image? I sure could, I just find writing a simple bash script to be considerably easier.

catkin's code revised
Code:
while read line
do
    if [[ $line == \<Processors\> ]]; then
        for (( i=0; i<4; i++ ))
        do
             read line
        done
        echo $line
       # <do what you want with the line 5 after Processors>
    fi
done < input.txt
When I was searching around on the tubes(before I posted) this is actually what I was looking for. I was looking for a way to find text that would be unique, and then be able to advance the line reader a certain amount of times to the text that I needed. All I could think of was how to do this in other languages like c++, java or perl. Thanks for the help catkin.

Quote:
Originally Posted by ntubski View Post
What makes it not a bash script? Is this better?

Code:
x=$(grep -n '<Processors>' hardware | grep -o '^[0-9]*')
let 'y = x + 4'
cpuspeed=$(sed -n "${y}s/<[^>]*>\| *//gp" hardware)
I jumped to quickly to a conclusion that your original code wasn't a bash script seeing 'xmlstarlet'. What I should have said was something about not having that particular utility. I look at your new code and the line that includes 'sed' confuses and infuriates me haha. I guess I'll have to read the man pages and some tutorials on sed. Thanks for the help.
 
Old 11-04-2010, 10:32 AM   #9
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by anotherunixnoob View Post
I guess I'll have to read the man pages and some tutorials on sed. Thanks for the help.
So, you can use sed for the task?
Code:
cpuspeed=$(sed -rn '/other/,+5 p' inputfile | sed -rn '$ s/[^>]*>([^<]*).*/\1/')
# or without the pipe
cpuspeed=$(sed -rn '/<Processors>/,/<Speed>.*<\/Speed>/ {/<Speed>.*<\/Speed>/ {s@.*<Speed>([^<]*)</Speed>.*@\1@;p}}' file)
The first one depends on "advancing" 5 lines (from your sample data it should be 4 though) to get the info, the second alternative searches for the pattern '<Speed>' after the pattern '<Processors>' is encountered.

Yes, sed replaces the both of your loops.

Last edited by crts; 11-04-2010 at 10:34 AM.
 
Old 11-04-2010, 04:41 PM   #10
anotherunixnoob
LQ Newbie
 
Registered: Nov 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Awesome thanks for the help. Right now my code looks like a mess but I'll keep working on it and as I get more familiar with the slightly more advanced stuff I'll be able to clean it up.
 
  


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
[SOLVED] Help needed with bash script and zenity. Wizard^^ Programming 4 10-29-2010 10:01 PM
Bash Script - Help needed manya Programming 14 07-22-2010 03:38 AM
Bash script. help needed tazthecat Linux - General 2 09-30-2005 01:54 AM
bash script help needed Henster Linux - General 4 08-21-2005 09:54 AM
Wrapper needed for BASH Script outspoken Programming 0 01-12-2005 09:04 AM

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

All times are GMT -5. The time now is 07:33 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