Solved

a custom script

Posted on 2003-11-20
7
201 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
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
A "Helper" for python 2 91
How to ssh into my virtual box linux machine 4 150
"uptime" high (200+) on RHEL6 but CPU usage is low... any ideas why? 4 105
awk file 6 48
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

786 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