By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,734 Members | 1,385 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,734 IT Pros & Developers. It's quick & easy.

Unix Adding Text to corresponding lines...

P: 16
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.thai
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.thai
2)MAIL=/var/spool/mail/norman.thai
3)PWD=/home/norman.thai
4)HOME=/home/norman.thai
Jan 15 '08 #1
Share this Question
Share on Google+
8 Replies

radoulov
P: 34
Expand|Select|Wrap|Line Numbers
  1. awk '$0=++c")"$0' filenorman.thai>_tmp&&mv _tmp filenorman.thai
Jan 15 '08 #2

P: 16
Expand|Select|Wrap|Line Numbers
  1. 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
Jan 16 '08 #3

radoulov
P: 34
It uses temporary file implicitly though ...

Expand|Select|Wrap|Line Numbers
  1. (rm file&&nl -s\) -w1>file)<file
Jan 16 '08 #4

P: 16
It uses temporary file implicitly though ...

Expand|Select|Wrap|Line Numbers
  1. (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.
Jan 16 '08 #5

radoulov
P: 34
i was told to look into loops.[...]
I'd search google for shell scripting tutorial ...
Jan 16 '08 #6

P: 16
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
Jan 16 '08 #7

radoulov
P: 34
Sure,
man nl says:

nl - number lines of files
[...]
-s, --number-separator=STRING
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.
Jan 16 '08 #8

P: 16
Sure,
man nl says:

nl - number lines of files
[...]
-s, --number-separator=STRING
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!
Jan 16 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.