Multiple regex in sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multiple regex in sed
# 1  
Old 09-08-2018
Multiple regex in sed

I am using the following sed script to remove new lines (\r\n and \n), except from lines starting with >:

Code:
sed -i ':a /^>/!N;s/\r\n\([^>]\)/\1/;s/\n\([^>]\)/\1/;ta'

Is there a way to include both \r\n and \n in one regex to avoid the second substitute script (s/\n\([^>]\)/\1/)?
# 2  
Old 09-08-2018
Give it a try as \r?\n

Last edited by Aia; 09-09-2018 at 03:55 AM.. Reason: grammar correction
# 3  
Old 09-08-2018
This did not work:

Code:
sed ':a /^>/!N;s/\r\n?\n\([^>]\)/\1/;ta'

Or this:

Code:
sed ':a /^>/!N;s/\r?\n\([^>]\)/\1/;ta'

# 4  
Old 09-09-2018
Try:

GNU sed:
Code:
sed ':a /^>/!N;s/\r\?\n\([^>]\)/\1/;ta' file

Code:
sed ':a /^>/!N;s/\r\{0,1\}\n\([^>]\)/\1/;ta' file

Code:
sed -r ':a 1!N;s/\r?\n([^>])/\1/;ta' file




---
Note:
The above approaches will not work if there is a carriage return on the last line
Instead try this:
Code:
sed -r ':a 1!N;s/\r$|\r?\n([^>])/\1/g;ta' file


Last edited by Scrutinizer; 09-09-2018 at 04:26 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 09-09-2018
Thanks a TON! That worked like a charm
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Regex with sed

hi i would like to say "DATABASENAME=" to "TABLESNAME=" remove "," and press enter myconfig file thanks (1 Reply)
Discussion started by: mnnn
1 Replies

2. Shell Programming and Scripting

Grep with Regex multiple characters

Hi everyone, So I'm a bit confused about a regex pattern that should exist, but I can't really find any way to do it... Let's say I want to match any lines that have a specific string, but I don't know the order of the letters but I know the length. Let's say it's 10 characters and begins... (3 Replies)
Discussion started by: Lost in Cyberia
3 Replies

3. UNIX for Advanced & Expert Users

sed REGEX to print multiple occurrences of a pattern from a line

I have a line that I need to parse through and extract a pattern that occurs multiple times in it. Example line: getInfoCall: info received please proceed, getInfoCall: info received please proceed, getInfoCall: info received please proceed, getInfoCall: info received please proceed,... (4 Replies)
Discussion started by: Vidhyaprakash
4 Replies

4. Shell Programming and Scripting

awk with multiple regex and substring

Hi Experts, I have a file on which i want to print the line which should match following criterias. Line should not start with 0 or 9 and Line should start with 1 and ( 576th character should not be 1 or 2 or 576-580 postion should not be NIPPF or CDIPB or 576-581 postion should... (2 Replies)
Discussion started by: millan
2 Replies

5. UNIX for Advanced & Expert Users

Regex pattern for multiple digits

Hello, I need to construct a pattern to match the below string (especially the timestamp at the beginning) 20101222100436_temp.dat The below pattern works _temp.dat However I am trying find if there are any other better representations. I tried {14}, but it did not work. I am on... (5 Replies)
Discussion started by: krishmaths
5 Replies

6. Shell Programming and Scripting

Converting perl regex to sed regex

I am having trouble parsing rpm filenames in a shell script.. I found a snippet of perl code that will perform the task but I really don't have time to rewrite the entire script in perl. I cannot for the life of me convert this code into something sed-friendly: if ($rpm =~ /(*)-(*)-(*)\.(.*)/)... (1 Reply)
Discussion started by: suntzu
1 Replies

7. Shell Programming and Scripting

multiple regex finding in files

Hello folks, I have a text file aa.txt that contains below text (\')|(\-\-) ((\%3D)|(=)) 20%0d% i want to search each line pattern in /opt/1.log and /opt/2.log. Can some one suggest (1 Reply)
Discussion started by: learnbash
1 Replies

8. Shell Programming and Scripting

sed - using regex and | need help

From my understanding when using regex1|regex2 the matching process tries each alternative in turn, from left to right, and the first one that succeeds is used. When im trying to extract the name from those examples: A) name.can.be.different.20.03.2009.boom B)... (2 Replies)
Discussion started by: TehOne
2 Replies

9. Shell Programming and Scripting

find -regex: matching multiple extensions

I want to use find to locate files with two different extensions, and run a grep on the results. The closest I have gotten is incredibly slow and ugly: for i in `ls -laR|egrep -e '(.js|.css)'`; do find . -name $i -print|xargs grep -H searchBg; done; This method makes my eyes bleed. Help! ;) ... (2 Replies)
Discussion started by: r0sc0
2 Replies

10. UNIX for Dummies Questions & Answers

sed regex

I would like to do this: replace the word "prod" with the word "special" but it may occur through the file naturally without a command, I only want it to happen when it has a specific command in front of it. The command will always look like this <IMG,###,###,##,>prod/directory/IMG/file ... (4 Replies)
Discussion started by: Shakey21
4 Replies
Login or Register to Ask a Question