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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

writing awk commands into a file 1

Status
Not open for further replies.

New2awk

MIS
Aug 8, 2005
12
AT
Hi I wrote some lines in awk and now I wanted to write them into a file, but there always occur some errors.
Hope someone can help me.

awk '/bringUp/' V03_autsfce12_26-07-05.xml > bringUp.txt

awk '{if ($2=="Status=\"bringUp\"") print $4 > "New.txt";
else if ($3=="Status=\"bringUp\"") print $2 > "New.txt";
else if($4=="Status=\"bringUp\"") print $3 > "New.txt";}' bringUp.txt

awk '{split($1,i,"\"")
print i[2] > "Neu.txt";}' New.txt


Thanks
 
Now I've got a second problem.
If I open the the file "Neu.txt" in Windows the word-wraps
don't exist there are only these small quads.
Is there a way I can fix this?
 
Hi

1) And what are that errors and when they occurs ?
2) Try to set the record separator to [tt]CRLF[/tt] :

Code:
BEGIN { ORS="\r\n" }
[gray]# then the rest of the script follows...[/gray]

Feherke.
 
Hi and thank you ;)
2) works fine now but
1) I tried some stuff now but it still doesn't work:
Code:
BEGIN { ORS="\r\n" }
{if ($2=="Status=\"bringUp\"") print $4 > "New.txt";	#
else if ($3=="Status=\"bringUp\"") print $2 > "New.txt";#This part works
else if($4=="Status=\"bringUp\"") print $3 > "New.txt";}#

END { split($1,i,"\"")
print i[2] > "Neu.txt";}

and I started it this way:
awk -f Test.awk bringUp.txt New.txt

..maybe I made a stupid mistake but I started with awk a couple of days ago so pardon me ^^
 
Hi

Could you please post a few lines from bringUp.txt ?

I agree, probably there is a misunderstanding :
[ol]
[li]You specified both bringUp.txt and New.txt as parameter for [tt]awk[/tt]. This way both files will be read and parsed. But during the file processing you write into New.txt, so the result is somehow unpredictable.[/li]
[li]You use [tt]$1[/tt] in the [tt]END[/tt] section. [tt]$1[/tt] means first field of current record. But when the [tt]END[/tt] section is executed, all inputs are finished so there is no current record anymore.[/li]
[/ol]

Feherke.
 
<Aps Name="ZZ1V7A851601" Status="bringUp" Type="delta" ForcedOnline="no"/> -> bringUp.txt

Name="ZZ1V7A851601" -> New.txt

and that's what Neu.txt should look like:

ZZ1V7A851601

I didn't know how to specify the parameters for each command and i hoped that might work
 
Hi

In your code you looked for that Status="bringUp" in the fields 2, 3 and 4. So I think it will be better to search for it in the entire line, then print out the value part of the Name attribute, anywhere it is in the line.

Code:
/Status="bringUp"/ {
  for (i=1;i<=NF;i++) if ($i~/^Name=/) print gensub(/Name="(.*)"/,"\\1","",$i) > "Neu.txt"
}

Note, that [tt]awk95.exe[/tt] not fully supports the regular expressions, so there is a possibility to not work.

Feherke.
 
Hey again ;)
Now i have another problem:
I still search for lines with "Status="bringUp"" but now, if a line has been found I want to get into the line above and write the VariantName + the Name into a file

<Service VariantName="A_Name" Version="7.5" ......>
<Aps Name="INTY1O7K0200" Status="bringUp" ......>

regards
 
Hi

Just put it in a variable, then use it when needed :
Code:
{
  for (i=1;i<=NF;i++) if ($i~/^VariantName=/) vn=gensub(/VariantName="(.*)"/,"\\1","",$i)
}
/Status="bringUp"/ {
  for (i=1;i<=NF;i++) if ($i~/^Name=/) print vn "+" gensub(/Name="(.*)"/,"\\1","",$i) > "Neu.txt" 
}

Feherke.
 
Hi okay that was somekind of easy ^^
And again thank you Feherke.
 
Hey is it actually possible to set a limit?
What I mean is that after the line with "VariantName" it can appear that there is more than one "Name" that has the status "bringUp", but I only need the first Name with the status "bringUp". Can I somehow stop it after one Name with bringUp has been found?

regards
 
Have a look at the exit statement in the awk man page.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hey well "exit" didn't work for me in the way I wanted to use it, but anyway I had to load the .txt file into an excel worksheet where I now just deleted the double/tribble... entries with a VBA Makro

regards
 
Hi

Hmm... So you want a single value or unique values ? Maybe you need this :

Code:
{
  for (i=1;i<=NF;i++) if ($i~/^VariantName=/) vn=gensub(/VariantName="(.*)"/,"\\
}

/Status="bringUp"/ {
  for (i=1;i<=NF;i++) if ($i~/^Name=/) {
    name=gensub(/Name="(.*)"/,"\\1","",$i)
    if (!done[name]) print vn "+" name > "Neu.txt" 
    done[name]=1
  }
}

Feherke.
 
Good morning;)
Aehm it's like this:
Code:
<Service VariantName="[COLOR=red]A_Name[/color]"  Version="7.5" ......>
<Aps Name="[COLOR=red]INTY1O7K0200[/color]"  Status="bringUp" ......>
<Aps Name="INTY1O7K0190" Status="bringUp" ......>
..
..

<Service VariantName="[COLOR=red]NameB[/color]" Version="7.5" ......>
<Aps Name="[COLOR=red]INTY1O7K0300[/color]" Status="bringUp" ......>
<Aps Name="INTY1O7K0299" Status="installed" ......>
..
..

The red highlighted words i need to readout.
It always starts with a VaiantName and in the next line there is the Name. The VarianteName and the Name only have to be readout if the status is on bringUp and also only the first Name with the status bringUp has to be readout *gg*

regards and good morning ;)
 
Hi

Code:
/Status="bringUp"/ [red]&& vn!=""[/red] {
  for (i=1;i<=NF;i++) if ($i~/^Name=/) {
    name=gensub(/Name="(.*)"/,"\\1","",$i)
    if (!done[name]) print vn "+" name > "Neu.txt" 
    done[name]=1
  }
}

{
  [red]vn=""[/red]
  for (i=1;i<=NF;i++) if ($i~/^VariantName=/) vn=gensub(/VariantName="(.*)"/,"\\1","",$i)
}

Feherke.
 
Thanks this is much shorter than the way I did it in vba, but can you explain me this line?
if (!done[name])
I searched for "done" but couldn't find anything suitable.
 
Hi

The [tt]done[/tt] is the name of an array. [tt]:)[/tt]
In [tt]awk[/tt] all arrays are associative, so their index could be a string expression. For example, "INTY1O7K0200". To keep control on unique values, the simplest method is to create an element in an array for each value encountered, then just check the keys of that array.

So [tt]if (!done[name])[/tt] means "if the [tt]name[/tt]-th element on the [tt]done[/tt] array is something false...".

But more [tt]awk[/tt]-like that line could be :

Code:
if (!(name in done)) print vn "+" name > "Neu.txt"

Feherke.
 
No need of array, I think
Code:
/VariantName=/{
  sub(/.*VariantName="/,"");sub(/".*/,"")
  vn=$0
}
/Status="bringUp"/ && vn!="" {
  sub(/.*Name="/,"");sub(/".*/,"")
  print vn "+" $0 > "Neu.txt"
  vn=""
}

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top