Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

a custom script

Posted on 2003-11-20
7
Medium Priority
?
207 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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 course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

604 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