ksh script print the line after at the same line

March 22, 2013 at 02:43:45
Specs: Windows 7
I HAVE FILE :
SAP 12222
SAP 34555
RING 5555
SAP 45454
SAP 88888
SAP 87654
RING 4332
I WANT TO PRINT IT LIKE THAT :

SAP 12222
SAP 34555-RING 5555
SAP 45454
SAP 88888
SAP 87654-RING 4332

when i have the word RING after the raw with SAP , i want to print it at the same line .


See More: ksh script print the line after at the same line

Report •

#1
March 22, 2013 at 12:28:56
A way is a korn shell script that sets a flag when the line starts with SAP and then process the lines that start with RING. Let me know if you have any questions:

#!/bin/ksh

sapflag=0
while read line
do
   if echo "$line"|grep "^SAP"  > /dev/null
   then
      if [ $sapflag -eq 1 ]
      then  # two SAP lines in a row
         printf "\n"
      fi
      printf "%s" "$line"
      sapflag=1
      continue
   fi

   if echo "$line"|grep "^RING"  > /dev/null
   then
      if [ $sapflag -eq 1 ]
      then
         printf "-%s" "$line"
      fi
      continue
   fi

   if [ $sapflag -eq 1 ]
   then # a line after an SAP line that doesn't start with RING
       printf "\n%s\n" "$line"
   else
       printf "%s\n" "$line"
   fi
   sapflag=0

done < datafile.txt
echo


Report •
Related Solutions


Ask Question