这是我要处理的示例文件。我想过使用关键字" description"作为某种RS,但不知道如何做到这一点,也不是一致的。
背景:我正在处理一个日志文件,其中包含第一行中的日期/时间戳(APR12),第二行中有关于日志的描述。这种描述适用于少数几个日志和missig。
001 APR12 aaa bbb
Description: This is a test file.
002 APR12 aaa bbb
Description: This is another test file.
003 APR12 aaa XXX
004 APR12 aaa bbb
Description: This is another,after skipping one.
001 APR12 aaa bbb Description: This is a test file.
002 APR12 aaa bbb Description: This is another test file.
003 APR12 aaa XXX
004 APR12 aaa bbb Description: This is another,after skipping one.
答案 0 :(得分:3)
$ awk '{printf "%s%s", (/^[0-9]/?rs:FS), $0; rs=RS} END{print ""}' file
001 APR12 aaa bbb Description: This is a test file.
002 APR12 aaa bbb Description: This is another test file.
003 APR12 aaa XXX
004 APR12 aaa bbb Description: This is another,after skipping one.
答案 1 :(得分:1)
每当当前行不以&#34开头时,您可以添加换行符;说明":
awk 'NR>1 && !/^Description/{print ""}{printf "%s ", $0}' file
NR>1
阻止在输出开头添加换行符。
如果处理了任何行,您可能还需要添加END
块以在输出的末尾添加换行符:END{if(NR)print ""}
。
答案 2 :(得分:1)
这可能适合你(GNU sed):
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LabelColourExample {
private Timer timer;
private JLabel label;
private Color [] colours = {
Color.red,
Color.blue,
Color.green,
Color.cyan,
Color.yellow,
Color.magenta,
Color.black,
Color.white
};
private int counter;
private static final int GAP = 5;
private ActionListener timerActions = new ActionListener () {
@Override
public void actionPerformed ( ActionEvent ae ) {
label.setForeground ( colours [ counter++ ] );
counter %= colours.length;
}
};
public LabelColourExample () {
counter = 0;
}
private void displayGUI () {
JFrame frame = new JFrame ( "Label Colour Example" );
frame.setDefaultCloseOperation ( JFrame.DISPOSE_ON_CLOSE );
JPanel contentPane = new JPanel ();
contentPane.setLayout ( new BorderLayout ( GAP, GAP ) );
label = new JLabel ( "MyName", JLabel.CENTER );
label.setOpaque ( true );
contentPane.add ( label, BorderLayout.CENTER );
JButton button = new JButton ( "Stop" );
button.addActionListener ( new ActionListener () {
@Override
public void actionPerformed ( ActionEvent ae ) {
JButton button = ( JButton ) ae.getSource ();
if ( timer.isRunning () ) {
timer.stop ();
button.setText ( "Start" );
} else {
timer.start ();
button.setText ( "Stop" );
}
}
} );
contentPane.add ( button, BorderLayout.PAGE_END );
frame.setContentPane ( contentPane );
frame.pack ();
frame.setLocationByPlatform ( true );
frame.setVisible ( true );
timer = new Timer ( 1000, timerActions );
timer.start ();
}
public static void main ( String[] args ) {
Runnable runnable = new Runnable () {
@Override
public void run () {
new LabelColourExample ().displayGUI ();
}
};
EventQueue.invokeLater ( runnable );
}
}
读取整个文件中的线对,如果对中的第二行开始sed 'N;s/\n\(Description\)/ \1/;P;D' file
,则用空格替换换行符。
答案 3 :(得分:1)
sed ':a
N;$!ba
s/\n\([^0-9]\)/ \1/g' YourFile
如果你在4.2.2之后有一个允许-z
(-Z option)的GNU sed版本。感谢@JJoao提供此优化代码。
sed -z 's/\n\(^[0-9]\)/ \1/g' YourFile
答案 4 :(得分:0)
可能过于复杂,但这里有sed
的解决方案:
# Does the line contain description?
# Yes ...
/Description/{
# Exchange hold and pattern space
x
# Append hold space to pattern space
# separated by newline
G
# Remove that newline by a space
s/\n\+/ /gp
}
# No ...
/Description/! {
# Exchange hold and pattern buffer
x
# The hold buffer contains a prefix line
/Description/! {
# Print it
p
}
# Exchange hold and pattern buffer again
x
# Store current line in the hold buffer
h
}
答案 5 :(得分:0)
perl -p0e 's!\n(?=Des)! !g'
(未经测试) - 它将所有文件加载到内存中......