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

Insert duplicate field - repeat in long string 1

Status
Not open for further replies.

arunrr

Programmer
Oct 2, 2009
103
US
Hello,

I have the following input...
(continuous line; Field Separator=,)

AAAAAA,1,1,10.0,0,46,3,3/46,15.33,4.60,20.0,0,0,BBBBBB,1,1,9.0,1,29,2,2/29,14.50,3.22,27.0,0,0,CCCCCC,1,1,8.0,1,33,2,2/33,16.50,4.12,24.0,0,0,DDDDDD,1,1,9.0,0,47,2,2/47,23.50,5.22,27.0,0,0,EEEEEE,1,1,5.0,0,20,1,1/20,20.00,4.00,30.0,0,0,FFFFFF,1,1,5.0,0,23,1,1/23,23.00,4.60,30.0,0,0,GGGGGG,1,1,6.0,0,27,1,1/27,27.00,4.50,36.0,0,0,HHHHHH,1,1,10.0,1,30,1,1/30,30.00,3.00,60.0,0,0,IIIIII,1,1,6.0,1,35,1,1/35,35.00,5.83,36.0,0,0,JJJJJJ,1,1,8.5,1,40,1,1/40,40.00,4.52,53.0,0,0,KKKKKK,1,1,10.0,0,42,1,1/42,42.00,4.20,60.0,0,0,LLLLLL,1,1,2.0,0,8,0,-,-,4.00,-,0,0,MMMMMM,1,1,10.0,0,37,0,-,-,3.70,-,0,0,

In this example the 8th field (starting at AAAAAA) is 3/46. I need to insert a 9th field which will be a duplicate of the 8th field. Similarly, the 8th field (starting count at BBBBBB) is 2/29. This must be duplicated and inserted as the next field. And so on...

Use of sed, awk, or any combination thereof is fine. Thanks for the help.

Arun
 
I find it easier to print out each field and then duplicate fields 8 and 21. Older awks have a limitation on the number of fields that awk supports, but this works for nawk on Solaris:

Code:
#!/bin/ksh

nawk ' BEGIN { FS="," }
{
for(i=1; i <=NF; i++)
   {
   if(i < 8 || (i > 8 && i < 21)  || (i > 21 && i < (NF - 1) ) )
      printf("%s,", $i)

   if(i == NF)
      printf("%s\n", $i)

   if( (i == 8) || (i == 21) )
      printf("%s,%s,", $i,$i)
   }
} ' mydata55.txt
 
Let me try it again:

Code:
#!/bin/ksh

nawk ' BEGIN { FS="," }
{
for(i=1; i <=NF; i++)
   {
   if(i < 8 || (i > 8 && i < 21)  || (i > 21 && i < NF ) )
      printf("%s,", $i)

   if(i == NF)
      printf("\n")

   if( (i == 8) || (i == 21) )
      printf("%s,%s,", $i,$i)
   }
} ' mydata55.txt
 
Thanks for the input. Works great...

However, I do need to take this beyond i=21, i.e., 34, 47, 60, 73, etc.

Basically, it would be i=(8 + (13 * x)), where x=0,1,2,3,4,5,etc.

A quick tip on how to set this up within the awk statement would be awesome.

Thanks
 
One way to create a shell script which dynamically creates the awk script based on a string - "8 21 34 47" in this case. Obviously there are enhancements that can be done to this:

Code:
#!/bin/ksh

file_name="theawk.ss"
echo "nawk ' BEGIN { FS=\",\" }
{
for(i=1; i <=NF; i++)
   {
" > $file_name

num=4  # number of arguments in the mystr string"
# you must have an even number of arguments
mystr="8 21 34 47"

args=0
cnt1=0
cnt2=0
for str in $mystr
do
   ((args+=1))
   if [[ args -eq 1 ]]
   then # build the beginning of the if statements
      cnt1=$str
      firstif="   if(i < $str || "
      secif="   if( (i == $str) "
      continue
   fi

   secif="${secif} || (i == ${str}) "

   if [[ cnt1 -eq 0 ]]
   then
      cnt1=$str
   elif [ $cnt2 -eq 0 ]
   then
      cnt2=$str
   fi

   if [[ $cnt1 -gt 0 && $cnt2 -gt 0 ]]
   then
      fstr=$(echo "(i > cnt1 && i < cnt2) || "| sed 's/cnt1/'"$cnt1"'/')
      fstr=$(echo "$fstr" | sed 's/cnt2/'"$cnt2"'/')
      firstif="${firstif}${fstr}"
      cnt1=$cnt2
      cnt2=0
   fi

   if [[ $args -eq $num ]]
   then # terminate the if statements
      fstr=$(echo "(i > cnt1 && i < NF )) "| sed 's/cnt1/'"$cnt1"'/')
      firstif="${firstif}${fstr}"
      secif="${secif} ) "
      break
   fi

done

# finish the program
echo "$firstif" >> $file_name
echo "      printf(\"%s,\", \$i)

" >> $file_name

echo $secif >> $file_name
echo "      printf(\"%s,%s,\", \$i,\$i)
" >> $file_name

echo "   if(i == NF)
      printf(\"\\\n\")
" >> $file_name

echo "   }
} ' mydata55.txt" >> $file_name

# execute $file_name
chmod 755 $file_name
./$file_name


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top