?
Solved

a custom script

Posted on 2003-11-20
7
Medium Priority
?
205 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 102 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 105 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
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

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

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…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month8 days, 17 hours left to enroll

765 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