Link to home
Start Free TrialLog in
Avatar of FirstMan
FirstMan

asked on

script help for inserting a middle line

Hi,

I have a script problem which I have been struggling with. I want to amend a previously script with the main purpose of inserting a line of text in the middle of a given file, instead of the firstline.

The shell script that I want to amend is as follow:

echo "Enter new line of text:"

read line

echo "Enter file name:"

read file

if test -f $file
then
       echo $line > temp.$$
       cat $file >> temp.$$
       mv temp.$$ $file
else
       echo $file does not exists  or is not a file
fi

Can you help me??
Avatar of ghostdog74
ghostdog74

you can try this awk script

awk '{
   s[++c]=$0
}
END{
   for(i=1;i<=c;i++){
        if (i==int(c/2)+1) {
           print "my line inserted"
             print i, s[i]
        }
        else {
             print s[i]
        }
   }
}' "file"
ASKER CERTIFIED SOLUTION
Avatar of omarfarid
omarfarid
Flag of United Arab Emirates image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial