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??
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??
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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"