Sponsored Content
Full Discussion: How to substitute?
Top Forums Shell Programming and Scripting How to substitute? Post 302268034 by vanitham on Sunday 14th of December 2008 11:12:18 PM
Old 12-15-2008
Hi,

Thanks a lot!!!


Quote:
Originally Posted by drl
Hi.

Here is a start that shows ParseWords:
Code:
#!/usr/bin/perl

# @(#) p1       Demonstrate parsing with quotes, operators AND, OR.
# http://search.cpan.org/~chorny/Text-ParseWords-3.27/ParseWords.pm

use warnings;
use strict;
use Text::ParseWords;

my ($debug);
$debug = 0;
$debug = 1;

my ( $i, $last, $line, @tokens );

while (<>) {
  chomp;
  @tokens = quotewords( '\s+', 0, $_ );
  print " input is :@tokens:\n" if $debug;
  $last = undef;
  foreach $i (@tokens) {
    if ( not defined($last) ) {
      $last = $i;
      print qin($i) . " ";
    }
    elsif ( $i eq "OR" or $i eq "AND" ) {
      $last = $i;
      print qin($i) . " ";
    }
    else {
      if ( $last ne "OR" and $last ne "AND" ) {
        print "AND " . qin($i) . " ";
      }
      else {
        print qin("$i") . " ";
      }
      $last = $i;
    }
  }
  print "\n";
}

print STDERR " ( Lines read: $. )\n" if $debug;

# qin - quote if necessary.

sub qin {
  my ($phrase) = $_[0];
  if ( $phrase =~ / / ) {
    return '"' . $phrase . '"';
  }
  else {
    return $phrase;
  }
}

exit(0);

Producing (on your data in file data1):
Code:
% ./p1 data1
 input is :apple bannana:
apple AND bannana
 input is :apple bannana AND chickko:
apple AND bannana AND chickko
 input is :milk shake OR Graphes orange:
"milk shake" OR Graphes AND orange
 ( Lines read: 3 )

See perldoc Text::ParseWords on your system or obtain from cpan as noted. It takes care of the quoted strings, placing all the tokens in a list.

If the output is not what you desire, feel free to modify or adapt the code as necessary ... cheers, drl
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Substitute in vi

I know in vi you can do :%s/replaceme/withthis/ but if i want to find all lines say without a # at the begining and I want to put it in how would that command be formatted? I can't figure it out for the life of me. #comment blah1 hey1 grrr1 #comment #blah1 #hey1 #grrr1 (5 Replies)
Discussion started by: kingdbag
5 Replies

2. UNIX for Dummies Questions & Answers

Substitute Variable

Can anyone tell me what is the purpose of a substitute variable in the unix programming language and give an example where it may be used? Thanks! (0 Replies)
Discussion started by: mmg2711
0 Replies

3. UNIX for Dummies Questions & Answers

rm substitute with blacklist

hi! first i want to apologize for two things. my English and (possible) posting in the wrong sub forum (i couldn't find one that fits my question) I am needing a script that can substitute rm. the idea is that the operator can/must delete some folders and files from time to time, when the free... (2 Replies)
Discussion started by: broli
2 Replies

4. Shell Programming and Scripting

In Help, Substitute Text ...

i'm writing a script that will extract and substitute a certain part of a data. i'm having trouble with the substituting part ... Here's my data looks like: 01/01/08-001-23:46:18-01/01/08-23:50:43 01/01/08-003-23:45:19-01/01/08-23:55:49 01/01/08-005-23:52:18-01/01/08-23:58:52 i want to... (6 Replies)
Discussion started by: solidhelix08
6 Replies

5. UNIX for Dummies Questions & Answers

substitute file name

correct file names are: *_0.txt *_1.txt incorrect file names are: *_12.txt *_0123.txt *_04321.txt all files that are incorrect need to replace the ending with *_1.txt therefore need to create a loop to find the wrong files in a dir ->that dont end in _1.txt or _0.txt and then... (3 Replies)
Discussion started by: sigh2010
3 Replies

6. Shell Programming and Scripting

vi substitute

My question is how would I substitute for ceratain number of occurences in a line? If this is my input rjohns BFSTDBS01 Standard Silver NPRO30DINCR 2 Client Is it possible to change the first 3 occurences of space " " to a comma? (7 Replies)
Discussion started by: reggiej
7 Replies

7. UNIX for Dummies Questions & Answers

substitute (')

I usually use : Code: awk '{gsub(/xxx/,"yyy");print}' to substitute xxx with yyy. I have a problem substitute an expression like Code: x ' y Because of the ( ' ) Any idea on how to get over this problem? Thanks (2 Replies)
Discussion started by: cosmologist
2 Replies

8. UNIX for Dummies Questions & Answers

Substitute in VI

Hi there, i am updating a file on UNIX and have many lines as per below : listen:x:37:4:Network Admin:/usr/net/nls: i would like to substitute from the :/usr to the end of the line. so at the moment im using this : :s/"\/$/ /g but i get an error.can anyone help? thank you (3 Replies)
Discussion started by: brian112
3 Replies

9. Shell Programming and Scripting

Grep and substitute?

I have to parse ASCII files, output the relevant data to a comma-delimited file and load it into a database table. The specs for the file format have been recently updated and one section is causing problems. This is the original layout for that section. ... (2 Replies)
Discussion started by: alan
2 Replies

10. How to Post in the The UNIX and Linux Forums

Prstat substitute

Hello All, I have a shell script where with the lines below: echo "${v_sd_dateTime},${RUN_QUEUE_SIZE},${LOAD_AVERAGE},${v_sd_load_list},${v_sd_thread_count_list}" >> ${v_sd_file} Format of the output : 01/05/2005 08:00:00, RUN_QUEUE_SIZE, LOAD_AVG, CPU_PROD1, CPU_PROD2, THREADS_PROD1,... (1 Reply)
Discussion started by: Supriya_84
1 Replies
Quote(3pm)						User Contributed Perl Documentation						Quote(3pm)

NAME
XML::Quote - XML quote/dequote functions SYNOPSIS
use strict; use XML::Quote qw(:all); my $str=q{666 > 444 & "apple" < 'earth'}; print xml_quote($str)," "; # 666 &gt; 444 &amp; &quot;apple&quot; &lt; &apos;earth&apos; my $str2=q{666 &gt; 444 &amp; &quot;apple&quot; &lt; &apos;earth&apos;}; print xml_dequote($str2)," "; # 666 > 444 & "apple" < 'earth' my $str3=q{666 > 444 & "apple" < 'earth'}; print xml_quote_min($str3)," "; # 666 > 444 &amp; &quot;apple&quot; &lt; 'earth' DESCRIPTION
This module provides functions to quote/dequote strings in "xml"-way. All functions are written in XS and are very fast; they correctly process utf8, tied, overloaded variables and all the rest of perl "magic". FUNCTIONS
$quoted = xml_quote($str); This function replaces all occurences of symbols '&', '"', ''', '>', '<' to '&amp;', '&quot;', '&apos;', '&gt;', '&lt;' respectively. Returns quoted string or undef if $str is undef. $dequoted = xml_dequote($str); This function replaces all occurences of '&amp;', '&quot;', '&apos;', '&gt;', '&lt;' to '&', '"', ''', '>', '<' respectively. All other entities (for example &nbsp;) will not be touched. Returns dequoted string or undef if $str is undef. $quoted = xml_quote_min($str); This function replaces all occurences of symbols '&', '"', '<' to '&amp;', '&quot;', '&lt;' respectively. Symbols ''' and '>' are not replaced. Returns quoted string or undef if $str is undef. EXPORT
xml_quote(), xml_dequote() are exported as default. PERFORMANCE
You can use t/benchmark.pl to test the perfomance. Here is the result on my P4 box. Benchmark: timing 1000000 iterations of perl quote, xs quote... perl quote: 108 wallclock secs (88.08 usr + 0.01 sys = 88.09 CPU) @ 11351.64/s (n=1000000) xs quote: 20 wallclock secs (16.78 usr + 0.00 sys = 16.78 CPU) @ 59591.20/s (n=1000000) Benchmark: timing 1000000 iterations of perl dequote, xs dequote... perl dequote: 106 wallclock secs (85.22 usr + 0.09 sys = 85.31 CPU) @ 11721.54/s (n=1000000) xs dequote: 19 wallclock secs (15.92 usr + 0.02 sys = 15.94 CPU) @ 62743.13/s (n=1000000) AUTHOR
Sergey Skvortsov <skv@protey.ru> SEE ALSO
http://www.w3.org/TR/REC-xml <http://www.w3.org/TR/REC-xml>, perlre COPYRIGHT
Copyright 2003 Sergey Skvortsov <skv@protey.ru>. All rights reserved. This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.14.2 2008-06-26 Quote(3pm)
All times are GMT -4. The time now is 04:18 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy