Solved

a custom script

Posted on 2003-11-20
7
198 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
  • 4
  • 2
7 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 34 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 7

Expert Comment

by:glassd
Comment Utility
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
Comment Utility
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
Comment Utility
In sh

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

Author Comment

by:aot2002
Comment Utility
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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 demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now