LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-21-2009, 05:46 AM   #1
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Rep: Reputation: 16
array - passing values to sed command


i grep this file and had to delete some of the results with sed - it worked fine

casper@kops11>egrep "cancel|modify|submit" evntlog.89655.Fri | sed -e '/Confirm/d' -e '/Third/d' -e '/find/d' -e '/Local/d' -e '/Auto/d' -e '/GEcp/d' -e '/BookConnectSServer/d' -e '/ExecReport/d'

just for investigation sake - i piped the grep results into a different file and then i tried to create a simple array loop that would sed out the line one by one out of the new file - is there a way to pass values to sed?

casper@kops11> vi simplearray
casper@kops11> more simplearray
#!/bin/sh

names=( Confirm Third find Local Auto GEcp BookConnectSServer ExecReport )
$(names[@])

for name in $(names[@])
do
sed '/"$name"/d' evntlog2.89655.Fri
done

casper@kops11> chmod +x simplearray
casper@kops11> simplearray
simplearray: line 4: names[@]: command not found
simplearray: line 9: names[@]: command not found
 
Old 08-21-2009, 05:59 AM   #2
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by casperdaghost View Post
casper@kops11> more simplearray
#!/bin/sh

names=( Confirm Third find Local Auto GEcp BookConnectSServer ExecReport )
$(names[@])

for name in $(names[@])
do
sed '/"$name"/d' evntlog2.89655.Fri
done

casper@kops11> chmod +x simplearray
casper@kops11> simplearray
simplearray: line 4: names[@]: command not found
simplearray: line 9: names[@]: command not found
The $( command ) part tells bash to run the string inside the brackets as a command. So right now your telling bash to run a variable as a command.
Either leave out the $( ) part or use $( echo names[@] ).


Cheers Zhjim
 
Old 08-21-2009, 06:09 AM   #3
rizhun
Member
 
Registered: Jun 2005
Location: England
Distribution: Ubuntu, SLES, AIX
Posts: 268

Rep: Reputation: 47
Or, do it in Perl:

IgnoreKeywords.pl:
Code:
#!/usr/bin/perl -w

my @names = ("Confirm", "Third", "find", "Local", "Auto", "GEcp", "BookConnectSServer", "ExecReport");

while (<STDIN>) {
  chomp;
  my $match = 0;

  foreach $name (@names) {
    if ( $_ =~ m/$name/ ) { $match++ }
  }

  if ( $match == 0 ) { print $_, "\n" }
}
Execution:
Code:
$ < evntlog.89655.Fri IgnoreKeywords.pl
 
Old 08-21-2009, 06:43 AM   #4
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by casperdaghost View Post
for name in $(names[@])
perhaps that should be
Code:
for name in "${names[@]}"
if you're going to read from a file it should be:
Code:
for name in $(<file)
or
Code:
while read name; do
   ...
done < file
or
Code:
# if you're going to read standard input
while read -u 4 name; do
   ...
done 4< file

Last edited by konsolebox; 08-21-2009 at 06:46 AM.
 
  


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
Passing float values in FIFO navderm Programming 3 01-04-2009 04:00 PM
perl - passing values to subroutines sporty Programming 7 08-08-2006 10:13 AM
Passing form values between multiple forms!! AskMe Programming 5 09-07-2005 08:44 PM
Sed Command & Passing Values joey52 Linux - Newbie 4 12-27-2004 08:46 PM
passing values in shell pantera Programming 1 05-20-2004 10:01 AM


All times are GMT -5. The time now is 09:12 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration