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!

extract from file

Status
Not open for further replies.

tini1208

Technical User
Feb 13, 2006
56
DE
hi all,

i want to extract several lines (the lines are more than one time in the file) from a file. the input looks like:

i_AttFlg1 = 16 16 16 16
i_lat = 50764695 50826421 50888144 50949866
i_lon = 95795766 95780813 95765832 95750822
i_OrbFlg = 0 0
0 0
0 0
0 0
i_spare1 = 0 0 0 0
i_LidarQF = 2 2 2 2
i_spare2 = 0 0 0 0 0 0 0 0
i_topo_elev = 0 0 0 0
i_atm_dem = 1442 1279 1315 1212
i_LRcld_bot = 32767 32767 32767 32767 32767 32767 32767
32767 32767 32767
i_LRcld_top = 32767 32767 32767 32767 32767 32767 32767
32767 32767 32767
i_LRcld_grd = 32767
i_spare3 = 0 0
i_MRcld_bot = 32767 32767 32767 32767 32767 32767 32767
32767 32767 32767
32767 32767 32767 32767 32767 32767 32767 32767 32767 32767
32767 32767 32767 32767 32767 32767 32767 32767 32767 32767
32767 32767 32767 32767 32767 32767 32767 32767 32767 32767
i_MRcld_top = 32767 32767 32767 32767 32767 32767 32767
32767 32767 32767
32767 32767 32767 32767 32767 32767 32767 32767 32767 32767
32767 32767 32767 32767 32767 32767 32767 32767 32767 32767
32767 32767 32767 32767 32767 32767 32767 32767 32767 32767

the goal is, to extract lines out of this input to an output file, for example:

i_lat
i_lon
i_MR_cld_bot
i_MR_cld_top

in case of i_MR_cld_bot and i_MR_cld_top i want to get the following lines, which belong to the variable.
i hope my explanation is comprehensible?!

please....can anybody help me??
 
What is the expected result from your posted sample ?
What have you tried so far ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
hi ph,

the expected result is an ascii table

lon lat MR_cld_bot MR_cld_top
xxx xxx xxx xxx
xxx xxx xxx xxx
xxx xxx xxx xxx

i tried to write a skript. here is a part of it. the rest is only variable setting:

awk '{if ($1==i_lat && $1==i_lon) print}' $i > $DATEI

with this, i started testing, what the awk is doing...thats the reason why i only used i_lon and i_lat.
i tried to get information about the command getline, but i am i beginner in using awk, so i didn'n understand a lot of

i hope, this is answering your questions.

regards
 
For i_lon there are four values in your sample input data. Do you just want the first one after the "i_lon ="?

Similarly for MR_cld_bot there are many more values... so you need to define exactly which data you want to put on each line.

Annihilannic.
 
hi annihilannic,

i want all values following i_lat oder i_lon and the first of each line in MRcld_bot/ MRcld_top.
it difficult for me to explain...

regards
 
Hi

My first thought was that (s)he want something like this :

i_lat[1] i_lon[1] i_MR_cld_bot[1] i_MR_cld_top[1]
i_lat[2] i_lon[2] i_MR_cld_bot[2] i_MR_cld_top[2]
...
i_lat[n] i_lon[n] i_MR_cld_bot[n] i_MR_cld_top[n]

But then I give it up. Let's wait for more specification.

Feherke.
 
A picture tells a thousand words. :)

Try showing us the desired output for the data you supplied above, for example, the way I interpret what you have said the result would be like this:

[tt]lon lat MR_cld_bot MR_cld_top
95795766 95780813 95765832 95750822 50764695 50826421 50888144 50949866 32767 32767[/tt]

But you can see it ends up with more than four columns? Is that correct?

Annihilannic.
 
Hi

What I understood after the latest explanation :

This :

i_lat = [red]50764695[/red] [green]50826421[/green] [blue]50888144[/blue] [purple]50949866[/purple]
i_lon = [red]95795766[/red] [green]95780813[/green] [blue]95765832[/blue] [purple]95750822[/purple]
i_MRcld_bot = [red]32767[/red] 32767 32767 32767 32767 32767 32767 32767 32767 32767
[green]32767[/green] 32767 32767 32767 32767 32767 32767 32767 32767 32767
[blue]32767[/blue] 32767 32767 32767 32767 32767 32767 32767 32767 32767
[purple]32767[/purple] 32767 32767 32767 32767 32767 32767 32767 32767 32767
i_MRcld_top = [red]32767[/red] 32767 32767 32767 32767 32767 32767 32767 32767 32767
[green]32767[/green] 32767 32767 32767 32767 32767 32767 32767 32767 32767
[blue]32767[/blue] 32767 32767 32767 32767 32767 32767 32767 32767 32767
[purple]32767[/purple] 32767 32767 32767 32767 32767 32767 32767 32767 32767

becomes this :

lon lat MR_cld_bot MR_cld_top
[red]50764695[/red] [red]95795766[/red] [red]32767[/red] [red]32767[/red]
[green]50826421[/green] [green]95780813[/green] [green]32767[/green] [green]32767[/green]
[blue]50888144[/blue] [blue]95765832[/blue] [blue]32767[/blue] [blue]32767[/blue]
[purple]50949866[/purple] [purple]95750822[/purple] [purple]32767[/purple] [purple]32767[/purple]

Or something else...

Feherke.
 
o.k....i will try...but its really hard for me to explain:
my input (example):

i_lat = v01 v02 v03 v04
i_lon = v05 v06 v07 v08
i_MRcld_bot = v09 v10 v11 v12 v13 v14 v15
v16 v17 v18
v19 v20 v21 v22 v23 v24 v25 v26 v27 v28
v29 v30 v31 v32 v33 v34 v35 v36 v37 v38
v39 v40 v41 v42 v43 v44 v45 v46 v47 v48
i_MRcld_top = v49 v50 v51 v52 v53 v54 v55
v56 v57 v58
v59 v60 v61 v62 v63 v64 v65 v66 v67 v68
v69 v70 v71 v72 v73 v74 v75 v76 v77 v78
v79 v80 v81 v82 v83 v84 v85 v86 v87 v88

my output:

lat lon i_MRcld_bot i_MRcld_top
v01 v05 v09 v49
v02 v06 v19 v59
v03 v07 v29 v69
v04 v08 v39 v79

better?? please let me know, its incomprehensible!!

regards
 
Well observed feherke, I should have noticed that there were four lines for the MR parameters, but I was confused by the wrapping.

Annihilannic.
 
yes....thats it! :)
can anybody help me?

regards
 
Here is my somewhat verbose solution:

Code:
awk '
BEGIN {
        print "lat lon MR_cld_bot MR_cld_top"
}
$1 == "i_lat" {
        lat[0]=$3
        lat[1]=$4
        lat[2]=$5
        lat[3]=$6
}
$1 == "i_lon" {
        lon[0]=$3
        lon[1]=$4
        lon[2]=$5
        lon[3]=$6
}
$1 == "i_MRcld_bot" {
        MR_cld_bot[0]=$3
        for (i=1; i<4; i++) {
                getline
                MR_cld_bot[i]=$1
        }
}
$1 == "i_MRcld_top" {
        MR_cld_top[0]=$3
        for (i=1; i<4; i++) {
                getline
                MR_cld_top[i]=$1
        }
        for (i=0; i<4; i++) {
                print lat[i],lon[i],MR_cld_bot[i],MR_cld_top[i]
        }
}
' input > output

Annihilannic.
 
thank you very very much...
but when i run the skript, there is an error:

unmatched '

i don't know, if there is a bug and if there is one, where it is. i checked blanks and line breaks....but there is nothing.

sorry...but i am a real beginner. :-(

 
You can see there is a ' at the very beginning and very end of the awk script... did you copy them both? The syntax is:

[tt]awk 'awk script here' inputfile > outputfile[/tt]

If you prefer you can take everything between the '' and put it in a separate file, say myscript.awk.

Then you would use:

[tt]awk -f myscript.awk inputfile > outputfile[/tt]

Annihilannic.
 
yes...i copied both of them.
when i put the skript into a seperate file, it looks like:

awk "
BEGIN {
print "lat lon MRcld_bot MRcld_top"
}
$1 == "i_lat" {
lat[0]=$3
lat[1]=$4
lat[2]=$5
lat[3]=$6
}
$1 == "i_lon" {
lon[0]=$3
lon[1]=$4
lon[2]=$5
lon[3]=$6
}
$1 == "i_MRcld_bot" {
MR_cld_bot[0]=$3
for (i=1; i<4; i++) {
getline
MR_cld_bot=$1
}
}
$1 == "i_MRcld_top" {
MR_cld_top[0]=$3
for (i=1; i<4; i++) {
getline
MR_cld_top=$1
}
for (i=0; i<4; i++) {
print lat,lon,MR_cld_bot,MR_cld_top
}
}
"


is it right? if it is and i run the file with the command, then i get the following erorrs:

awk: newline in string near line 1
awk: syntax error near line 2
awk: bailing out near line 2
awk: newline in string near line 34

regards, tini
 
Take off the first line and the last line, they are not part of the awk script itself.

Annihilannic.
 
thanks a lot...it works!
sorry for my ignorance.

bye bye...tini
 
hi...it's me again!!

the skript works fantastic! but now, i have a new problem!
i tried to adapt the skript, but it failed. i don't know, what went wrong, but i sure, that something is wrong!!!
o.k....the new parameter, i want to extract from the same file looks like:

i_ElvuseFlg = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1

that means 15 values in first line, 20 in second and 5 in third.
i need to get all 40 values in one record...like:

1
1
1
1
1
...

so, i adapt the skript and this is the result:

BEGIN {
print "Elv"
}
$1 == "i_ElvuseFlg"{
Elv[0]=$3
Elv[1]=$4
Elv[2]=$5
Elv[3]=$6
Elv[4]=$7
Elv[5]=$8
Elv[6]=$9
Elv[7]=$10
Elv[8]=$11
Elv[9]=$12
Elv[10]=$13
Elv[11]=$14
Elv[12]=$15
Elv[13]=$16
Elv[14]=$17
Elv[15]=$18
for (i=1; i++) {
getline
Elv=$19
Elv=$20
Elv=$21
Elv=$22
Elv=$23
Elv=$24
Elv=$25
Elv=$26
Elv=$27
Elv=$28
Elv=$29
Elv=$30
Elv=$31
Elv=$32
Elv=$33
Elv=$34
Elv=$35
}
for (i=1; i++) {
getline
Elv=$36
Elv=$37
Elv=$38
Elv=$39
Elv=$40
}
for (i=0; i++) {
print Elv
}
}

i'm sure, that some of you see the mistake...but i'm trying since 2 days!

tanks so far!!!!
 
Hi

Abit ugly, but I tried to make it flexible. This puts all values starting after the "i_ElvuseFlg" key, upto the next "i_???" key in the array.
Code:
/^i_ElvuseFlg/ {
  ind=0
  do {
    for (i=ind==0?3:1;i<=NF;i++) Elv[++ind]=$i
    if (getline!=1) break
  } while (!/^i_/)
  for (i=1;i in Elv;i++) print i,Elv[i]
}

Feherke.
 
hi feherke,

thanks for your help....but there is one question left:

if i put this in a seperate file, must there be a BEGIN at start?
when i run this without any other commands, it doesn't work.

cherrs
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top