Re: insert file?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Nov 28, 2007 10:26 AM, adrian kok <adriankok2000@xxxxxxxxxxxx> wrote:
> Hi
>
> Please help how insert `who` in the third line of the
> file
>
> "head -n 3 file" << `who`
>
> Thank you
>

If you are talking about inserting it in the output on the screen then
you could do something like this where file is replaced with the file
you are working on.

head -n 3 file;who;tail -n $(echo "$(wc -l file | cut -d " " -f 1) -3" |bc) file

What I did was use the word count command (wc) to count the number of
lines and then used the cut command to grab only the first field in
the output of the wc command (being the actual number of lines in the
file).  I then used the bc command (a calculator) to substract 3 from
the total number of lines.  Finally the result of that calculation was
used in the tail command to display all but the first 3 lines of the
file.

if you want to insert it in the file itself then one way would be to
redirect the head command to a new file, then double redirect the who
command to the same file to append to it, then double redirect the
tail command to the new file and you are done.  So something like
this:

head -n 3 file >newfile;who>>newfile;tail -n $(echo "$(wc -l file |
cut -d " " -f 1) -3" |bc) file>>newfile

Jacques B.


[Index of Archives]     [Current Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]     [Fedora Docs]

  Powered by Linux