Adding lines to files based on their names


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding lines to files based on their names
# 8  
Old 06-25-2010
So long as the reference ids (1a, 1b, 6) are valid sh varname sequences (alphanumeric and underscore), and the replacement text does not contain a "." on a line by itself (this tells ed to exit insert mode):
Code:
#!/bin/sh

_r1a='>Reference1a
AGCATGACGATCAGTGTGCGGGTGA'

_r1b='>Reference1b
AGCATGTGTGCGGGTGA'

_r6='>Reference6
AGCATGTGTGCGGGTGATTTTTT'

for f in *.fas; do
    printf '1i\n%s\n.\nw\nq\n' "$(eval echo \"\$_r"${f%%-*}"\")" | ed -s "$f"
done

# 9  
Old 06-25-2010
Code is not working

alister,

I have tried your code
Code:
#!/bin/sh

_r1a='>Reference1a
AGCATGACGATCAGTGTGCGGGTGA'

_r1b='>Reference1b
AGCATGTGTGCGGGTGA'

_r6='>Reference6
AGCATGTGTGCGGGTGATTTTTT'

for f in *.fas; do
    printf '1i\n%s\n.\nw\nq\n' "$(eval echo \"\$_r"${f%%-*}"\")" | ed -s "$f"
done

And this is what I am getting
Quote:
/bin/sh^M: bad interpreter: No such file or directory
I am missing something?
# 10  
Old 06-25-2010
Did you create that script under Windows? If that is the case, first run:
Code:
dos2unix script.sh

Then rerun your script.
# 11  
Old 06-25-2010
Linux Red Hat

No, I used gedit. Is it working in your end?
# 12  
Old 06-25-2010
Quote:
Originally Posted by Xterra
No, I used gedit. Is it working in your end?
I didn't try it on my test machine, but that error you are seeing means that illegal control character is appended to the end of the first line. Did you try using "dos2unix" on that file? As for the gedit.. Maybe you have character encoding misconfigured there? If it is set to something like DOS, then editor will insert those "^M" characters at the end of every line. Did you use gedit to write scripts earlier without problems?
# 13  
Old 06-25-2010
dos2unix

I did use it and I this is what I am getting
Quote:
?
?
?
# 14  
Old 06-25-2010
I would suggest to use some other editor to write your script.. You can use
Code:
nano

it is very easy to use, with all CTRL+ commands printed on the bottom. I'm sure that if you use it to create that script, it will run.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Concatenate files based on names

Dear all, I have a list of files and I woulk like to concatenate some of them based on their name. Basically my files are names like that: file1_abcd_other_useless_letters_1_C1.txt file1_abcd_other_useless_letters_1_C2.txt file1_xywz_other_useless_letters_1_C1.txt... (4 Replies)
Discussion started by: giuliangiuseppe
4 Replies

2. Shell Programming and Scripting

Two files, remove lines from second based on lines in first

I have two files, a keepout.txt and a database.csv. They're unsorted, but could be sorted. keepout: user1 buser3 anuser19 notheruser27 database: user1,2343,"information about",field,blah,34 user2,4231,"mo info",etc,stuff,43 notheruser27,4344,"hiya",thing,more thing,423... (4 Replies)
Discussion started by: esoffron
4 Replies

3. Shell Programming and Scripting

Join lines from two files based on match

I have two files. File1 >gi|11320906|gb|AF197889.1|_Buchnera_aphidicola ATGAAATTTAAGATAAAAAATAGTATTTT >gi|11320898|gb|AF197885.1|_Buchnera_aphidicola ATGAAATTTAATATAAACAATAAAA >gi|11320894|gb|AF197883.1|_Buchnera_aphidicola ATGAAATTTAATATAAACAATAAAATTTTT File2 AF197885 Uroleucon aeneum... (2 Replies)
Discussion started by: pathunkathunk
2 Replies

4. Shell Programming and Scripting

Deleting lines based on a condition for a group of files

hi i have a set of similar files. i want to delete lines until certain pattern appears in those files. for a single file the following command can be used but i want to do it for all the files at a time since the number is in thousands. awk '/PATTERN/{i++}i' file (6 Replies)
Discussion started by: anurupa777
6 Replies

5. Shell Programming and Scripting

Generate files from one file based on lines

Hi Friends, I have a file1 file1.txt 1ABC 13478 aqjerh 473 343 2hej 478 5775 24578 23892 3fhd fg 847 brjkb f99345 487 4eh ehjk 84 47589 8947 234 5784 487 738 52895 8975 6 57489 eghe9 4575 859479 7fnbd 4y5 4iuy 458 h irh 8fjdg 74 7845 8475 5789 94yr 48yr 4hr erhj reh... (3 Replies)
Discussion started by: i150371485
3 Replies

6. Shell Programming and Scripting

combine lines from two files based on an if statement

I'm rather new to programming, and am attempting to combine lines from 2 files in a way that is way beyond my expertise - any help would be appreciated! I need to take a file (file1) and add columns to it from another file (file2). However, a line from file2 should only be added to a given line... (3 Replies)
Discussion started by: Cheri
3 Replies

7. Shell Programming and Scripting

Adding lines to files based on file extension

I have posted this before but did not get many replies, so here it goes again. I have several files name like this If the file extension is 1a, I woould like to add at the beggining of the file the following sequence If the file extension is 1b, thn the entry that should be added is the next... (2 Replies)
Discussion started by: Xterra
2 Replies

8. UNIX for Dummies Questions & Answers

sort lines in different files based on the starting letter

Hi ,, i have the below file... D 2342135 B 214236 C argjlksd V lskjrghaklsr C slkrgj B sdg4tsd E aslkgjlkasg i want to sort the lines into different files based on the starting letter of the line. so that i have different files for lines starting with a letter. thanks (1 Reply)
Discussion started by: jathin12
1 Replies

9. Shell Programming and Scripting

how to combine 2 lines in same files based on any text

hi, I want to combine two lines in same file. If the line ends with '&' it should belongs to previous line only Here i am writing example. Ex1: line 1 : return abcdefgh& line 2 : ijklmnopqr& line 3 : stuvw& line 4 : xyz output should be line 1: return abcdefghijklmnopqrstuvwxyz ... (11 Replies)
Discussion started by: spc432
11 Replies

10. Shell Programming and Scripting

Adding 3 Lines to Multiple c and h files with perl

Hello, i need some help with a perl script. i need to add the lines: #ifdef LOGALLOC #include "logalloc.h" #endif // LOGALLOC To all c and h files in a project with subdirectories. Logalloc is a tool to log all *alloc and free's in a text file, it defines the *alloc funtions new.... (2 Replies)
Discussion started by: Lazzar
2 Replies
Login or Register to Ask a Question