Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Number lines in a file AWK
01-11-2010, 09:13 AM (This post was last modified: 01-11-2010 09:17 AM by codecaine.)
Post: #1
Number lines in a file AWK
lineNumber is global variable because its in the begin section. For each line lineNumber is incremented by 1. then I just made a simple printf statement printing the line count and the entire line. If you save the filename as myscript you can execute the program like this. Remember you must my shell scripts executable chmod 755 myscript
Code:
#!/bin/bash
./myscript /etc/passwd
If you want to save the output just simply do this.
./myscript /etc/passwd > numberOutput.txt
awk '

BEGIN{
    lineNumber=0;
}

{
    lineNumber++;
    printf("%d. %s\n",lineNumber, $0);          
}

' "$1"

Of course you can use cat in BASH to do the exact same thing. Each line is tabbed at the beginning though.
Code:
cat -n /etc/passwd
Visit this user's website Find all posts by this user
Quote this message in a reply
01-11-2010, 08:25 PM
Post: #2
RE: Number lines in a file AWK
This is almost the same as in C++.
It is interesting how it outputs it in a file !

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: