0

I am trying to filter lines from a text file based on patterns in proceeding lines which keeping the pattern matched line. I also need to rename the pattern matched files

for example:

>text chr1    
AAA  
BBA  
AAA  
>additional text chr2  
ABA  
AAA  
CCC  
TTT  
>some text chr3  
TTT  
CCC  
TCT  
>no good text 1 3  
AAA  
CCC  
TCT  
>unimportant text 2 3 3  
ACC  
CCC  
TCT  

the command would print the lines containing "chr" and all following lines until a line beginning with ">" does not contain "chr":

>chr1  
AAA  
BBA  
AAA  
>chr2  
ABA  
AAA  
CCC  
TTT  
>chr3   
TTT  
CCC  
TCT  
8
  • Hello and welcome to the U&L stack exchange site! Please review the Help Center to get information on how to best post to this site. To get to your question, could you please edit your post to include additional context such as what you have tried. It also is not entirely clear what the patterns are. Could you please clarify? Thank you.
    – kemotep
    Commented Nov 20, 2018 at 19:38
  • Does your data file actually contain those backslashes? Commented Nov 20, 2018 at 20:47
  • So you want to - deduplicate (chr1), and - remove integer numbers after \>?
    – RudiC
    Commented Nov 20, 2018 at 21:37
  • I've made some edits: the "\" is a mistake. @RudiC I am not sure what you mean by "deduplicate," I changed the numbers to make it more clear they're just standing in for other unimportant text that I want to get rid of.
    – znl
    Commented Nov 20, 2018 at 22:16
  • That output doesn't seem as if 'the command would print the lines containing "chr"' Where is the first "paragraph" '>other text chr1 AAA BBB CCC'?
    – RudiC
    Commented Nov 21, 2018 at 7:24

1 Answer 1

0

I did it using awk, grep and sed. Assuming there aren't trailing whitespaces in the original file.

awk 'NR>1&&/>/{print ""}{printf " %s", $0}END{print ""}' file |grep chr|sed 's/^ //' |sed 's/^.*chr/>chr/' |awk 'BEGIN{OFS="\n";}{print $1,$2,$3,$4}'

first, group blocks starting lines with '>' and preceding each field with a whitesapce

 >text chr1 AAA BBA AAA
 >additional text chr2 ABA AAA CCC TTT
 >some text chr3 TTT CCC TCT
 >no good text 1 3 AAA CCC TCT
 >unimportant text 2 3 3 ACC CCC TCT

second, filter oll tthe lines that contain the substring "chr"

 >text chr1 AAA BBA AAA
 >additional text chr2 ABA AAA CCC TTT
 >some text chr3 TTT CCC TCT

third, remove all leading whitespace

>text chr1 AAA BBA AAA
>additional text chr2 ABA AAA CCC TTT
>some text chr3 TTT CCC TCT

fourth, remove all characters between ">" and "chr"

>chr1 AAA BBA AAA
>chr2 ABA AAA CCC TTT
>chr3 TTT CCC TCT

and finally use awk to print all the lines with "\n" as output file separator

>chr1
AAA
BBA
AAA
>chr2
ABA
AAA
CCC
>chr3
TTT
CCC
TCT

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .