我有一个样本文件,如下所示:
5 6 7 8
55 66 77 88
A B C D
1 2 3 4
2 4 6 8
3 8 12 16
E F G H
11 22 33 44
and so on...
我想在bash脚本中输入命令,或者只在bash终端中输入命令,以独立于其他列提取其中一列。例如,我想使用pattern = C执行类似grep / awk命令的操作并获得以下输出:
C
3
6
12
如何提取一个独立于其他列的特定列,并在模式后面提取一行以便在我的输出中输入7或G列的上一列?
答案 0 :(得分:2)
如果在找到的期限之后总是有3条记录:
awk '{for(i=1;i<=NF;i++) {if($i=="C") col=i}} col>0 && rcount<=3 {print $col; rcount++}' test
这将查看记录中的每个字段,如果找到&#34; C&#34;,它将捕获列号i
。如果列号大于0,则它将打印列的内容。它最多可记录3条记录,然后停止打印。
答案 1 :(得分:0)
//Message.java
package Message;
import java.io.*;
public class Message implements Serializable {
String msg;
String tag;
String username;
private static final long serialVersionUID = 4L;
// Methods.
}
// Reading MessageObjects in Server.java.
@Override
public void run() {
Message message = null;
try {
while ((message = (Message)reader.readObject()) != null) {
// Processing message.
}
}
}
// Sending Message Objects in android App.
public void onClick(View v) {
if(!msgBox.getText().toString().equals("")) {
final String msg;
try {
msg = msgBox.getText().toString();
writer.writeObject(new Message(msg, CLIENT, username));
msgBox.setText("");
writer.flush();
// Updating ui etc.
}
catch (IOException e) {
e.printStackTrace();
}
textBox.smoothScrollBy(textBox.getMaxScrollAmount(), 100);
}
}
// Connection-method in android app, initalizes streams.
private boolean connect(String username, String address, int port) {
boolean connected = false;
try {
server = new Socket(address, port);
InputStreamReader(server.getInputStream());
reader = new ObjectInputStream(server.getInputStream());
writer = new ObjectOutputStream(server.getOutputStream());
writer.writeObject(new Message("!newUser",AUTOMATED,username));
writer.flush();
connected = true;
System.out.println("Connected!");
}
catch (Exception ex) {
ex.printStackTrace();
System.out.println("Cannot Connect!");
connected = false;
// UI-things.
}
if(connected){
// Thread that listens for replies.
listenThread();
}
return connected;
}