a custom script

i need a custom script to perform a few functions

it must add a counter key starting from key 0 to the end of the file

example in php i would use a for(i=0; i!=EOF; i++) statement

heres the first line of the file and all is consistant except i need a incrementing key to be produced .....

oringinal line of file

new line should read
next reads 1, then 2, and the data from the line read

thanks in advance
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cat filename | while read line
     echo $line | sed "s/\(.*\)/$i,\1/" >> newfile
     let i = i + 1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Just to make things a liitle simple, change line:

echo $line | sed "s/\(.*\)/$i,\1/" >> newfile


echo "$i,$line" >> newfile

Alternatively use this construct:

cat filename | while read line
     echo "$i,$line"
     let i = i + 1
done > newfile

I'm not at a Unix machine at the moment, but I am sure that sed does have method if inserting the line number into the line. I'll look into it ang report back.

It's Unix, so there are lots of ways.
Of course:

awk '{print NF","$0}' filename > newfile


nl -ba -s, filename > newfile

Still working on sed, something to do with "=". Not sure how to suppress the newline though.
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Got it. Courtesy of:


sed = filename | sed 'N;s/\n/,/' > newfile
aot2002Author Commented:
had to use

and also


let i = i + 1 wouldnt work it wouldnt increase the counter ???

im grading with a B
In sh

x=`expr $x + 1`
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux OS Dev

From novice to tech pro — start learning today.