Replacing stopwords based on a list


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Replacing stopwords based on a list
# 1  
Old 03-26-2015
Error Replacing stopwords based on a list

Dear all,
I have Files with lines of text in them, I want to replace the stopwords in them with ",".
I have create a file which contain the stopwords...

I have been trying for last 3 hours but no success
I have managed to replace one using "sed" and delete the line containing them using "grep" but no actually the whole thing

failed examples
Code:
cat $X | sed "s/\<a\>/\,/g"

and
Code:
grep -v -F -f stopwords.txt mainfiles.txt > newfiles.txt


was wondering whether you can help me ...
Thanks a lot in advance

P.S. using sed I face the problem of case-sensitivity as well

Last edited by A-V; 03-26-2015 at 06:00 PM..
# 2  
Old 03-26-2015
You can replace multiple words with:
Code:
sed -E 's/(word1|word2|word3)/,/g'

Your sed version might support case insensitive matching:
Code:
sed -E 's/(word1|word2|word3)/,/gI'

Depending on how your list looks like, you can generate the sed statement automatically from your word list (example for comma separated):
Code:
sed -E 's/('`sed 's/,/|/g' wordlist`')/,/gI'

# 3  
Old 03-27-2015
Thank you Walter Misar for help... never thought of double sed...
used \b to include whole words as well Smilie
# 4  
Old 03-27-2015
Hi.

Here is an alternate solution with perl. The stop words are in a list, one pair to a line, separated by TAB. There is a "general string" which is supplied if no replacement string is specified. This driver script lists the data file, the replacements file, the perl code, and the results:
Code:
#!/usr/bin/env bash

# @(#) s2	Demonstrate easy replacement with perl.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && . $C perl

FILE=${1-data1}

pl " perl code:"
cat p2

pl " Input data file $FILE:"
cat $FILE

pl " Replacement pairs, old TAB new, unpaired get \"general string\":"
cat replacements.txt

pl " Results:"
./p2 $FILE

exit 0

producing:
Code:
$ ./s2

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian 5.0.8 (lenny, workstation) 
bash GNU bash 3.2.39
perl 5.10.0

-----
 perl code:
#!/usr/bin/env perl

# @(#) p2	Demonstrate replacement from hashed pairs.

use strict;
use warnings;
use Carp;

my ($f);
my $rf = "replacements.txt";
my $gs;    # general string, used if no replacement found.
$gs = "...";

# Read pairs into hash.
my (%hp);
open( $f, "<", $rf ) || croak "cannot open pairs file \"$rf\"";
my ( $t1, $t2 );
while (<$f>) {
  chomp;
  ( $t1, $t2 ) = split( /\s+/, $_, 2 );
  $hp{$t1} = defined($t2) ? $t2 : $gs;
}
close $f;

# Print hash if debugging needed.
# foreach $t1 ( keys %hp ) { print "$t1 => $hp{$t1}\n" };

# Read and replace.
while (<>) {
  foreach $t1 ( keys %hp ) {
    s/$t1/$hp{$t1}/gi;
  }
  print;
}

-----
 Input data file data1:
Now is the time
for all good men
to come to the aid
of their country.
aa aa cc bb cc bb aa AA Aa aA aa

-----
 Replacement pairs, old TAB new, unpaired get "general string":
Now	Then
is	was
good	manly
their	< yours, mine, ours >
aid	bandage
to
aa	xx
bb	yy
cc	zz

-----
 Results:
Then was the time
for all manly men
... come ... the bandage
of < yours, mine, ours > country.
xx xx zz yy zz yy xx xx xx xx xx

If one is interested, there is a perl module https://metacpan.org/release/String-Replace which may be a bit faster (claimed in the object-oriented mode), but I do not think that it allows case insensitivity.

Best wishes ... cheers, drl
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replacing tag based on condition

Hi All, I am having a file like below. The file will having information about the records.If you see the file the file is header and data. For example it have 1 men tag and the tag id will be come after headers. The change is I want to convert All pets tag from P to X. I did a sed like below... (5 Replies)
Discussion started by: arunkumar_mca
5 Replies

2. UNIX for Advanced & Expert Users

Replacing string length based on pattern

Hi All, I have a file which is like below. I need to read all the patterns that starts with P and then replace the 9 digit values to 8 digit values (remove leading integer). Can you please help Example : ( Please look below File) File : P,1 M1,... (7 Replies)
Discussion started by: arunkumar_mca
7 Replies

3. Shell Programming and Scripting

Replacing a character with a number based on lines

Hi, I am in need of help for the two things which is to be done. First, I have a file that has around four columns. The first column is filled with letter "A". There are around 400 lines in the files as shown below. A 1 5.2 3.2 A 2 0.2 4.5 A 1 2.2 2.2 A 5 2.1 ... (2 Replies)
Discussion started by: begin_shell
2 Replies

4. Shell Programming and Scripting

Help with awk replacing identical columns based on another file

Hello, I am using Awk in UBUNTU 12.04. I have a file like following with three fields and 44706 rows. F1 A A F2 G G F3 A T I have another file like this: AL_1 F1 A A AL_2 F1 A T AL_3 F1 A A AL_1 F2 G G AL_2 F2 G A AL_3 F2 G G BO_1 F1 A A BO_2 F1 A T... (6 Replies)
Discussion started by: Homa
6 Replies

5. Shell Programming and Scripting

Finding/replacing strings in some files based on a file

Hi, We have a file (e.g. a .csv file, but could be any other format), with 2 columns: the old value and the new value. We need to modify all the files within the current directory (including subdirectories), so find and replace the contents found in the first column within the file, with the... (9 Replies)
Discussion started by: Talkabout
9 Replies

6. Shell Programming and Scripting

Replacing the text in a row based on certain condition

Hi All, I felt tough to frame my question. Any way find my below input. (.CSV file) SNo, City 1, Chennai 2, None 3, Delhi 4,None Note that I have many rows ans also other columns beside my City column. What I need is the below output. SNo, City 1, Chennai 2, Chennai_new 3, Delhi... (2 Replies)
Discussion started by: ks_reddy
2 Replies

7. Shell Programming and Scripting

Replacing headers based on a second file

I have a file with thousands of sequences that looks like this: I need to replace the headers using a second file Thus, I will end up having the following file: I am looking for an AWK script that I can easily plug in my current pipeline. Any help will be greatly appreciated! (6 Replies)
Discussion started by: Xterra
6 Replies

8. UNIX for Dummies Questions & Answers

Script for replacing text in a file based on list

Hi All, I am fairly new to the world of Unix, and I am looking for a way to replace a line of text in a file with a delimited array of values. I have an aliases file that is currently in use on our mail server that we are migrating off of. Until the migration is complete, the server must stay... (8 Replies)
Discussion started by: phoenixjc
8 Replies

9. Shell Programming and Scripting

Replacing Character in a file based on element

Hi, I have file like below. Unix:/pclls/turc>cat tibc.property executeReceiver=Y executeSender=Y I want to replace executeSender=N in the file. My file should be like below. executeReceiver=Y executeSender=N I tried with the below command, its giving error. cat tibc.property |... (2 Replies)
Discussion started by: senthil_is
2 Replies

10. Shell Programming and Scripting

Replacing text based on replacement tables

Dear all, will be grateful for your advices.. The need is (i guess) simple for UNIX experts. Basically, there are replacement tables, which would be used to replace text strings in the data (large volumes..). An exmpl table (a "config file"): VIFIS1_1_PE1836 VIBRIO_FISCHERI VIPAR1_1_PE1662 ... (7 Replies)
Discussion started by: roussine
7 Replies
Login or Register to Ask a Question