LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   sed mathing at the end of line by the beging of the next (https://www.linuxquestions.org/questions/programming-9/sed-mathing-at-the-end-of-line-by-the-beging-of-the-next-789919/)

sqn 02-18-2010 06:37 AM

sed mathing at the end of line by the beging of the next
 
Hi all,

I have ran into a problem. I have to remove commas at the end of line, but only if the next line does not start with a (or many) space(s).

I have the following so far:

Code:

sed -e 's/,[ \t]*$//g' aliases
but this one removes all commas and spaces

thx a lot

druuna 02-18-2010 06:57 AM

Hi,

Is this what you are looking for:
Code:

#!/bin/bash
sed '
/,$/ {
# append the next line
    N
# look for "," followed by "<space(s)>"
    /,[ ]*/ {
#    delete ","
          s/,$//
#    print
          P
#    then delete the first line
          D
    }
}' infile

Testrun:
Code:

$ cat infile
xxxxx, yyyyy zzzzz,
xxxxx, yyyyy zzzzz,

xxxxx yyyyy zzzzz,
xxxxx yyyyy zzzzz,
 
$ ./rem.sed
xxxxx, yyyyy zzzzz,
xxxxx, yyyyy zzzzz

xxxxx yyyyy zzzzz,
xxxxx yyyyy zzzzz

Hope this helps.

sqn 02-18-2010 07:15 AM

close...
I need to remove the commas and spaces from the end of line ONLY if the next line does NOT start with a space. If a space is found on the next line leave the comma on the previsious line in place

Thx for the fast answer

druuna 02-18-2010 07:21 AM

Hi,

Overlooked the not part....

I'll give it another try but your first and last reply contradict each other.

1) remove commas at the end of line, but only if the next line does not start with a (or many) space(s).
2) remove the commas and spaces from the end of line ONLY if the next line does NOT start with a space.

Which is correct 1 or 2?

sqn 02-18-2010 07:39 AM

Quote:

Originally Posted by druuna (Post 3868045)
Hi,

Overlooked the not part....

I'll give it another try but your first and last reply contradict each other.

1) remove commas at the end of line, but only if the next line does not start with a (or many) space(s).
2) remove the commas and spaces from the end of line ONLY if the next line does NOT start with a space.

Which is correct 1 or 2?

My bad...
so i'll reformulate with an example so I'll make it easier

I have the folloing file:
Code:

cat file
asd: df, er, we er,
 qw, er, er
1234: df, er, er,
2345: 12, df, rt, yu

I want to remove the comma only at the line [1234: df, er, er,], because the next line is not starting with a space.

druuna 02-18-2010 07:46 AM

Ok,

Been playing with this while waiting for your answer.

This should work (it does take into account that there could be spaces after the ,):
Code:

#!/bin/bash
sed '
/,[ ]*$/ {
    N
    /,[ ]*\n[A-Za-z0-9]/ {
          s/,[ ]*\n/\n/
          P
          D
    }
}' infile

Testrun

Code:

$ cat infile
xxxxx yyyyy zzzzz,  extra spaces at the end
xxxxx yyyyy zzzzz, 

xxxxx, yyyyy zzzzz,  extra spaces at the end
xxxxx, yyyyy zzzzz, 
 
xxxxx yyyyy zzzzz,
xxxxx yyyyy zzzzz,
 
xxxxx yyyyy zzzzz,
xxxxx yyyyy zzzzz,
 
asd: df, er, we er,
 qw, er, er
1234: df, er, er,
2345: 12, df, rt, yu

$ ./rem.sed
xxxxx yyyyy zzzzz
xxxxx yyyyy zzzzz, 

xxxxx, yyyyy zzzzz
xxxxx, yyyyy zzzzz, 
 
xxxxx yyyyy zzzzz
xxxxx yyyyy zzzzz,
 
xxxxx yyyyy zzzzz
xxxxx yyyyy zzzzz,
 
asd: df, er, we er,
 qw, er, er
1234: df, er, er
2345: 12, df, rt, yu

Hope this is what you wanted.

sqn 02-18-2010 07:53 AM

that's it!

THX a lot... I'm strugling with this for 2 days...

thx again :)

druuna 02-18-2010 07:54 AM

You are welcome :)

jschiwal 02-18-2010 07:59 AM

Sed is a line editor. If you have a multi-line pattern, you need to use H (hold) or N (next) to build up more than one line in the pattern space or the hold buffer.

Not sure if this will work in all cases. Maybe your definition should define the patterns expected such as the pattern at the beginning of most lines. It looks like you have output that was line wrapped. Maybe undoing the line wrap is what you need.

Try this. I only tested it in your example:
Code:

sed  '/,$/{N;s/,\n /\n /};' testfile

sqn 02-18-2010 08:10 AM

the file is a mail aliases, and line wrapping for long aliases is more easy to edit and/or maintain.

This problem apeared after removing some users.


All times are GMT -5. The time now is 05:46 AM.