Solved

a custom script

Posted on 2003-11-20
7
203 Views
Last Modified: 2010-04-22
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
118,1000,81,87,"",118,00304,RL

new line should read
0,118,1000,81,87,"",118,00304,RL
next reads 1, then 2, and the data from the line read

thanks in advance
0
Comment
Question by:aot2002
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 34 total points
ID: 9794046
i=0
cat filename | while read line
do
     echo $line | sed "s/\(.*\)/$i,\1/" >> newfile
     let i = i + 1
done
0
 
LVL 7

Assisted Solution

by:glassd
glassd earned 35 total points
ID: 9798432
Just to make things a liitle simple, change line:

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

to

echo "$i,$line" >> newfile

Alternatively use this construct:

i=0
cat filename | while read line
do
     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.
0
 
LVL 7

Expert Comment

by:glassd
ID: 9798662
Of course:

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

or:

nl -ba -s, filename > newfile

Still working on sed, something to do with "=". Not sure how to suppress the newline though.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:glassd
ID: 9798752
Got it. Courtesy of:

http://www.mousetrap.net/syllabus/interunix/day3.html

sed = filename | sed 'N;s/\n/,/' > newfile
0
 
LVL 1

Author Comment

by:aot2002
ID: 9800259
had to use
#!/bin/sh

and also

i++

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

im grading with a B
0
 
LVL 7

Expert Comment

by:glassd
ID: 9800629
In sh

x=`expr $x + 1`
0
 
LVL 1

Author Comment

by:aot2002
ID: 9801091
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question