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!

Another beginner question... 3

Status
Not open for further replies.

BIS

Technical User
Jun 1, 2001
1,893
NL
Hallo Again,

I have a file like this:

First_Name = John
my_last_name = Doe

I would like to get:

<name>John Doe</name>

I am doing this:

/^(First_Name|my_last_name)/ {r=r" "$3; next}; r {print"<name>"r"</name>"; r=""}

But the result is:
<name> John Doe</name>

Can anybody tell me how to get rid of that space after <name> ?
 
Brute force method:
/^(First_Name|my_last_name)/ {r=r" "$3; next}; r {print"<name>"substr(r,2)"</name>"; r=""}

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
man awk:

substr(s,i,n) substr(s,i)
Returns the substring of string s, starting at index i, of length n.If n is omitted, the suffix of s, starting at i is returned.


Learned a bit more there - thank you (again).
 
One last question for today if I may...

Suppose I have a line with:

Address =

but I do not know what will be after the = sign.

Is there a way to get this, something like

/^Address/ {$0="<address>$3and anything after this exluding trailing whitespace"</address>}
 
Code:
# Set field-separator.
BEGIN { FS = " *= *" }
# Remove trailing spaces.
{ sub(/[ \t]+$/, "") }
/^First_Name/ { first = $2 ; next }
/^my_last_name/ {$0 = "<name>" first " " $2 "</name>"}
/^Address/ {$0="<address>" $2 "</address>"}
3.1416
 
many many thanks!
May I ask two questions?

The line { sub(/[ \t]+$/, "") } - I have a hard time understanding this, which is why I guess I don't completely understand why in the address part I only need to print $2

If the address part is on more than one line, how would I modify this?

Sorry if these are basic questions, I am just trying to learn...
 
oh wait - the $2 must come from the FS. Is there a way to make this look on more than one line? Suppose I have something like:

Remarks = Hallo Hallo

Some rambling here.

Thanks

Bye.

$2 will only print Hallo Hallo

and not the following lines.
 
Sorry to be pressing, but I can't seem to figure this out...

Is there a way for {$0="<address>" $2 "</address>"} above to look on more than one line?
 
Code:
# Set field-separator.
BEGIN { FS = " *= *" }
# Remove trailing spaces.
{ sub(/[ \t]+$/, "") }
/^First_Name/ { pr(); first = $2 ; next }
/^my_last_name/ { pr(); $0 = "<name>" first " " $2 "</name>"
  print; next}
/^Address/ { pr(); accum="<address>" $2; next}
{ accum = accum ";" $0 }
END { pr() }
function pr()
{ if ( accum )  print accum "</address>"
  accum = ""
}
 
Many thanks,

If you have the time, could you be so kind as to explain this to me? I get some strange results which I am sure a little understanding would fix...
 
If your screen-resolution isn't high enough, tektips splits the lines in the code. This may be better.
[tt]
# Set field-separator.
BEGIN { FS = " *= *" }
# Remove trailing spaces.
{ sub(/[ \t]+$/, "") }
/^First_Name/ { pr(); first = $2 ; next }
/^my_last_name/ { pr()
$0 = "<name>" first " " $2 "</name>"
print; next}
/^Address/ { pr(); accum="<address>" $2; next}
{ accum = accum ";" $0 }
END { pr() }
function pr()
{ if ( accum ) print accum "</address>"
accum = ""
}
[/tt]
For an introduction to Awk, see faq271-5564.
 
Many thanks, I am almost there I think.
I cannot figure out what the

{ accum = accum ";" $0 }

line does. With the above it seems to move through the entire file, and not just everything after /^Address/ .Does the ";" represent a new line or something?
 
Simple string concatenation.
Each line read is tacked on to accum until we hit
First_Name, my_last_name, or Address; then accum is printed.
 
Hmm, this has got me stumped...

Suppose I have this file:

First_Name = John
my_last_name = Doe
Address = Some Street, 48
Some City

Some Country




and I want the output to be:

<name>John Doe</name>
<address>Some Street, 48
Some City
Some Country</address>

I can't seem to get everything after Address =
 
Hmm never mind :)

I got it using something like:

/^Address =/ {
print "<address>"$0
while (getline > 0)
print $0
print "</address>"
}

I will need to refine this, but it looks as if I am on the right track.
 
Code:
# Set field-separator.
BEGIN { FS = " *= *" }
# Remove trailing spaces.
{ sub(/[ \t]+$/, "") }
/^First_Name/ { pr(); first = $2 ; next }
/^my_last_name/ { pr(); $0 = "<name>" first " " $2 "</name>"
  print; next}
/^Address/ { pr(); accum="<address>" $2; next}
NF { accum = accum RS $0 }
END { pr() }
function pr()
{ if ( accum )  print accum "</address>"
  accum = ""
}
The output is
[tt]
<name>John Doe</name>
<address>Some Street, 48
Some City
Some Country</address>
[/tt]
 
Thanks,
The RS was the missing part I guess.
Thanks for your patience as well...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top