ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
As you see in
awk -f clean-up.awk phonelist > phonelist.new
it creates a phonelist.new file and leaves it up
to you to replace the old one if it's correct.
As for the learning ... there's a pretty good book
out there by Ellie Quigley, "Unix shells by example",
that covers grep, sed and awk (not exhaustively)
but with good explanations.
Originally posted by nazs Thank you to everyone who has responded . You all are awesom. I will try it out again when i go to work tomorrow. Does anyone have any recomendations on how to learn to write scripts like this. (Ex. books or web pages, or cdroms) I would love to learn this stuff. Please let me know. I will get back to you all and let you know how things work and i definitely will try it out on a test_copy first.
Nazs
A place to start would be to type
Code:
info
in a terminal. This would show you the utilities available, and a description of how to use them.
Awk is an awesome tool, but sometimes its overkill
Run this script
Code:
#!/bin/bash
FULL_LIST=/path/to/list
DEL_LIST=/path/to/names
for i in $(cat $DEL_LIST); do
x=$(grep -n $i $FULL_LIST | cut -d ":" -f 1)
while [ $(sed -n $x'p' $FULL_LIST) ]; do
sed -ie $x'd' $FULL_LIST
done
done
on your copy.
Use the man pages and/or info pages for grep, cut and sed to see how these tools are being used. There are plenty of bash tutorials on line that will show you how to write a script using them.
That is a beauty ahh and I have been severily outclassed!
I looked at the option p for sed but didn't quite catch the fact that this is what I needed to get to the next blank line. That's why I was having to manually put in the start and stop lines.
Originally posted by ahh
Awk is an awesome tool, but sometimes its overkill :)
Your shell version leaves tons of blank lines
behind, and it takes longer to run ...
Code:
[tink@diggn:~/tmp4]$ time test.awk testing.ori > testing.2
real 0m0.004s
user 0m0.000s
sys 0m0.000s
[tink@diggn:~/tmp4]$ time ./rem.sh
real 0m0.033s
user 0m0.000s
sys 0m0.030s
Originally posted by Tinkster Your shell version leaves tons of blank lines
behind, and it takes longer to run ...
...
I still like the awk version better
Yeah, I know its slower and leaves blank lines, but its shorter and easier, and the blank lines are easily removed with 'cat -s'.
Tink i have a question on your revised script. iIf i have one file with the list of names to be deleted and also a file with all the names and nubers it needs to go through to make the deletions from. Where do i place the path to the file with the names to be deleted and also where do i put the path to the list with all the names and numbers? i know i am suppose to put paths where it says /path/to/killem just not sure where what goes where.
Originally posted by nazs Tink i have a question on your revised script. iIf i have one file with the list of names to be deleted and also a file with all the names and nubers it needs to go through to make the deletions from. Where do i place the path to the file with the names to be deleted and also where do i put the path to the list with all the names and numbers? i know i am suppose to put paths where it says /path/to/killem just not sure where what goes where.
Thanks Nazs
The name of the file with the names to be deleted is the killem
in my /path/to/killem ...
The file you want to EDIT goes as a parameter to the awk-script.
If you change the awk to look like that:
Code:
#!/usr/bin/awk -f
BEGIN {
count = 0
while (( getline < "killem") > 0 ){
count += 1
name[count]=$1
}
close( "killem" )
RS=""; FS="\n";OFS="\n\n"
}
{
flag = 1
for ( i=1; i<=count;i++){
if($1 ~ name[i]) flag = 0
}
if( flag == 1 ) {print $0; printf "\n"}
}
and make it executable (chmod u+x FILE) you can actually
run it like you would have done the shell-script, too.
./FILE phonelist > newlist
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.