using awk removing newline and specific position


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting using awk removing newline and specific position
# 1  
Old 03-18-2010
using awk removing newline and specific position

Hello Friends,
Input File looks as follows:
Code:
>FASTA Header1
line1
line2
line3
linen
>FASTA Header2
Line1
Line2
linen
>FASTA Header3
and so on
.......

Output:
Want something as:
Code:
>FASTA Header1 
line1line2line3linen
>FASTA Header2 
line1line2line3
>FASTA Header3
and so no.......

My one line:
Code:
awk '{if($0~/^>/){print;next}else{printf("%s",$0)}}' TEXT File

This does:
Code:
>FASTA Header1 
line1line2line3linen>FASTA Header2 line1line2line3

Which I do not want.
So how can I add a "\n" before ">" and after "my lines"

how can read the next line and check if ">" occurs if so need to add "\n" to the end of current line.

Will awk be faster or PERL is the way to go as the file is huge. Thousands of line and more.
One of the programs in perl using arrays went out of memory Smilie,
so need to read and print line by line rather than buffer first.

Thanks

Last edited by Franklin52; 03-19-2010 at 05:31 AM.. Reason: Please use code tags!
# 2  
Old 03-18-2010
Here's a Perl solution -

Code:
$ 
$ cat -n f8
     1  >FASTA Header1
     2  line1
     3  line2
     4  line3
     5  linen
     6  >FASTA Header2
     7  Line1
     8  Line2
     9  linen
    10  >FASTA Header3
    11  Line1
    12  Line2
    13  linen
$ 
$ perl -lne 'if (/^>/){print "\n",$_} else {printf} END{print}' f8

>FASTA Header1
line1line2line3linen
>FASTA Header2
Line1Line2linen
>FASTA Header3
Line1Line2linen
$

There are no arrays involved here. I tested it on a dummy file that has 250,000 "FASTA Headers" and 4 lines under each FASTA header. So that's 1,250,000 lines in all.

Code:
$ 
$ head f88
>FASTA Header1
line1
line2
line3
line4
>FASTA Header2
line1
line2
line3
line4
$ 
$ tail f88
>FASTA Header249999
line1
line2
line3
line4
>FASTA Header250000
line1
line2
line3
line4
$ 
$ wc f88
 1250000  1500000 10888895 f88
$ 
$

The Perl script, when run on this file, and redirected to another file, takes roughly 5 seconds on my system.

Code:
$ 
$ 
$ time perl -lne 'if (/^>/){print "\n",$_} else {printf} END{print}' f88 >f89

real    0m5.454s
user    0m3.456s
sys     0m0.179s
$ 
$

HTH,
tyler_durden
# 3  
Old 03-19-2010
Code:
awk '{if($0~/^>/){print"\n"$0;next}else{printf("%s",$0)}}'

# 4  
Old 03-19-2010
MySQL

See the following command.
Code:
cat Input_data | tr "\n" " " | sed 's/[>]FASTA Header[0-9]*/\n&\n/g'

# 5  
Old 03-19-2010
bash ?
Code:
while read L
do
     [ "${L:0:1}" = ">" ] && L="\n$L\n"
     echo -en "$L"
done < infile

# 6  
Old 03-19-2010
Thanks a lot guys.
I just did a slight modification to the awk code:
Code:
awk '{if (NR==1 && $0 ~/>/){print$0;next}if($0~/^>/){print"\n"$0;next}else{printf("%s",$0)}}'

As I was getting a extra newline at the first read of header. Of course I use 'tail' to get rid of it. But than again, wanted to get it right with Awk. Perl, Sed and bash codes are good too (just need to modify a bit). Thanks again for all the input and quick answers.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Linux

Removing a character at specific position in a column

Hi, I have a file like this (about 8 columns in total, this being the 2nd column) gi_49482297_ref_YP_039521.1_ gi_49482297_ref_YP_039521.1_ gi_49482315_ref_YP_039539.1_ gi_49482315_ref_YP_039539.1_I want to remove the _ at the end of the line. And at later stages I would want to replace the... (5 Replies)
Discussion started by: Syeda Sumayya
5 Replies

2. Shell Programming and Scripting

Awk command to replace specific position characters.

Hi, I have a fixed width file. The way this file works is say for example there are 30 columns in it each with different sizes say 10,5,2, etc... If data in a field is less than the field size the rest of it is loaded with spaces. I would like an awk command to that would replace I have... (8 Replies)
Discussion started by: pinnacle
8 Replies

3. Shell Programming and Scripting

Removing ^M and the newline that follows it.

Hi Gurus, Apologies as I feel like this must be answered already on here somewhere but I just can't find it. I find many people looking to remove all \n and \r (CR and LF) or one or the other but the only times I've found someone trying to remove them only when both are together they've found... (7 Replies)
Discussion started by: Leedor
7 Replies

4. Shell Programming and Scripting

Removing 0 from a specific position - if it exists

I have a file that I need to parse using a script. The dates in the file are displayed in the format: Mar 2, 2011 9:09:31 PM I have tried using the date command %e and %l but it pads an extra space for the day and hour if they are single digits. So this I used a normal date command: ... (6 Replies)
Discussion started by: crazyideas
6 Replies

5. Shell Programming and Scripting

awk removing specific line

Hi, I have file with lines key=val. For ex(conf.file):- c=3 ef=78 b=40 ca=40 I want to remove the line with c=3, when I execute the below command it's removing both lines i.e c=3 and ca=40. Could you please correct my command. /usr/xpg4/bin/awk -v key=c 'match($0,key)... (3 Replies)
Discussion started by: axes
3 Replies

6. Shell Programming and Scripting

substitute a string on a specific position for specific lines

I woud like to substitue a string on a specific position for specific lines I've got a file and I would like to change a specific string from "TOCHANGE" to "ABCABCAB" For every line (except 1,2, 3 and the last one) , I need to check between the 9th and the 16th digits. For the 3rd line, I... (7 Replies)
Discussion started by: BSF
7 Replies

7. Shell Programming and Scripting

Remove text from n position to n position sed/awk

I want to remove text from nth position to nth position couple of times in same line my line is "hello is there anyone can help me with this question" I need like this ello is there anyone can help me with question 'h' is removed and 'this' removed from the line. I want to do this... (5 Replies)
Discussion started by: elamurugu
5 Replies

8. Shell Programming and Scripting

AWK or SED to add string at specific position

Greetings. I don't have experience programing scripts. I need to insert a string in a specific position of another string on another file (last.cfg), for example: File last.cfg before using script: login_interval=1800 lcs.machinename=client04 File last.cfg after using script:... (4 Replies)
Discussion started by: vanesuke
4 Replies

9. Shell Programming and Scripting

Using sed to replace specific character and specific position

I am trying to use sed to replace specific characters at a specific position in the file with a different value... can this be done? Example: File: A0199999123 A0199999124 A0199999125 Need to replace 99999 in positions 3-7 with 88888. Any help is appreciated. (5 Replies)
Discussion started by: programmer22
5 Replies

10. Shell Programming and Scripting

sed removing carriage return and newline

Hi, I'm not very familiar with unix shell. I want to replace the combination of two carriage returns and one newline with one carriage return and one newline. I think the best way to do this is to use sed. I tried something like this: sed -e "s#\#\#g" file.txt but it doesn't work. Thanx... (2 Replies)
Discussion started by: mored
2 Replies
Login or Register to Ask a Question