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.

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

  • 08-09-2007 07: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