Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

stuck

  • 06-09-2007 12: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