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

How can I copy part of a text line to a differnt part of the same line

Status
Not open for further replies.

Jahanur

Technical User
Nov 11, 2003
1
US
Hi Folks,
How can I copy part of a text line to a differnt part of the same line?

For example:
I have a file that has the following entry :
---------
<option value=““>ALBANY – ALB</option>
<option value=““>ALBUQUERQUE – ABQ</option>
<option value=““>ANCHORAGE – ANC</option>
<option value=““>APPLETON – ATW</option>
---------
And would like to add the 3 letter airline code inbetween the quotation mark, like this:

<option value=“ALB“>ALBANY – ALB</option>
<option value=“ABQ“>ALBUQUERQUE – ABQ</option>
<option value=“ANC“>ANCHORAGE – ANC</option>
<option value=“ATW“>APPLETON – ATW</option>

Please help.
Jahanur
 
Hi Jahanur,

This will work if you have nawk, gawk, or another of the
more modern &quot;awks&quot;:

nawk 'BEGIN{FS=&quot;&quot;}

{

one = index($0,&quot;=&quot;)
two = index($0,&quot;-&quot;)

first = substr($0, 1, one + 1)
second = substr($0, one + 2, (two + 2)-(one +2) )
third = substr($0, two + 2, 3 )
fourth = substr($0, two + 5)

$0 = sprintf(&quot;%s%s%s%s%s\n&quot;, first, third, second, third, fourth)

print

}' infile > outfile


Hope this helps!

[sig]<p>flogrr<br><a href=mailto:flogr@yahoo.com>flogr@yahoo.com</a><br><a href= > </a><br> [/sig]
 
awk ' BEGIN{ FS=&quot;-|<|=|>&quot; }

{
gsub( &quot; &quot;, &quot;&quot;)
print &quot;<&quot; $2 &quot;=\&quot;&quot; $5 &quot;\&quot;&quot; &quot;>&quot; $4 &quot; - &quot; $5 &quot;<&quot;$6
}'

regards Gregor [sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top