Sponsored Content
Top Forums Shell Programming and Scripting Manipulating word based off of contents Post 302373169 by m1xram on Thursday 19th of November 2009 04:12:17 PM
Old 11-19-2009
filter

# Filter out non @ words on Key: records.

cat FILE | sed '/^Key:/s/ \+[^@ ]\+[@][^@ ]\+ \?//g'

# This is crude though, an AWK solution that splits and trims first by ':'
# and then by ',' would be much more exact. Will work on it.

---------- Post updated at 02:12 PM ---------- Previous update was at 12:44 PM ----------

# Ok, how about a PERL solution?
# Save below program as SOMETHING.pl
chmod +x SOMETHING.pl

# Example:
cat FILE | ./SOMETHING.pl -v -p '@'
# -v, Reverse match.
# -p PATTERN, Default is '@'
----------------------------------------------------------
Code:
#!/usr/bin/perl 

use Getopt::Std;

my (@in, @word, @out);
my $pat;
my $reverse = 0;

getopts("vp:");
if (defined($opt_v)) {
    $reverse = 1;
}
if (defined($opt_p)) {
    $pat = $opt_p;
} else {
    $pat = '@';
}

while(<STDIN>) {
    $line = $_;
    chomp $line;
    if ($line =~ /^Key:/) {
    @in = ();
    @in = (split(/: */, $line));
    @word = ();
    @word = (split(/, */, $in[1]));
    @out = ();
    foreach my $item (@word) {
        if ($reverse eq 0 && ($item =~ m/$pat/)) {
        push(@out, $item);
        } elsif ($reverse eq 1 && ($item !~ m/$pat/)) {
        push(@out, $item);
        }
    }
    print "$in[0]: ", join(", ", @out), "\n";
    }
    else {
    print "$line\n";
    }
}
exit 0;

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using first word and print their contents using awk

suppose u hava a file G1354R tGGC-CGC D1361N cGAC-AAC I1424T ATC-ACC R768W gCGG-TGG Q1382R CAG-CGG Q178E gCAG-GAG Y181C TAC-TGC .........cont. So the question is By searching for first word i.e.character say R output shud be R768W gCGG-TGG R182P CGG-CCG R189W ... (6 Replies)
Discussion started by: cdfd123
6 Replies

2. Shell Programming and Scripting

need help with finding a word in file's contents

Hi, I need to check if a particular name is already in the file or not and i am using following code for this... match=$(grep -n -e "$output1" outputfiles.txt ) where output1 is the variable name having names in it and outputfiles.txt is the file name ..and i am using ksh can anybosy... (6 Replies)
Discussion started by: manmeet
6 Replies

3. Shell Programming and Scripting

parsing a file and manipulating the contents

Hi I have a text file as follows BOB 14/14 TOM 94/94 SAM 3/3 CRIS 13/13 TOM 6/6 CRIS 27/27 SAM 2/2 JACK 25/25 CRIS (6 Replies)
Discussion started by: shellignorant
6 Replies

4. Shell Programming and Scripting

Merging files based on the contents

Hi, I have a file f1 having the contents as below select (<condn>) from dual I have another file f2 having the contents as below 1, 2, 3 I want to replace <condn> in f1 with the contents of f2 I tried using sed like this sed "s:<condn>:`cat f2`:g" f1 The above command resulted in sed:... (3 Replies)
Discussion started by: mr_manii
3 Replies

5. Shell Programming and Scripting

Renaming Files Based on Contents

Hello everyone, I currently have a situation which is causing me some issues for keeping up with certain files. I will explain this to the best of my abilities. I have a list of files as follows 50_REPORT_1111 - file contains the word Car 50_REPORT_2222 - file contains the word House... (15 Replies)
Discussion started by: DerangedNick
15 Replies

6. Shell Programming and Scripting

How to fetch rows based on line numbers or based on the beginning of a word?

I have a file which will have rows like shown below, ST*820*316054716 RMR*IV*11333331009*PO*40.31 REF*IV*22234441009*xsss471-2762 DTM*003*091016 ENT*000006 RMR*IV*2222234444*PO*239.91 REF*IV*1234445451009*LJhjlkhkj471-2762 </SPAN> DTM*003* 091016 RMR*IV*2223344441009*PO*40.31... (18 Replies)
Discussion started by: Muthuraj K
18 Replies

7. Shell Programming and Scripting

Remove specific word from data contents problem asking

Hi, Below is my input file: >tempZ_1 SAFSDAFDSG GGERRTTZZZ ZASRARARET WETPOASDAZ ZZZASASFAS >temp_9 ASAEGPIOEO EIOPIZZZAS FDFGZZZARA ESEAZZZAAS . . Desired output file: (9 Replies)
Discussion started by: patrick87
9 Replies

8. Shell Programming and Scripting

copy contents of unix file to Word document

Hello, I have a unix file about 3000lines which i want to copy from and paste it into a Word document. If i cat the file and try to scroll through it then not everything is captured so i am getting and incomplete paste. Any help is really appreciated. jak (2 Replies)
Discussion started by: jakSun8
2 Replies

9. UNIX Desktop Questions & Answers

Manipulating a list of numbers based on values

Hi, I have a single column of numbers from in tabulated text format ranging from 0 to 1. I want to manipulate the list of numbers so that if the number is greater than 0.5 (> 0.5), I get 1 - number. If the number is less than 0.5, the number is taken as it is and not altered. For example: ... (1 Reply)
Discussion started by: evelibertine
1 Replies

10. Shell Programming and Scripting

File comparison based on contents

Hi I have 2 files 1.del ---- 1,2,3,4,5 1,2,3,4,4 1,1,1,1,2 2.del ---- 1,2,3,4,5 1, 1,2,3,4,4 1,1,1,1,2 I need to compare the above two files in unix, as in the output should only tell the difference in contents as I should get only the line 1 ( from 2.del) , rest all lines are... (4 Replies)
Discussion started by: Ethen561
4 Replies
Regexp::Common::list(3) 				User Contributed Perl Documentation				   Regexp::Common::list(3)

NAME
Regexp::Common::list -- provide regexes for lists SYNOPSIS
use Regexp::Common qw /list/; while (<>) { /$RE{list}{-pat => 'w+'}/ and print "List of words"; /$RE{list}{-pat => $RE{num}{real}}/ and print "List of numbers"; } DESCRIPTION
Please consult the manual of Regexp::Common for a general description of the works of this interface. Do not use this module directly, but load it via Regexp::Common. $RE{list}{-pat}{-sep}{-lastsep} Returns a pattern matching a list of (at least two) substrings. If "-pat=P" is specified, it defines the pattern for each substring in the list. By default, P is "qr/.*?S/". In Regexp::Common 0.02 or earlier, the default pattern was "qr/.*?/". But that will match a single space, causing unintended parsing of "a, b, and c" as a list of four elements instead of 3 (with "-word" being "(?:and)"). One consequence is that a list of the form "a,,b" will no longer be parsed. Use the pattern "qr /.*?/" to be able to parse this, but see the previous remark. If "-sep=P" is specified, it defines the pattern P to be used as a separator between each pair of substrings in the list, except the final two. By default P is "qr/s*,s*/". If "-lastsep=P" is specified, it defines the pattern P to be used as a separator between the final two substrings in the list. By default P is the same as the pattern specified by the "-sep" flag. For example: $RE{list}{-pat=>'w+'} # match a list of word chars $RE{list}{-pat=>$RE{num}{real}} # match a list of numbers $RE{list}{-sep=>" "} # match a tab-separated list $RE{list}{-lastsep=>',s+ands+'} # match a proper English list Under "-keep": $1 captures the entire list $2 captures the last separator $RE{list}{conj}{-word=PATTERN} An alias for $RE{list}{-lastsep=>'s*,?s*PATTERNs*'} If "-word" is not specified, the default pattern is "qr/and|or/". For example: $RE{list}{conj}{-word=>'et'} # match Jean, Paul, et Satre $RE{list}{conj}{-word=>'oder'} # match Bonn, Koln oder Hamburg $RE{list}{and} An alias for $RE{list}{conj}{-word=>'and'} $RE{list}{or} An alias for $RE{list}{conj}{-word=>'or'} SEE ALSO
Regexp::Common for a general description of how to use this interface. AUTHOR
Damian Conway (damian@conway.org) MAINTAINANCE
This package is maintained by Abigail (regexp-common@abigail.be). BUGS AND IRRITATIONS
Bound to be plenty. For a start, there are many common regexes missing. Send them in to regexp-common@abigail.be. LICENSE and COPYRIGHT This software is Copyright (c) 2001 - 2009, Damian Conway and Abigail. This module is free software, and maybe used under any of the following licenses: 1) The Perl Artistic License. See the file COPYRIGHT.AL. 2) The Perl Artistic License 2.0. See the file COPYRIGHT.AL2. 3) The BSD Licence. See the file COPYRIGHT.BSD. 4) The MIT Licence. See the file COPYRIGHT.MIT. perl v5.18.2 2013-03-08 Regexp::Common::list(3)
All times are GMT -4. The time now is 03:04 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy