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

selecting substrings and changing them

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hello,

I need to write that turns the following input:

Info: yes
Delta: three|calu|||fire|2;5.1|klay
Info: no
Delta: Jane||help||5;4.3|fuzz
Info: yes
Delta: |8;5.9|klem

into:

Info: yes
Delta: 2;5.1
Info: no
Delta: 5;4.3
Info: yes
Delta: 8;5.9

Can someone help me with this?

Mozluv
 
BEGIN {
FS=":"
}

$1 ~ /^Info/ { print }
$1 ~ /^Delta/ {
deltaValue=$2
numDeltas=split(deltaValue, deltaARR, "|")
for (i=1; i <= numDeltas; i++)
if ( deltaARR ~ &quot;;&quot;) {
print $1 FS deltaARR;
next;
}

}
 
ooops - TGML enabled - sorry 'bout that

BEGIN {
FS=&quot;:&quot;
}

$1 ~ /^Info/ { print }
$1 ~ /^Delta/ {
deltaValue=$2
numDeltas=split(deltaValue, deltaARR, &quot;|&quot;)
for (i=1; i <= numDeltas; i++)
if ( deltaARR ~ &quot;;&quot;) {
print $1 FS deltaARR;
next;
}
print $1;

}
 
Hi,

Here is a simple awk script .. run it at command promt and pass it filename as an argument.

>>>>>>>>>>>>>>>>>>>>>>>>

#!/bin/sh

FileName=$1

awk -F: '{
split($1,a,FS);
i=split($2,b,&quot;|&quot;);
if(i > 0)
{
if(i == 1)
{
printf(&quot;%s:%s\n&quot;,a[1],b[1]);
}
else
{
printf(&quot;%s:%s\n&quot;,a[1],b[i-1]);
}
}
}' $FileName

>>>>>>>>>>>>>>>>

hope, this is what you were looking for :)

regards,
Mahesh
 
you could try this as well

awk -F: '/^Info/{print $0}/^Delta/{gsub(/[A-z]*[|]*/,&quot;&quot;,$2); print $1&quot;:&quot;$2 }'
 
Many thanks to everyone! I'm learning fast, thanks to you!

Mozluv
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top