Unix Adding Text to corresponding lines...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • thaiZilla
    New Member
    • Jan 2008
    • 16

    Unix Adding Text to corresponding lines...

    grep $USER xx >> filenorman.thai

    i used the above command to gain a searched list for a user and put it into a file called filenorman.thai , the following is a bit of its contents...

    USER=norman.tha i
    MAIL=/var/spool/mail/norman.thai
    PWD=/home/norman.thai
    HOME=/home/norman.thai

    My question is, how do i add the corresponding line number as well as a bracket to make the text look like this:-

    1)USER=norman.t hai
    2)MAIL=/var/spool/mail/norman.thai
    3)PWD=/home/norman.thai
    4)HOME=/home/norman.thai
  • radoulov
    New Member
    • Jun 2007
    • 34

    #2
    Code:
    awk '$0=++c")"$0' filenorman.thai>_tmp&&mv _tmp filenorman.thai

    Comment

    • thaiZilla
      New Member
      • Jan 2008
      • 16

      #3
      Originally posted by radoulov
      Code:
      awk '$0=++c")"$0' filenorman.thai>_tmp&&mv _tmp filenorman.thai
      Is there a method without awk cos i'm learning from the basics and i've used tmp files before but i want a way without using a tmp file. Thanks

      Comment

      • radoulov
        New Member
        • Jun 2007
        • 34

        #4
        It uses temporary file implicitly though ...

        Code:
        (rm file&&nl -s\) -w1>file)<file

        Comment

        • thaiZilla
          New Member
          • Jan 2008
          • 16

          #5
          Originally posted by radoulov
          It uses temporary file implicitly though ...

          Code:
          (rm file&&nl -s\) -w1>file)<file
          i was told to look into loops. I'm new tho i'll have a crack at what you put in.

          Comment

          • radoulov
            New Member
            • Jun 2007
            • 34

            #6
            Originally posted by thaiZilla
            i was told to look into loops.[...]
            I'd search google for shell scripting tutorial ...

            Comment

            • thaiZilla
              New Member
              • Jan 2008
              • 16

              #7
              Originally posted by radoulov
              I'd search google for shell scripting tutorial ...
              Hey thanks for your time, both codes you provided for me work, i vaguely understand the last one...i just don't understand why my tutorials would ask me to look at loops.

              If you have any time could you explain the nl -s\ command and w1 in your coding - (rm file&&nl -s\) -w1>file)<file

              Thanks again you have been helpful

              Comment

              • radoulov
                New Member
                • Jun 2007
                • 34

                #8
                Sure,
                man nl says:

                nl - number lines of files
                [...]
                -s, --number-separator=STRIN G
                add STRING after (possible) line number
                [...]
                -w, --number-width=NUMBER
                use NUMBER columns for line numbers



                The -s option let you specify the character after the number:
                ")" in our case, we need to escape it,
                because it's special for the shell, the -w option let you specify
                the number of columns for the line numbers, try the command
                without it to see the difference.

                Comment

                • thaiZilla
                  New Member
                  • Jan 2008
                  • 16

                  #9
                  Originally posted by radoulov
                  Sure,
                  man nl says:

                  nl - number lines of files
                  [...]
                  -s, --number-separator=STRIN G
                  add STRING after (possible) line number
                  [...]
                  -w, --number-width=NUMBER
                  use NUMBER columns for line numbers



                  The -s option let you specify the character after the number:
                  ")" in our case, we need to escape it,
                  because it's special for the shell, the -w option let you specify
                  the number of columns for the line numbers, try the command
                  without it to see the difference.
                  Ahhh thanks very much, much appreciated!

                  Comment

                  Working...