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 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
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
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.
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
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.