?
Solved

editing an ASCII file

Posted on 2003-03-09
22
Medium Priority
?
165 Views
Last Modified: 2010-04-21
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
0
Comment
Question by:kotkova
[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
  • 12
  • 9
22 Comments
 
LVL 8

Expert Comment

by:mnashadka
ID: 8100490
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.
0
 

Author Comment

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

Thnx
0
 
LVL 24

Expert Comment

by:fridom
ID: 8101130
No grep will give you the whole line

Regards
Friedrich
0
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 
LVL 8

Accepted Solution

by:
mnashadka earned 300 total points
ID: 8102824
As Friedrich said, it will take the whole line.  grep -v will give you all of the lines that don't match the pattern (which should be every other line in the file), allowing you to delete that line and keep the rest.
0
 

Author Comment

by:kotkova
ID: 8104838
Thank you so much, I really appreciate it!!!
0
 

Author Comment

by:kotkova
ID: 8104855
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?
0
 
LVL 8

Expert Comment

by:mnashadka
ID: 8105318
Yes, that's true.
0
 

Author Comment

by:kotkova
ID: 8105372
hey, btw, is there anyway I can get more points witout buying them, is the only option for me is to answer questions?
thnx    
0
 
LVL 8

Expert Comment

by:mnashadka
ID: 8105506
I think you have to answer the questions or buy the points.  I'm not sure of any other way.
0
 

Author Comment

by:kotkova
ID: 8105751
hey, btw, is there anyway I can get more points witout buying them, is the only option for me is to answer questions?
thnx    
0
 

Author Comment

by:kotkova
ID: 8106730
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!
0
 
LVL 8

Expert Comment

by:mnashadka
ID: 8107039
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.
0
 

Author Comment

by:kotkova
ID: 8107246
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
0
 
LVL 8

Expert Comment

by:mnashadka
ID: 8107386
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.
0
 

Author Comment

by:kotkova
ID: 8107424
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  :-)
0
 
LVL 8

Expert Comment

by:mnashadka
ID: 8107462
It would be the same.  ./hello
0
 

Author Comment

by:kotkova
ID: 8107519
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
0
 
LVL 8

Expert Comment

by:mnashadka
ID: 8107632
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).
0
 

Author Comment

by:kotkova
ID: 8107783
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?
0
 

Author Comment

by:kotkova
ID: 8107858
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?
0
 

Author Comment

by:kotkova
ID: 8108231
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
0
 
LVL 8

Expert Comment

by:mnashadka
ID: 8108405
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.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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 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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

801 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