Sponsored Content
Top Forums Shell Programming and Scripting sed parser behaving strange on replacing multiple words in multiple files Post 303008983 by sammy777888 on Friday 8th of December 2017 04:43:52 AM
Old 12-08-2017
sed parser behaving strange on replacing multiple words in multiple files

I have 4000 files like

Code:
$cat clus_grp_seq10_g.phy 

 18 1002
anig_OJJ65951_1     ATGGTTTCGCAGCGTGATAGAGAATTGTTTAGGGATGATATTCGCTCGCGAGGAACGAAGCTCAATGCTGCCGAGCGCGAGAGTCTGCTAAGGCCATATCTGCCAGATCCGTCTGACCTTCCACGCAGGCCACTTCAGCGGCGCAAGAAGGTTCCTCG
aver_OOF92921_1     ATGGTTTCGCAACGAGAT---------AGAGAATTGAATATCACGGCTTCCTCAGGGGTCTCTGGCATTATGCTGGTGCTCAGATGAGGTTTGGC
anid_EAW13573_1     ATGGTCTCACAGCGTGACAGAGAGTTGGCTGTTGAATACCAGGGCTATCTCAGGGGTTTGTGGCATTACGCTGGGGCCCAGATGCGATTTGGC
azon_EAW20028_1     ATGGCCCTAGCACGTGATAGAGAATTACTGAGGGACACTATTCGCACCCAAGGGACCGCACTTACTGCTGCCGATCGCGAAAATATCCTGAAGCCATATCTGCCGGATCCATCAGAACTTGCACGTCGGCCACTACAGCGACAGAAGAAAGC
awen_EED46037_1     ATGGTATCACAACGGGATAGAGTGGTGTGTCTGCC------------------------------------------------CTCTACAGGTCA------AAACAGTGCGAAATA---------AA
acar_EAL84889_1     ATGGCCCT
akaw_EAWE3573_1     ---------ATGGTCTCAC---------AGCGTGACAGAGAGT---------TGGCTGTTGAATACCAGGGCTATCTCAGGGGTTTGTGGCATTACGC

I want to replace 7 patterns (aver, anid, anig, acar, azon, awen, akaw) in all the files. The resulting file should be like (No change in file name)


Code:
$cat clus_grp_seq10_g.phy 

 18 1002
anig     ATGGTTTCGCAGCGTGATAGAGAATTGTTTAGGGATGATATTCGCTCGCGAGGAACGAAGCTCAATGCTGCCGAGCGCGAGAGTCTGCTAAGGCCATATCTGCCAGATCCGTCTGACCTTCCACGCAGGCCACTTCAGCGGCGCAAGAAGGTTCCTCG
aver     ATGGTTTCGCAACGAGAT---------AGAGAATTGAATATCACGGCTTCCTCAGGGGTCTCTGGCATTATGCTGGTGCTCAGATGAGGTTTGGC
anid     ATGGTCTCACAGCGTGACAGAGAGTTGGCTGTTGAATACCAGGGCTATCTCAGGGGTTTGTGGCATTACGCTGGGGCCCAGATGCGATTTGGC
azon     ATGGCCCTAGCACGTGATAGAGAATTACTGAGGGACACTATTCGCACCCAAGGGACCGCACTTACTGCTGCCGATCGCGAAAATATCCTGAAGCCATATCTGCCGGATCCATCAGAACTTGCACGTCGGCCACTACAGCGACAGAAGAAAGC
awen     ATGGTATCACAACGGGATAGAGTGGTGTGTCTGCC------------------------------------------------CTCTACAGGTCA------AAACAGTGCGAAATA---------AA
acar     ATGGCCCT
akaw     ---------ATGGTCTCAC---------AGCGTGACAGAGAGT---------TGGCTGTTGAATACCAGGGCTATCTCAGGGGTTTGTGGCATTACGC

I wrote a bash script for this
Code:
#!/bin/bash
j=1
for ((i=0;i<=4000;i++));
do
echo "$j"

sed -e s/'aver_[^ ]*'/aver/g clus_grp_seq"$j"_g.phy | sed -e s/'anid_[^ ]*'/anid/g | sed -e s/'anig_[^ ]*'/anig/g | sed -e s/'acar_[^ ]*'/acar/g | sed -e s/'azon_[^ ]*'/azon/g | sed -e s/'awen_[^ ]*'/awen/g | sed -e s/'akaw_[^ ]*'/akaw/g -> clus_grp_seq"$j"_g.phy
wait
let j++
done

but the parser is making several files completely blank. In the folder some files like clus_grp_seq2000_g.phy does not exists, in such case blank file like clus_grp_seq2000_g.phy is OK. But in cases even the file exists in the folder like clus_grp_seq10_g.phy as shown above the parser is making blank files.
Please let me know the problem or suggest an alternative solution.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

renaming multiple files while replacing string

hi, i've found a few examples of scripts to do this but for some reason can't get them to work properly. basically i have some dirs with a few hundred files mixed in with a bunch of other files that were made with a typo in part of them. long-file-names-tyo-example.ext want to be able... (2 Replies)
Discussion started by: kevin9
2 Replies

2. Shell Programming and Scripting

Replacing string in multiple files

Hi, I need to replace the string 'abcd' with 'xyz' in a file sample.xml This sample.xml is also present in the subdirectories of the current directory. Eg, If I am in /user/home/ the sample.xml if present in /user/home/ /user/home/folder1/ /user/home/folder2/... (3 Replies)
Discussion started by: arulanandsp
3 Replies

3. Shell Programming and Scripting

Replacing text from multiple files at multiple location

Hi, I have many files scattered in all different folders. I want to replace the text within all the files using a single command ( awk, sed...) Is it possible? example find all the files in which there is text "memory" and replace it with "branded_memories". the files can be at the... (2 Replies)
Discussion started by: rudoraj
2 Replies

4. UNIX for Dummies Questions & Answers

best method of replacing multiple strings in multiple files - sed or awk? most simple preferred :)

Hi guys, say I have a few files in a directory (58 text files or somthing) each one contains mulitple strings that I wish to replace with other strings so in these 58 files I'm looking for say the following strings: JAM (replace with BUTTER) BREAD (replace with CRACKER) SCOOP (replace... (19 Replies)
Discussion started by: rich@ardz
19 Replies

5. Shell Programming and Scripting

Counting occurrences of all words in multiple files

Hey Unix gurus, I would like to count the number occurrences of all the words (regardless of case) across multiple files, preferably outputting them in descending order of occurrence. This is well beyond my paltry shell scripting ability. Researching, I can find many scripts/commands that... (4 Replies)
Discussion started by: twjolson
4 Replies

6. Shell Programming and Scripting

How to count the number of occurrence of words from multiple files?

File 1 aaa bbb ccc File 2 aaa xxx zzz bbb File 3 aaa bbb xxx Output: (4 Replies)
Discussion started by: Misa-Misa
4 Replies

7. Shell Programming and Scripting

USING sed to remove multiple strings/words from a line

Hi I use sed comnand to remove occurance of one workd from a line. However I need to removed occurance of dufferent words in ne line. Original-1 Hi this is the END of my begining Comand sed s/"END"/"start"/g Output-1 Hi this is the start of my beginig But I have more... (9 Replies)
Discussion started by: mnassiri
9 Replies

8. Shell Programming and Scripting

Replacing old TNS entries with New one in multiple files

I have requirement to replace old TNS entries with New one in multiple files. one file may contain more then one occurrence of tns. Example: Below is the one of occurrence in a current file(s). i am interested to replace only red part. <connection-pool name="Google_APP_CP"... (4 Replies)
Discussion started by: KDDubai333
4 Replies

9. Shell Programming and Scripting

Replacing matched patterns in multiple files with awk

Hello all, I have since given up trying to figure this out and used sed instead, but I am trying to understand awk and was wondering how someone might do this in awk. I am trying to match on the first field of a specific file with the first field on multiple files, and append the second field... (2 Replies)
Discussion started by: karlmalowned
2 Replies

10. UNIX for Beginners Questions & Answers

Issue with search and replacing multiple items in multiple files

Im having an issue when trying to replace the first column with a new set of values in multiple files. The results from the following code only replaces the files with the last set of values in val.txt. I want to replace all the files with all the values. for date in {1..31} do for val in... (1 Reply)
Discussion started by: ncwxpanther
1 Replies
regex(1F)                                                          FMLI Commands                                                         regex(1F)

NAME
regex - match patterns against a string SYNOPSIS
regex [-e] [ -v "string"] [ pattern template] ... pattern [template] DESCRIPTION
The regex command takes a string from the standard input, and a list of pattern / template pairs, and runs regex() to compare the string against each pattern until there is a match. When a match occurs, regex writes the corresponding template to the standard output and returns TRUE. The last (or only) pattern does not need a template. If that is the pattern that matches the string, the function simply returns TRUE. If no match is found, regex returns FALSE. The argument pattern is a regular expression of the form described in regex(). In most cases, pattern should be enclosed in single quotes to turn off special meanings of characters. Note that only the final pattern in the list may lack a template. The argument template may contain the strings $m0 through $m9, which will be expanded to the part of pattern enclosed in ( ... )$0 through ( ... )$9 constructs (see examples below). Note that if you use this feature, you must be sure to enclose template in single quotes so that FMLI does not expand $m0 through $m9 at parse time. This feature gives regex much of the power of cut(1), paste(1), and grep(1), and some of the capabilities of sed(1). If there is no template, the default is $m0$m1$m2$m3$m4$m5$m6$m7$m8$m9. OPTIONS
The following options are supported: -e Evaluates the corresponding template and writes the result to the standard output. -v "string" Uses string instead of the standard input to match against patterns. EXAMPLES
Example 1: Cutting letters out of a string To cut the 4th through 8th letters out of a string (this example will output strin and return TRUE): `regex -v "my string is nice" '^.{3}(.{5})$0' '$m0'` Example 2: Validating input in a form In a form, to validate input to field 5 as an integer: valid=`regex -v "$F5" '^[0-9]+$'` Example 3: Translating an environment variable in a form In a form, to translate an environment variable which contains one of the numbers 1, 2, 3, 4, 5 to the letters a, b, c, d, e: value=`regex -v "$VAR1" 1 a 2 b 3 c 4 d 5 e '.*' 'Error'` Note the use of the pattern '.*' to mean "anything else". Example 4: Using backquoted expressions In the example below, all three lines constitute a single backquoted expression. This expression, by itself, could be put in a menu defini- tion file. Since backquoted expressions are expanded as they are parsed, and output from a backquoted expression (the cat command, in this example) becomes part of the definition file being parsed, this expression would read /etc/passwd and make a dynamic menu of all the login ids on the system. `cat /etc/passwd | regex '^([^:]*)$0.*$' ' name=$m0 action=`message "$m0 is a user"`'` DIAGNOSTICS
If none of the patterns match, regex returns FALSE, otherwise TRUE. NOTES
Patterns and templates must often be enclosed in single quotes to turn off the special meanings of characters. Especially if you use the $m0 through $m9 variables in the template, since FMLI will expand the variables (usually to "") before regex even sees them. Single characters in character classes (inside []) must be listed before character ranges, otherwise they will not be recognized. For exam- ple, [a-zA-Z_/] will not find underscores (_) or slashes (/), but [_/a-zA-Z] will. The regular expressions accepted by regcmp differ slightly from other utilities (that is, sed, grep, awk, ed, and so forth). regex with the -e option forces subsequent commands to be ignored. In other words, if a backquoted statement appears as follows: `regex -e ...; command1; command2` command1 and command2 would never be executed. However, dividing the expression into two: `regex -e ...``command1; command2` would yield the desired result. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWcsu | +-----------------------------+-----------------------------+ SEE ALSO
awk(1), cut(1), grep(1), paste(1), sed(1), regcmp(3C), attributes(5) SunOS 5.10 12 Jul 1999 regex(1F)
All times are GMT -4. The time now is 02:48 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy