IPB

Bienvenue invité ( Connexion | Inscription )

 
Reply to this topicStart new topic
> Sed et suppression retour chariot [Résolu], Comment supprimer \n avec sed
Options
malou50
posté 30 Jun 2005, 08:54
Message #1


Adepte de Macbidouille
*

Groupe : Membres
Messages : 42
Inscrit : 17 Dec 2003
Membre no 12 577



Bonjour
J'essaye de supprimer le \n , retour chariot, en fin de certaine ligne avec sed mais je n'y
arrive pas.
je l'utilise comme suivant :
CODE
sed "s/\n//g" test1.txt > test2.txt


Le problème est qu'il ne me remplace pas les \n par "rien" ni même autre chose, j'ai essayé.
Auriez vous une idée.
Si je ne suis pas assez claire n'hesitez pas à me le dire.
merci

Ce message a été modifié par malou50 - 30 Jun 2005, 08:54.
Go to the top of the page
 
+Quote Post
_Olivier_
posté 30 Jun 2005, 09:15
Message #2


- Macbidouilleur d'Or !
*****

Groupe : Membres
Messages : 2 125
Inscrit : 23 Aug 2002
Lieu : Grandmetz (Belgique, Leuze)
Membre no 3 222



Trouvé dans la FAQ sed

CODE

5.10. Why can't I match or delete a newline using the \n escape               |
     sequence? Why can't I match 2 or more lines using \n?                   |

  The \n will never match the newline at the end-of-line because the         |
  newline is always stripped off before the line is placed into the          |
  pattern space. To get 2 or more lines into the pattern space, use          |
  the 'N' command or something similar (such as 'H;...;g;').                 |

  Sed works like this: sed reads one line at a time, chops off the           |
  terminating newline, puts what is left into the pattern space where        |
  the sed script can address or change it, and when the pattern space        |
  is printed, appends a newline to stdout (or to a file). If the             |
  pattern space is entirely or partially deleted with 'd' or 'D', the        |
  newline is *not* added in such cases. Thus, scripts like                   |

     sed 's/\n//' file       # to delete newlines from each line             |
     sed 's/\n/foo\n/' file  # to add a word to the end of each line         |

  will NEVER work, because the trailing newline is removed _before_          |
  the line is put into the pattern space. To perform the above tasks,        |
  use one of these scripts instead:                                          |

     tr -d '\n' < file              # use tr to delete newlines              |
     sed ':a;N;$!ba;s/\n//g' file   # GNU sed to delete newlines             |
     sed 's/$/ foo/' file           # add "foo" to end of each line          |

  Since versions of sed other than GNU sed have limits to the size of        |
  the pattern buffer, the Unix 'tr' utility is to be preferred here.         |
  If the last line of the file contains a newline, GNU sed will add          |
  that newline to the output but delete all others, whereas tr will          |
  delete all newlines.                                                       |
 
  To match a block of two or more lines, there are 3 basic choices:          |
  (1) use the 'N' command to add the Next line to the pattern space;         |
  (2) use the 'H' command at least twice to append the current line          |
  to the Hold space, and then retrieve the lines from the hold space         |
  with x, g, or G; or (3) use address ranges (see section 3.3, above)        |
  to match lines between two specified addresses.                            |
 
  Choices (1) and (2) will put an \n into the pattern space, where it        |
  can be addressed as desired ('s/ABC\nXYZ/alphabet/g'). One example         |
  of using 'N' to delete a block of lines appears in section 4.13            |
  ("How do I delete a block of _specific_ consecutive lines?"). This         |
  example can be modified by changing the delete command to something        |
  else, like 'p' (print), 'i' (insert), 'c' (change), 'a' (append),          |
  or 's' (substitute).                                                       |

  Choice (3) will not put an \n into the pattern space, but it _does_        |
  match a block of consecutive lines, so it may be that you don't            |
  even need the \n to find what you're looking for. Since GNU sed            |
  version 3.02.80 now supports this syntax:                                  |

     sed '/start/,+4d'  # to delete "start" plus the next 4 lines,           |

  in addition to the traditional '/from here/,/to there/{...}' range         |
  addresses, it may be possible to avoid the use of \n entirely.             |



Donc, un tr -d '\n' < file > file_sans_CR fonctionne...

C'était une question intéressante !


--------------------
* Rejoignez-nous sur le chan IRC de MacBidouille !
Go to the top of the page
 
+Quote Post
malou50
posté 30 Jun 2005, 09:29
Message #3


Adepte de Macbidouille
*

Groupe : Membres
Messages : 42
Inscrit : 17 Dec 2003
Membre no 12 577



Merci beaucoup pour la réponse, ça marche

Ce message a été modifié par malou50 - 30 Jun 2005, 09:33.
Go to the top of the page
 
+Quote Post
_Olivier_
posté 30 Jun 2005, 09:35
Message #4


- Macbidouilleur d'Or !
*****

Groupe : Membres
Messages : 2 125
Inscrit : 23 Aug 2002
Lieu : Grandmetz (Belgique, Leuze)
Membre no 3 222



Ah diable... sous Mac OS X, la commande sed ':a;N;$!ba;s/\n//g' file ne fonctionne pas.


--------------------
* Rejoignez-nous sur le chan IRC de MacBidouille !
Go to the top of the page
 
+Quote Post
malou50
posté 30 Jun 2005, 09:48
Message #5


Adepte de Macbidouille
*

Groupe : Membres
Messages : 42
Inscrit : 17 Dec 2003
Membre no 12 577



enlève les '
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic
1 utilisateur(s) sur ce sujet (1 invité(s) et 0 utilisateur(s) anonyme(s))
0 membre(s) :

 



Nous sommes le : 28th April 2024 - 00:45