LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-20-2005, 12:28 AM   #16
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928

My thing is non-destructive

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.



Cheers,
Tink
 
Old 04-20-2005, 05:51 AM   #17
ahh
Member
 
Registered: May 2004
Location: UK
Distribution: Gentoo
Posts: 293

Rep: Reputation: 31
Quote:
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.

http://www.google.co.uk/search?hl=en&q=bash+tutorial

If you want a closer look at what is going on when the script runs, change the first line to
Code:
#!/bin/bash -x
and it will print out what it is doing.
 
Old 04-20-2005, 07:49 AM   #18
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
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.
 
Old 04-20-2005, 01:32 PM   #19
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
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
I still like the awk version better ;)


Cheers,
Tink
 
Old 04-20-2005, 04:59 PM   #20
ahh
Member
 
Registered: May 2004
Location: UK
Distribution: Gentoo
Posts: 293

Rep: Reputation: 31
Quote:
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'.

But I agree, yours is technically superior.
 
Old 04-20-2005, 06:38 PM   #21
nazs
Member
 
Registered: Apr 2005
Posts: 57

Original Poster
Rep: Reputation: 15
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
 
Old 04-20-2005, 06:59 PM   #22
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
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



Cheers,
Tink
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sed and escaping & in something like: echo $y | sed 's/&/_/g' prx Programming 7 02-03-2005 11:00 PM
Sed q? ky-lab_rat Linux - Software 5 10-26-2004 07:59 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM
Help with sed odious1 Linux - General 2 08-29-2003 10:52 AM
help with sed DavidPhillips Programming 3 08-11-2003 04:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration