Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

if-clause depending of a substring

Status
Not open for further replies.

lzbsupp1

Programmer
Jun 12, 2003
4
DE
Hallo,

I've the Problem to substitute a awk-scribt with a shell script.( On my AIX-Systems I've only the old version of awk, no nawk).

I have a file like this:

///MIF-File
Start Component
Name = "My Workstation Template"
Description = "Sample MIF Template"

Start Group
Name = "PC Information"
Id = 1
Class = "AMO|PC Information|1.0"

Start Attribute
Name = "Start Component
Name = "My Workstation Template"
Description = "Sample MIF Template"

Start Group
Name = "PC Information"
Id = 1
Class = "AMO|PC Information|1.0"

Start Attribute
Name = "Inventar Number"
Id = 1
Storage = Specific
Type = String(32)
Access = Write-Only
Value = "I798xxxxxx"
End Attribute"
Id = 1
Storage = Specific
Type = String(32)
Access = Write-Only
Value = "I798xxxxxx"
End Attribute
///END MIF-File

I want to print the Value for "Name" and "Value" after line 13 with the possibility to change "Value".

Here's the awk-Script:

#! /bin/nawk -f
BEGIN{
anfang=13
FS = "="
out = "hardwareinfo.mif"
print "" > out
}

{name=$0}
{if (NR>anfang&&$0 ~ /Name/) {
print $2
}
}
{if (NR>anfang&&$0 ~ /Value/) {
print $2"\n\nBitte neuen Wert eingeben"
getline x < &quot;/dev/fd/0&quot;
name = &quot; Value = \&quot;&quot; x&quot;\&quot;&quot;
}
}
{print name >> out
}


Now I'm looking since 2 days for a way to do this in a shell-Script. All i have to read the file line by line but i have no idea for the if-clause.

Thank you and reguards
 
Perhaps a combination of 'if' and 'case' ?

eg...
count=0
while read line
do
((count=count+1))
if [ $count -gt 13 ]
then
case $line in
*Name*) echo contains name ;;
*) echo does not contain name ;;
esac
fi
done < infile
 
Thnk you very much. That helped my a lot. But now I#ve the problem that a read command after the pattern is ignored (No errormessage) Does one know wy ?

#!/bin/bash
f [ $# = 0 ]
then echo ERROR: Usage is : $0 filename
exit 1
fi

#read the file line by line
count=0
while read line
do
((count=count+1))
if [ $count -gt 13 ]
then
case $line in
*Name*) echo $line|cut -d = -f2;;
*Value*)
echo $line|cut -d = -f2
echo &quot;Wollen Sie den Wert ändern ?&quot;
read eingabe;;
esac
fi
done < &quot;$1&quot;


Thank you and reguards
 
Both 'read's are reading from the standard input (file descriptor 0). What you want is to read from two inputs: file and keyboard.

Change the while loop to read from file descriptor 3...

while read [blue]-u3[/blue] line
do
:
done [blue]3<[/blue] &quot;$1&quot;
 
Thank you very much. The script work now.
One last question to the case-command. If i've more command's accourding to one case, what is the best way to handle this, or is it enough to end with ;; ?

pattern)command
command
command
command;;
 
I would separate each command in the list with ;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top