Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

stuck

  • 06-09-2007 01:52AM
    #1
    Closed Accounts Posts: 91 ✭✭


    #!/bin/bash
    echo $1 | cat - $2 >> /tmp/$$ && mv /tmp/$$ $2

    im trying to get the first argument to go in the middle of the second argument which is a file, anyone any ideas.i have only managed to get it to go on the end or the front.

    been fiddling about with wc -l, i get the number of lines but not sure how i use that to insert the argument


Comments

  • Registered Users, Registered Users 2 Posts: 1,065 ✭✭✭Snowbat


    I'm curious why you'd want to add something in the middle of a file but...
    #!/bin/bash
    HALFWAY=$((`cat $2 | wc -l`/2))
    awk "NR <= $HALFWAY" $2 > /tmp/$$
    echo $1 >> /tmp/$$
    awk "NR > $HALFWAY" $2 >> /tmp/$$
    mv /tmp/$$ $2
    


  • Closed Accounts Posts: 91 ✭✭magnia


    cant use awk or seds but what u wrote looks good. trying to learn unix out of a book. this was just one of the questions at the end of a chapter. can it be done with awk or sed.


  • Registered Users, Registered Users 2 Posts: 1,065 ✭✭✭Snowbat


    Even my router has awk and sed available but you could do it this way:
    #!/bin/bash
    HALFWAY=$((`cat $2 | wc -l`/2))
    COUNTER=0
    for i in `cat $2`;
    do
            echo $i >> /tmp/$$
            if [ $COUNTER -eq $HALFWAY ]
            then
                    echo $1 >> /tmp/$$
            fi
            let COUNTER+=1
    done
    mv /tmp/$$ $2
    


  • Registered Users, Registered Users 2 Posts: 37,485 ✭✭✭✭Khannie


    tail has an option to specify number of lines. Off the top of my head, it's what I'd use.


Advertisement