How to add newline before and after a special character?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to add newline before and after a special character?
# 1  
Old 12-07-2012
How to add newline before and after a special character?

So I have a file that contains

Code:
>NM_#########AUGCAUCGUAGCUAGUCGAUACUGGACUG>NM_########AUGAGUAUGUAUGAUGUAUGUAUGA

where # is any digit 0-9 (the text is many repetitions of the pattern above, not just that, but all in one line), and I want it to show

Code:
>NM_#########
AUGUAGUGCUAGCUGAUCGAUGCUAGUCGUAGC
>NM_########
AGUGAGUCGUCGUGACUGACUGUGGCAUCGUA

Basically I need to add a new line before every > and between a number and a letter.

OR

If it's easier, I have something like this

Code:
>NM_#########
AUGCUGAC
GACGUAGC
ACGUGUAG
>NM_########
AGUGCUGA
ACGUAGCU
ACGUGCUA

and I want to condense all the letter only lines to one line, like the output shown above.

If someone could help me how to do this with a simple command, it would really help.

Thank you!
# 2  
Old 12-07-2012
It would be better if you posted your sample input without masking digits with "#"'s... Anyway try this:
Code:
perl -pe 's/(\d)(\D)/$1\n$2/g;s/(\w)>/$1\n>/g' file

# 3  
Old 12-11-2012
continuous the topic, add one more sed command to reformat the output.

Code:
perl -pe 's/(\d)(\D)/$1\n$2/g;s/(\w)>/$1\n>/g' infile |sed '/^[!A-Z]/ s/.\{8\}/&\n/g'


>NM_123455673
AUGCAUCG
UAGCUAGU
CGAUACUG
GACUG
>NM_12345634
AUGAGUAU
GUAUGAUG
UAUGUAUG
A

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove newline character if it is the only character in the entire file.?

I have a file which comes every day and the file data look's as below. Vi abc.txt a|b|c|d\n a|g|h|j\n Some times we receive the file with only a new line character in the file like vi abc.txt \n (8 Replies)
Discussion started by: rak Kundra
8 Replies

2. Shell Programming and Scripting

sed command to add special character (')

I have a file (input) which contains are below. Member Analytics Engine Enterprise Manager Dev Tutorial I want to change contains as below by using sed command 'Member Analytics Engine'; 'Enterprise Manager'; 'Dev Tutorial'; First, I tried to add (') on every first line by using sed... (8 Replies)
Discussion started by: anshu ranjan
8 Replies

3. Shell Programming and Scripting

sed add special character

Hi all I got test.test.test and need test.test\.test * I need the backslash before the last dot in the line I tried echo test.test.test | sed 's/\./\\./g' but it gives me test\.test\.test Thanks (7 Replies)
Discussion started by: stinkefisch
7 Replies

4. Shell Programming and Scripting

How to add newline character at end of file?

Hi All, I have following piece of code in UNIX C Shell script and I want to add one more command which can add newline at the end of file only if there is no newline character exists. foreach file (`ls $dd_PLAYCARD_EDI_IN`) if ( -f $dd_PLAYCARD_EDI_IN/${file} ) then cat -n... (4 Replies)
Discussion started by: jnrohit2k
4 Replies

5. Shell Programming and Scripting

Remove last newline character..

Hi all.. I have a text file which looks like below: abcd efgh ijkl (blank space) I need to remove only the last (blank space) from the file. When I try wc -l the file name,the number of lines coming is 3 only, however blank space is there in the file. I have tried options like... (14 Replies)
Discussion started by: Sathya83aa
14 Replies

6. Shell Programming and Scripting

cutting long text by special char around 100 byte and newline

Regard, How can i cut the text by special char(|) around 100 byte and write the other of the text at newline using Perl. ... (3 Replies)
Discussion started by: Shawn, Lee
3 Replies

7. UNIX for Dummies Questions & Answers

newline character in a variable

variable="unix\nlinux" echo $variable expected output: unix linux :wall: can i do that ?? thanks in advance!! (3 Replies)
Discussion started by: sathish92
3 Replies

8. Shell Programming and Scripting

Why SED can't see the last newline character?

Removed. My question does not make sense. and SED does see the last newline character. But I still have a question: How to remove the last newline character(the newline character at the end of last line) using SED? ---------- Post updated 05-01-11 at 10:51 AM ---------- Previous update was... (7 Replies)
Discussion started by: kevintse
7 Replies

9. UNIX for Dummies Questions & Answers

echo without newline character

hi, I have a for loop where in I write some file name to another file. I want to write all the filenames to another without any newlines. how can i avoid getting new lines with echo? Thanks, Srilaxmi (2 Replies)
Discussion started by: srilaxmi
2 Replies

10. UNIX for Dummies Questions & Answers

newline character

hi, I want to print the below lines "Message from bac logistics The Confirmation File has not been received." When i give like this in the code "Message from bac logistics\n The Confirmation File has not been received." It is giving only Message from bac logistics\n The... (9 Replies)
Discussion started by: trichyselva
9 Replies
Login or Register to Ask a Question