Link to home
Start Free TrialLog in
Avatar of kotkova
kotkova

asked on

editing an ASCII file

Hello,
I have this ASCII file with 4 alphanumeric columns like this:

KEY     NAME      AGE   POINTS
A1250   Ariane1    30    250
B3200   Ben3       32    200
etc...

From a shell script, how can I add and delete lines without affecting the other lines.  I cant add lines with the cat fonction because it will erase all the other entries...
Same thing with deleting a line by line number...
Can you please help?

Thank you
Avatar of mnashadka
mnashadka

To append lines, you can use the >> operator, like in:
echo "A4111 Bob1 25 300" >> myfile
To erase lines, you have to write the data to another file, and then move that file back to the original name.  I'm not sure of your requirements, but you might be able to use something like grep -v on the name:
grep -v Bob1 myfile > myfile.tmp
mv myfile.tmp myfile
Otherwise awk or sed might be good for you.
Avatar of kotkova

ASKER

OK,that does make sens...
But wont grep -v Bob1 myfile > myfile.tmp
just take the name Bob1 and not the hole line?

Thnx
Avatar of F. Dominicus
No grep will give you the whole line

Regards
Friedrich
ASKER CERTIFIED SOLUTION
Avatar of mnashadka
mnashadka

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kotkova

ASKER

Thank you so much, I really appreciate it!!!
Avatar of kotkova

ASKER

Btw, I dont have to do the grep only by the first colomn of every line right? I mean, I can do a grep by name or points and it will still take the hole line?
Yes, that's true.
Avatar of kotkova

ASKER

hey, btw, is there anyway I can get more points witout buying them, is the only option for me is to answer questions?
thnx    
I think you have to answer the questions or buy the points.  I'm not sure of any other way.
Avatar of kotkova

ASKER

hey, btw, is there anyway I can get more points witout buying them, is the only option for me is to answer questions?
thnx    
Avatar of kotkova

ASKER

Another little qurestion....
suppose Im entering an entry like this:

echo "$name      $age        $points" >> result

how can I generate a key that would be unique to every entry, having the 2 first lettres of the name and the followed by a number, something like this:

KEY     NAME      AGE   POINTS
BE01   ben        30    250
BE02   Ben        32    200

how can I do that wen this is how Im entering my infos:
echo "$name      $age        $points" >> result

Thnx again!
To make a key like that is a little more work.  You can extract the first 2 characters by using awk:
name=BEN
key=$( echo $name | awk '{ print substr($0, 0, 2) }' )
Then you could use that to get the max number in the file, like:
count=$( grep "^$key" result | awk '{ print substr($1, 3, length($1) - 2) }' | sort -n | tail -1 )
if [[ -z $count ]] ; then
  count=1
else
  (( count = count + 1 ))
fi
Add 1 to this and then use the key and concatenate that to the key.
key=$key$count

There might be an easier way, but I can't think of it right now.
Avatar of kotkova

ASKER

Thanks, ;-)

Dont I have to change the heading of my executalbe file to use awk? I write this for my heading now:
#!/bin/sh
but I know that for awk I need something else dont I?
BTW mnashadka, thank you sooo much for all the help so far... I really appreciate!

Anna
awk works inside regular shell scripts.  If the whole program is awk, and you don't want to use the awk syntax, then you have to change the heading.  The way I wrote it you can use awk.  The syntax I used was for a ksh script though, so you'd have to change the heading (to ksh) or change the if statements to use test instead of the [[ ]] syntax.  Good luck.
Avatar of kotkova

ASKER

so it would be #!/bin/ksh I imagine since I dont really use awk.  But in that case, if my file is called hello, as a Bourne shell script I executed itlike this:
./hello

in the case of an awk or ksh file, what woyld be the syntaxe to execute?

thnx  :-)
It would be the same.  ./hello
Avatar of kotkova

ASKER

ok, well this is what I do but I get this error:
bash: ./test: No such file or directory

and test contains exaclty this:
#!/bin/ksh

name=Ford
key=$( echo $name | awk '{ print substr($0, 0, 2) }' )
count=$( grep "^$key" result | awk '{ print substr($1, 3, length($1) - 2) }' | sort -n | tail -$
if [[ -z $count ]] ; then
count=1
else
(( count = count + 1 ))
fi

key=$key$count

echo $key

I checked the spelling and all that and the error does not make any sense
Does ksh live in this directory?  Do a "which ksh" to find out where it lives.  Since you're in Linux, you could also use bash for the same thing (it accepts the same test formatting).
Avatar of kotkova

ASKER

oh shoot...I just checked and we dont have korn...  :,-(
I did which ksh and it did :
which: no ksh in (/bin:.....)
crap...  Am I doomed?
Avatar of kotkova

ASKER

Also, I did is sepratly as a awk file and I get theese errors...
awk: cmd. line:1: ./test
awk: cmd. line:1: ^ parse error
awk: cmd. line:1: ./test
awk: cmd. line:1:   ^ unterminated regexp

can u plz help?
Avatar of kotkova

ASKER

Ok, sorry, my stupid mistake...
Now its all good and it works, Thank you so much for all your help. Sounds cheezy but I really mean it...  Im really bad @ shell but if ever you need help in C/C++ or assembler, I would love to help...@ least to give back what I received...  Tanx again  :0D
I'm glad it works for you.  I agree with you.  I often write things that other people would put into shell scripts in C++.  Shell scripts are very powerful, but shouldn't be used for everything (especially if performance is an issue).  Good luck with this project.