[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

script help for inserting a middle line

Posted on 2007-10-07
2
Medium Priority
?
241 Views
Last Modified: 2010-04-21
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??
0
Comment
Question by:FirstMan
2 Comments
 
LVL 9

Expert Comment

by:ghostdog74
ID: 20030330
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"
0
 
LVL 40

Accepted Solution

by:
omarfarid earned 2000 total points
ID: 20030609
Hi,

Try this:

echo "Enter new line of text:"

read line

echo "Enter file name:"

read file

if test -f $file
then
       len=`wc -l < $file`
       if test $len -gt 0
       then
              mid=`expr $len / 2`
              rem=`expr $len - $mid`
              head -$mid $file > temp.$$
              echo $line >> temp.$$
              tail -$rem $file >> temp.$$
              mv temp.$$ $file
       else
             echo $line > $file
       fi
else
       echo $file does not exists  or is not a file
fi
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses
Course of the Month18 days, 20 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question