How i input a pattern into a file where it does not exist


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How i input a pattern into a file where it does not exist
# 1  
Old 05-26-2010
Power How i input a pattern into a file where it does not exist

I have a strange problem and i am wondering the best way to tackel it.

i have hundres of files. in each of these files i would like to see a representative of each of my animals at the start of each line, in this case
mouse, cat, dog, pig, rabbit, in this exact order.

However not all of my files contain representatives of each animal.

If i have a file that contains

>dog;ATGCATGCATTTA
>rabbit;CTANNNAAAAAAA

I would like to be able to substitute the other animals except place them in my order of preference, so i would like my end file to look like this

>mouse;-----
>cat;-----
>dog;ATGCATGCATTTA
>pig;-----
>rabbit;CTANNNAAAAAAA

this is a short snipit of who i was thinking of tackling the problem like this

Code:
if grep '>mouse' FILEIN
   then 
       print
   else
        sed '1i\
>mouse;-----' FILEIN > FILEOUT_1
fi
if grep '>cat' FILEOUT_1
   then 
       print
   else
       sed '/>mouse/ a\
>cat;----' FILEOUT_1 > FILEOUT2
fi

Now as you can see that is an absolutely horrible piece of code, which does everything but actually do what i would like it too.

If any one can lend advice here it would be hugely appreciated.
# 2  
Old 05-26-2010
You were on the right way!
Does your sed support the -i option (the so-called in-place editing)?

Code:
$ cat testfile
>dog;ATGCATGCATTTA
>rabbit;CTANNNAAAAAAA
$ 
$ ./checktest.sh
>dog;ATGCATGCATTTA
>rabbit;CTANNNAAAAAAA
$ 
$ cat testfile
>mouse;-----
>cat;-----
>dog;ATGCATGCATTTA
>pig;-----
>rabbit;CTANNNAAAAAAA
$

checktest.sh
Code:
#!/bin/sh

if ! grep '>mouse' testfile
   then 
        sed -ie '1i\
>mouse;-----\
' testfile
fi

if ! grep '>cat' testfile
   then
        sed -ie '2i\
>cat;-----\
' testfile
fi

if ! grep '>dog' testfile
   then
        sed -ie '3i\
>dog;-----\
' testfile
fi

if ! grep '>pig' testfile
   then
        sed -ie '4i\
>pig;-----\
' testfile
fi

if ! grep '>rabbit' testfile
   then
        sed -ie '5i\
>rabbit;-----\
' testfile
fi

Hope this is what you wanted to achieve...
# 3  
Old 05-27-2010
Perfect

Thanks so much, that worked like a dream!Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copy data to new file based on input pattern

Hi All, I want to create a new file based on certain conditions and copy only those conditioned data to new file. Input Data is as it looks below. ORDER|Header|Add|32|32|1616 ORDER|Details1......... ORDER|Details2......... ORDER|Details3......... ORDER|Details4............ (10 Replies)
Discussion started by: grvk101
10 Replies

2. UNIX for Beginners Questions & Answers

Reducing input file size after pattern search

I have a very large file with millions of entries identified by @M. I am using the following script to "extract" entries based on specific strings/patterns: #!/bin/bash if ] then file=$1 else echo "Input_file passed as an argument $1 is NOT found." exit; fi MID=(NULL "string-1"... (10 Replies)
Discussion started by: Xterra
10 Replies

3. Shell Programming and Scripting

Search pattern in a file taking input from another file

Hi, Below is my requirement File1: svasjsdhvassdvasdhhgvasddhvasdhasdjhvasdjsahvasdjvdasjdvvsadjhv vdjvsdjasvdasdjbasdjbasdjhasbdasjhdbjheasbdasjdsajhbjasbjasbhddjb svfsdhgvfdshgvfsdhfvsdadhfvsajhvasjdhvsajhdvsadjvhasjhdvjhsadjahs File2: sdh hgv I need a command such that... (8 Replies)
Discussion started by: imrandec85
8 Replies

4. Shell Programming and Scripting

Removing multiple lines from input file, if multiple lines match a pattern.

GM, I have an issue at work, which requires a simple solution. But, after multiple attempts, I have not been able to hit on the code needed. I am assuming that sed, awk or even perl could do what I need. I have an application that adds extra blank page feeds, for multiple reports, when... (7 Replies)
Discussion started by: jxfish2
7 Replies

5. Shell Programming and Scripting

Print all lines between two keyword if a specific pattern exist

I have input file as below I need to check for a pattern and if it is there in file then I need to print all the lines below BEGIN and END keyword. Could you please help me how to get this in AIX using sed or awk. Input file: ABC ******** BEGIN ***** My name is Amit. I am learning unix.... (8 Replies)
Discussion started by: Amit Joshi
8 Replies

6. Programming

take input from a variable as pattern to awk

Hi everyone, Can anyone tell me how to take contents of a variable as a pattern for awk command. Am doing as below, but doesnt get any output: $c = "Tue Dec"; $log = ` awk '/ \$c /' in.txt`; print $log; (7 Replies)
Discussion started by: anandrec
7 Replies

7. Shell Programming and Scripting

Split a file into multiple files based on the input pattern

I have a file with lines something like. ...... 123_start ...... ....... 123_end .... ..... 456_start ...... ..... 456_end .... ..... 789_start .... .... 789_end (6 Replies)
Discussion started by: abinash
6 Replies

8. Shell Programming and Scripting

Need script to take input from file, match on it in file 2 and input data

All, I am trying to figure out a script to run in windows that will allow me to match on First column in file1 to 8th Column in File2 then Insert file1 column2 to file2 column4 then create a new file. File1: 12345 Sam 12346 Bob 12347 Bill File2:... (1 Reply)
Discussion started by: darkoth
1 Replies

9. Shell Programming and Scripting

extract/select pattern from input

Hey, examples of the input (text line): /bla/blMOasdn234.adanif24/blabla.rar /bla/blMOasdn234.adanif24/blabla23124.bin /bla/bla/bla/bla/bla/bla.bin and what I need to do is extract/select only the dir path so the output would be: /bla/blMOasdn234.adanif24/ /bla/blMOasdn234.adanif24/... (4 Replies)
Discussion started by: TehOne
4 Replies

10. Shell Programming and Scripting

Find script with input pattern file

Howdy: I have a file with 140+ file name patterns. Each prefix can have dozens of files with different extension names. e.g. 1-S51 1113-G6V 1117-G6V 1119-G6V 1127-G6V 12XW-AF5W 14-UA8N I need to search in 12 directories, (/data/lgc1/basin_mas to /data/lgc12/basin_mas) for all the... (8 Replies)
Discussion started by: iguanathompson
8 Replies
Login or Register to Ask a Question