vlad

Hi people

I am trainee at a new job which requires me to use UNIX, am more than comfortable dealing with processes and so on, problem I am having is that files get to big 1000s of lines, and I am trying to number these lines using a script so I can deal with these files without losing track of which lines am looking at, problem is I cant get the number to go in front of the lines.

For example assume the file has the contents as follows:
Line of text One
Line of text Two
Line of text Three
Line of text Four

I want it to look like this
1)Line of text One
2)Line of text Two
3)Line of text Three
4)Line of text Four

But what I keep getting is
1)
2)
3)
4)
Line of text One
Line of text Two
Line of text Three
Line of text Four

My employer doesn�t want me using SED or AWK :mad:, which is a shame because I got it to work easy with either of them.
Also temp variables wont do as the system is restarted every 2hrs, and the process am trying to achieve has to be automated.

Any tips or suggestions will greatly appreciated.

thanks

Since you are viewing the files, why dont you the 'set number' feature in vim.

:set number to be more precise.

thanks you the quick reply

i dont want to just view the files, i want to edit them using a simple shell script, nothing to fancy.

i dont really know what :set number is :P, i shall look into it.

thanks again

if editing the files is that way is not possible i can save them to new files with that numbered format, i.e read the input of a file and copy to a new.

firstly, show your code

secondly:

does your employer know any unix? If they know, then they should know sed/awk are essential tools that most admins use.
not tested,

v=0
while read  line
do
 v=$((v+1))
 printf "%s) %s\n"  $v  $line 
done < "file"

I dont get exactly what you are trying to say. But perhaps this may help.

nl file.txt > file_with_numbered_lines.txt

Also, if you have set nu, in your .vimrc, then all the lines will be numbered when you open the file. You can do editing as well.

Assume xx is the file with the relevant contents.
#!/bin/bash
Lines = wc-l << xx /* the number of lines from the file */
Number=0
while (($Number > $lines+1))
do
Number=$(($Number+1))
echo "$Number� /at this point how do I call the text on that line number/

/then output to any new file (not important at this point)/

done

This is the type of mini programme am trying to create, perhaps I am way over my head with this, but hey learning is a process :slight_smile:

This is looks like something I can :slight_smile:

I have a question though, the �while read line� statement, how does this get information from the file, is it from this �done < "file"�?.

sorry if am asking silly questions i only been using UNIX for a week now :stuck_out_tongue:

also can i use the grep to achieve this, if say i will be looking for occurrences of the current user who is executing the script?(in this case it should be me, but i can use it for others)

cat xx| grep -n .\*| grep "^1"

or something like that, i cant test it atm, but i think cat pipes file to 'grep -n' which inserts line numbers and pipes it to 'grep '^1' which gets the line 1. odds are this might fail as this is 1st time i am going to use grep :stuck_out_tongue:

Trying out grep and am confused as hell
I am using $user to save time.
Is this a valid method? xx is the file with contents

grep �i xx $user 

So form what am reading I need to use pipes (new to me)

grep �n xx 

will display the line numbers?

And

grep �I $user

should check for the current username in the file.

Do I need an other grep command to start from from line 1?

So I can use an if statement to check if the users is in the file, if so do the following:

 grep �n xx | grep �I $user | grep '^1' | >> newfile

Any feedback would be great ^^

Change employer, he sounds like a teacher by giving you arbitary restrictions.

if it was only that simple porter :cool:

if am not using SED or Awk, i have no idea what am doing in these situations.

Perl, C, C++, Python, Pascal, Fortran, Java, ....

If an employer says write a UNIX script but don't use sed then that is called micro-managing. If a teacher says that it's because he wants you to grasp certain concepts.

ok i am using this now, it works fine, but all i need to do is restrict the lines shown by a user, so am trying it with $USER for now.

#!/bin/bash/

x=0
while read line
do
x=$((x+1))
echo "$x ) $line"
done < "xx"

thx ghostdog74 :wink:

thanks everyone for your help it works like a charm now. :smiley: