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

removing a line from a file and then placing into another file

  • 08-09-2007 6:31pm
    #1
    Closed Accounts Posts: 91 ✭✭


    grep `whoami` $1 >> file

    this lets me take out the username from a file and then i move it to a file but i need it to do one step at a time because i want the occurences to be numbered like

    1)HOME=/home/joe.bloggs
    2)LOGNAME=joe.bloggs


    instead of just

    HOME=/home/joe.bloggs
    LOGNAME=joe.bloggs


Comments

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


    Is there a question in there? I'm a bit lost tbh.


  • Closed Accounts Posts: 91 ✭✭magnia


    grep `whoami` $1 >> file

    this is extract the information i need. everytime grep finds an occurence of the username it grabs that line of the file and then i move it to another file but how do i grab it and then add the number to it like if its the first occurence the number would be 1 and then add a bracket so it would look like

    1)HOME=/home/joe.bloggs
    2)LOGNAME=joe.bloggs

    how do i do that


  • Registered Users, Registered Users 2 Posts: 354 ✭✭AndrewMc


    magnia wrote:
    grep `whoami` $1 >> file

    this is extract the information i need. everytime grep finds an occurence of the username it grabs that line of the file and then i move it to another file but how do i grab it and then add the number to it like if its the first occurence the number would be 1 and then add a bracket so it would look like

    1)HOME=/home/joe.bloggs
    2)LOGNAME=joe.bloggs

    how do i do that

    You can use 'cat -n' to add line numbers, such as:
    grep `whoami` $1 | cat -n >> file
    

    The line numbers cat adds aren't quite in the same format as you describe. If you need exactly that format, you could use:
    grep `whoami` $1 | awk '{print NR")"$0}' >> file
    


  • Closed Accounts Posts: 91 ✭✭magnia


    yeah i had it done with awks and my gay tutor said were not allowed use them or seds anyone any ideas


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


    #!/bin/bash
    COUNTER=1
    for i in `grep \`whoami\` $1`
    do
            echo "$COUNTER)$i" >> file
            let COUNTER+=1
    done
    


  • Advertisement
Advertisement