Solved insert a pattern after two succesive lines in a file

June 28, 2014 at 13:18:19
Specs: Windows 7
hi all,

i have a file with below contents.


status() {
ipsec _realsetup status
RETVAL=$?
return $RETVAL
}

version() {
ipsec version
RETVAL=$?
return $RETVAL
}


# do it
case "$1" in
start|--start)
start
;;
stop|--stop)
stop
;;
restart|--restart)
restart
;;
reload|force-reload)
restart
;;
condrestart|try-restart)
condrestart
;;
status|--status)
status
;;
version)
version
;;
*)
echo "Usage: $prog {start|stop|restart|reload|force-reload|condrestart|try-restart|status|version}"
RETVAL=2
esac

exit $RETVAL
======================

some more information also present.

i want to insert one line before after the start line. in below info

case "$1" in
start|--start)
start
--------------------------------->here i want to insert.
;;

there are lot of start strings present in this file.

can u please tel me how to do it in shell script in linux OS.

thanks,
joe


See More: insert a pattern after two succesive lines in a file

Report •

#1
June 29, 2014 at 22:11:53
✔ Best Answer
How about a bash script that prints a new-line if two lines begin with the string 'start':

#!/bin/bash

sflag=0
while read line
do
   if [[ "$line" =~ ^start.*$ ]]
   then
      ((sflag++))
      printf "%s\n" "$line"
      continue
   fi

   if [[ $sflag -eq 2 ]]
   then
       echo
   fi
   printf "%s\n" "$line"
   sflag=0
done < datafile.txt

message edited by nails


Report •
Related Solutions


Ask Question