我有.dat
个文件:
#id|firstName|lastName|gender|birthday|creationDate|locationIP|browserUsed
933|Mahinda|Perera|male|1989-12-03|2010-03-17T13:32:10.447+0000|192.248.2.123|Firefox
1129|Carmen|Lepland|female|1984-02-18|2010-02-28T04:39:58.781+0000|81.25.252.111|Internet Explorer
4194|Hồ Chí|Do|male|1988-10-14|2010-03-17T22:46:17.657+0000|103.10.89.118|Internet Explorer
8333|Chen|Wang|female|1980-02-02|2010-03-15T10:21:43.365+0000|1.4.16.148|Internet Explorer
8698|Chen|Liu|female|1982-05-29|2010-02-21T08:44:41.479+0000|14.103.81.196|Firefox
8853|Albin|Monteno|male|1986-04-09|2010-03-19T21:52:36.860+0000|178.209.14.40|Internet Explorer
10027|Ning|Chen|female|1982-12-08|2010-02-22T17:59:59.221+0000|1.2.9.86|Firefox
我希望firstName
从特定行开始lastName
,birthday
和id
。
示例:如果输入是933,我想提取(用空格分隔):
Mahinda Perera 1989-12-03
答案 0 :(得分:1)
这应该这样做:
#!/bin/sh
id="$1"
awk -F '|' -v ID="$id" '($1==ID){print $2, $3, $5}' infile
用作:
$ script.sh 933
Mahinda Perera 1989-12-03
答案 1 :(得分:1)
awk -F'|' '$1 ~ /933/{print $2, $3, $5}' file
Mahinda Perera 1989-12-03
如果字段1匹配933打印以下字段:2,3和5.
答案 2 :(得分:0)
使用GNU sed:
public void getImage(){
//String id = editTextId.getText().toString().trim();
session = new SessionManager(getApplicationContext());
HashMap<String, String> user = session.getUserDetails();
String user_id = user.get(SessionManager.KEY_ID);
class GetImage extends AsyncTask<String,Void,Bitmap> {
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(Profile.this, "Uploading...", null, true, true);
}
@Override
protected void onPostExecute(Bitmap b) {
super.onPostExecute(b);
loading.dismiss();
imageView.setImageBitmap(b);
}
@Override
protected Bitmap doInBackground(String... params) {
String user_id = params[0];
String add = "http://www.example.com/folder/getProfile.php?user_id="+user_id;
URL url = null;
Bitmap image = null;
try {
url = new URL(add);
image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return image;
}
}
GetImage gi = new GetImage();
gi.execute(user_id);
}
$ id=933
$ sed -n "/^$id"'|/{s/^[^|]*|\([^|]*\)|\([^|]*\)|[^|]*|\([^|]*\)|.*$/\1 \2 \3/p;q}' infile
Mahinda Perera 1989-12-03
阻止打印;其余的做到了这一点:
-n
要让它在BSD sed下运行,"/^$id"'|/ { # If a line starts with the ID... (notice quoting for parameter expansion)
# Capture second, third and fifth field, discard rest, print
s/^[^|]*|\([^|]*\)|\([^|]*\)|[^|]*|\([^|]*\)|.*$/\1 \2 \3/p
q # Quit to avoid processing the rest of the file for nothing
}'
和右括号之间必须有另一个分号。
表明awk更适合这个问题。
答案 3 :(得分:-1)
使用:
awk '{print $2 $3 $5}' infile.dat