如何在特定字符串后插入换行符?

时间:2019-04-24 22:45:51

标签: unix awk sed terminal scripting

我有一个包含以下文本的文件,我想在S.字符后换行

我有这个

1900-01-01 00:00:00|1|S|S|S|S.1900-01-01|S.1900-01-01 00:00:00|1|S|S|S|S.1900-01-01 00:00:00|1|S|S|S|S.

我想要

1900-01-01 00:00:00|1|S|S|S|S.
1900-01-01|S.
1900-01-01 00:00:00|1|S|S|S|S.
1900-01-01 00:00:00|1|S|S|S|S.

我尝试过

$ sed 's/S./\n/g' file.csv > file2.csv

3 个答案:

答案 0 :(得分:3)

使用任何接受<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script>为“换行符”的sed:

\n

或以bash表示$ sed 's/S\./&\n/g' file 1900-01-01 00:00:00|1|S|S|S|S. 1900-01-01|S. 1900-01-01 00:00:00|1|S|S|S|S. 1900-01-01 00:00:00|1|S|S|S|S. 以获取换行符:

$'\n'

或随身携带任何sed到任何外壳中:

$ sed 's/S\./&\'$'\n''/g' file
1900-01-01 00:00:00|1|S|S|S|S.
1900-01-01|S.
1900-01-01 00:00:00|1|S|S|S|S.
1900-01-01 00:00:00|1|S|S|S|S.

请注意,由于脚本会按要求在每个 $ sed 's/S\./&\ /g' file 1900-01-01 00:00:00|1|S|S|S|S. 1900-01-01|S. 1900-01-01 00:00:00|1|S|S|S|S. 1900-01-01 00:00:00|1|S|S|S|S. 之后添加换行符,因此输出将以空行结尾。如果您实际上只想在每个S.中线之后添加一个换行符,那么它将是:

S.

答案 1 :(得分:1)

文件中的每一行都有S.组合时,可以使用非贪婪模式

grep -oP ".*?S[.]" file

当您可以有不同的行时,您需要添加替代行

grep -oP ".*?S[.]|.*" file

答案 2 :(得分:0)

如果<!DOCTYPE html> <html> <title>ARMS Hamburger Site</title> <meta charset="UTF-8"> <meta http-equiv="x-ua-compatible" content="ie=9"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css"> <body> <!-- Footer --> <footer class="w3-center w3-light-grey w3-padding-32"> <button">Try it</button> <script language="javascript"> var today = new Date(); var t0 = today.getSeconds(); var connection = new ActiveXObject("ADODB.Connection") ; var table = document.getElementById("myTable"); var connectionstring="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\\\drivepath\\blah\\filedatabase.accdb;Jet OLEDB:Engine Type=5;Persist Security Info=False;Mode=Share Exclusive;" connection.Open(connectionstring); var rs = new ActiveXObject("ADODB.Recordset"); rs.Open("SELECT * FROM table", connection); rs.MoveFirst var str=rs.GetString(,,"</td><td>","</td></tr><tr><td>"," "); rs.close; connection.close; var today2 = new Date(); var t1 = today2.getSeconds(); alert(t1-t0); </script> <center> <table border="1" width="100%"> <tr> <td><script language="javascript">document.Write(str)</script></td> </tr> </table> </center> <br> </footer> </body> </html> 文件中的数据,请尝试使用gnu sed:

  String MessageID = "<blah@1223434556com">"
  ItemView ivew = new ItemView(3);
  service.TraceEnabled = true;
  ExtendedPropertyDefinition PidTagInternetMessageId = new ExtendedPropertyDefinition(4149, MapiPropertyType.String);
  SearchFilter sf = new SearchFilter.IsEqualTo(PidTagInternetMessageId, MessageID);
  FindItemsResults<Item> iCol = service.FindItems(WellKnownFolderName.Inbox, sf, ivew);

  foreach (Item item in iCol.Items)
  {
    Console.WriteLine(item.Subject);
  }