Computing.Net > Forums > Unix > need help for sed

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

need help for sed

Reply to Message Icon

Name: jds
Date: February 5, 2008 at 13:38:27 Pacific
OS: windowsXP
CPU/Ram: petium III/ram 256
Comment:

Hi,
I am trying to add line number in front of each line for a file. I tried this :
# number each line of a file (number on left, right-aligned)
sed = filename | sed 'N; s/^/ /; s/ *\(.\{6,\}\)\n/\1 /'

But this command give me line number like bellow:
1 asdasdas
2 sdfsdfsd
....
10 sdfasdf
11 sdfsdf

How can make the line number like this:
1010 asdasdas
1020 sdfsdfsd
....
1110 sdfasdf
1120 sdfsdf
...
basicly do some math to it:)
Thanks in advance



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: February 5, 2008 at 21:36:41 Pacific
Reply:

I see no way of solving your problem with sed. It's definitely possible with awk; simply create two variables - one with the starting integer, and the other with the increment. Simply cycle thru the file, printf the beginning integer, and increment the integer by the increment variable printf'ing each line.


0

Response Number 2
Name: Luke Chi
Date: February 7, 2008 at 13:37:24 Pacific
Reply:

$ nl -i10 -v1000 infile.txt

Example:

$ cat infile.txt
1
2
3
4
5
6
7
8
9
10
11
12

$ nl -i10 -v1000 infile.txt
1000 1
1010 2
1020 3
1030 4
1040 5
1050 6
1060 7
1070 8
1080 9
1090 10
1100 11
1110 12

Luke Chi


0

Response Number 3
Name: jds
Date: February 9, 2008 at 21:53:14 Pacific
Reply:

Thanks very much Nails and Luke.
The Luke's way perfect solved my issue.
I did reply a few days ago but for some reason it sidn't posted successfully.
Anyway, thanks very much


0

Response Number 4
Name: Luke Chi
Date: February 12, 2008 at 17:33:34 Pacific
Reply:

Glad to hear that

Luke Chi


0

Response Number 5
Name: malli.eskala
Date: February 18, 2008 at 04:37:08 Pacific
Reply:

its really excelent Luke.

Regards,
mallikarjuna setty


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: need help for sed

need help for sed www.computing.net/answers/unix/need-help-for-sed/7979.html

need help on SED script www.computing.net/answers/unix/need-help-on-sed-script/7487.html

help with a sed function www.computing.net/answers/unix/help-with-a-sed-function/4297.html