Solved

Bash script to parse the Windows version written inside the license.rtf

Posted on 2010-09-17
21
602 Views
Last Modified: 2013-12-26
Hello,

Since I'm totally newbie in bash scripting,can someone help me to write a bash script to parse the version of Windows written inside the license.rtf file ? Thanks.

If it starts with :

MICROSOFT SOFTWARE LICENSE TERMS

WINDOWS 7 ULTIMATE

These license terms are an agreement between Microsoft Corporation (or based on where you live, one of its affiliates) and you. Please read them. They apply to the software named above, which includes the media on which you received it, if any. Printed-paper license terms, which may come with the software, may replace or modify any on-screen license terms. The terms also apply to any Microsoft

I need to grab the version of Windows used,in this case it is : WINDOWS 7 ULTIMATE

thanks in advance.
0
Comment
Question by:marietto2008
  • 12
  • 9
21 Comments
 
LVL 14

Expert Comment

by:ddsh79
ID: 33700013
If it is in the third line always then you can use the script below
i=0
while read line
do
      if [ $i -eq 3 ]
      then
            echo "$line"
      fi
      i=`expr $i + 1`
done < winver.txt

here winver.txt is the file to read
0
 
LVL 14

Expert Comment

by:ddsh79
ID: 33700023
An alternate can be

cat winver.txt | grep "WINDOWS"

but this will return all the lines containing WINDOWS (all caps)
0
 
LVL 2

Author Comment

by:marietto2008
ID: 33710714
I like the option n.2. I've tried to parse the eula.txt of windows XP with this command :

mario@mario-desktop:/media/FREEDOS/sh$ cat eula.txt | grep "Microsoft Windows"

Microsoft Windows XP Home Edition

I need to store the output "Microsoft Windows XP Home Edition" inside a variable

and I've tried to parse the license.rtf of Windows Vista / Seven with this command :

cat license.rtf | grep "WINDOWS"

\pard\brdrb\brdrs\brdrw10\brsp20 \nowidctlpar\sb120\sa120 WINDOWS 7 ULTIMATE\par
mario@mario-desktop:/media/FREEDOS/sh$

in this case I need to store inside a variable the output : WINDOWS 7 ULTIMATE
0
 
LVL 14

Expert Comment

by:ddsh79
ID: 33711542
inside a script you can always store output into a variable using the command in `` (acutes)


temp = `cat licence.rtf | grep "WINDOWS"`
echo $temp
this should store the the value of the commands (in your case "WINDOWS 7 ULTIMATE")  to the variable temp
0
 
LVL 2

Author Comment

by:marietto2008
ID: 33711557
cat licence.rtf | grep "WINDOWS" returns nothing,because Rich text is a kind of markup language, similar to html. What you see in the display is not exactly the text the file itself contains. The parsing would have to take this into account.
0
 
LVL 2

Author Comment

by:marietto2008
ID: 33711559
errata corrige :

cat licence.rtf | grep "WINDOWS" returns * nothing that I can use as is *
0
 
LVL 14

Expert Comment

by:ddsh79
ID: 33711575
ah sure... then may be you can use cut to get the output. Do not have a linux box right now to test it but you can do something like below
cat licence.rtf | grep "WINDOWS" | cut -d\ -f8
here -d\ is used for delimiter as "\"
-f8 is for field 8 after cut is performed, so this should return the output.

you can also use awk for this.
0
 
LVL 2

Author Comment

by:marietto2008
ID: 33711675
mario@mario-desktop:/media/FREEDOS/sh$ cat license.rtf | grep "WINDOWS" | cut -d\ -f8

error : cut: delimiter must be a single character.
0
 
LVL 14

Expert Comment

by:ddsh79
ID: 33711692
use it in single quotes '\'
0
 
LVL 2

Author Comment

by:marietto2008
ID: 33711698
mario@mario-desktop:/media/FREEDOS/sh$ cat license.rtf | grep "WINDOWS" | cut -d'\' -f8

sb120
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 2

Author Comment

by:marietto2008
ID: 33711701
this is the beginning of the license.rtf file :

{\rtf1\ansi\ansicpg1252\deff0\deflang1033\deflangfe2052\deftab360{\fonttbl{\f0\fswiss\fprq2\fcharset 0 Tahoma;}}
{\*\generator Msftedit 5.41.21.2508;}\viewkind4\uc1\pard\nowidctlpar\sb120\sa120\b\f0\fs20 MICROSOFT SOFTWARE LICENSE TERMS\par
\pard\brdrb\brdrs\brdrw10\brsp20 \nowidctlpar\sb120\sa120 WINDOWS 7 ULTIMATE\par
\pard\nowidctlpar\sb120\sa120\b0 These license terms are an agreement between Microsoft Corporation (or based on where you live, one of its affiliates) and $
\pard\nowidctlpar\sb120\sa120\tx0\'b7\tab updates,\par
\'b7\tab supplements,\par
\'b7\tab Internet-based services, and\par
\'b7\tab support services\par
\pard\nowidctlpar\sb120\sa120 for this software, unless other terms accompany those items. If so, those terms apply.\par
\b By using the software, you accept these terms. If you do not accept them, do not use the software. Instead, return it to the retailer for a refund or cre$
\b As described below, using the software also operates as your consent to the transmission of certain computer information during activation, validation an$
\pard\brdrt\brdrs\brdrw10\brsp20 \nowidctlpar\sb120\sa120 If you comply with these license terms, you have the rights below for each license you acquire.\par
\pard\nowidctlpar\sb120\sa120\tx360 1.\tab OVERVIEW.\par
0
 
LVL 14

Expert Comment

by:ddsh79
ID: 33711702
use -f9 or -f10 or may be -f7.. here number after f represents the column you want.
0
 
LVL 2

Author Comment

by:marietto2008
ID: 33711708
mario@mario-desktop:/media/FREEDOS/sh$ cat license.rtf | grep "WINDOWS" | cut -d'\' -f9

sa120 WINDOWS 7 ULTIMATE

mario@mario-desktop:/media/FREEDOS/sh$ cat license.rtf | grep "WINDOWS" | cut -d'\' -f10

par

the first should be good,but I have to cut "sa120"
0
 
LVL 14

Expert Comment

by:ddsh79
ID: 33711709
i can also see a space "sa120 WINDOWS 7 ULTIMATE" so you need to do another cut after getting this string like

cat license.rtf | grep "WINDOWS" | cut -d'\' -f9 | cut -d' ' -f2
here ' ' is space
0
 
LVL 2

Author Comment

by:marietto2008
ID: 33711715
mario@mario-desktop:/media/FREEDOS/sh$ cat license.rtf | grep "WINDOWS" | cut -d'\' -f9 | cut -d' ' -f2
WINDOWS

has been cut exactly what I want : "7 ULTIMATE"
0
 
LVL 14

Accepted Solution

by:
ddsh79 earned 500 total points
ID: 33711747
i surely forgot  the another spaces :).. my bad.. well cut here will give you all the words separated by a space. But i believe you need all the things after sb120 so you can specify all the fields as

cut -d' ' -f "2 3 4"

0
 
LVL 2

Author Comment

by:marietto2008
ID: 33711768
Bingo,you "won" 500 points :D

mario@mario-desktop:/media/FREEDOS/sh$ cat license.rtf | grep "WINDOWS" | cut -d'\' -f9 | cut -d' ' -f "2 3 4"
WINDOWS 7 ULTIMATE

btw sh scripting is a kind of language I have because you have to pay your attention over aspects frankly not so important and you can't pay attention over the whole piece of code.
0
 
LVL 2

Author Comment

by:marietto2008
ID: 33711770
*I hate*
0
 
LVL 2

Author Comment

by:marietto2008
ID: 33712315
ver_windows=$(cat /mnt/sda1/Windows/system32/eula.txt | grep "Microsoft Windows")
echo $ver_windows

Microsoft Windows XP Home Edition

if [ "$ver_windows" = "Microsoft Windows XP Home Edition" ]; then ver_windows_min="xp"
echo home
fi

output : nothing

I think that $ver_windows is not = to "Microsoft Windows XP Home Edition" ,why ?
0
 
LVL 14

Expert Comment

by:ddsh79
ID: 33713927
Try this
if [ $ver_windows -eq "Microsoft Windows XP Home Edition" ]
then
    ver_windows_min="xp"
    echo $home
fi
0
 
LVL 2

Author Comment

by:marietto2008
ID: 33714182
FIXED : there was a stupid hidden space at the end of the first line.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
wordappend challenge 8 85
python sqlite question 11 43
White board coding practice 3 61
Modify a small python script 19 51
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now