如何在没有行号和标签之间的空格的情况下进行grep?

时间:2016-12-01 06:34:21

标签: regex sed grep

public class ContactAdapter extends BaseAdapter implements Filterable{
private Context ctx;
private LayoutInflater lInflater;
private ArrayList<Contact> contactList;
private ArrayList<Contact> filteredContactList;
private ContactFilter contactFilter;

public ContactAdapter(Context context, ArrayList<Contact> contacts) {
    this.ctx = context;
    this.contactList = contacts;
    this.filteredContactList = contacts;
    this.lInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    getFilter();
}

@Override
public int getCount() {
    return contactList.size();
}

@Override
public Object getItem(int position) {
    return contactList.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

public Contact getContact(int position) {
    return ((Contact) getItem(position));
}

@Override
public View getView(int position, View convertView, final ViewGroup parent) {
    View view = convertView;
    if (view == null) {
        view = lInflater.inflate(R.layout.main, parent, false);
    }

    final Contact c = getContact(position);

    ((TextView) view.findViewById(R.id.contactName)).setText(c.getContactName());
    ((TextView) view.findViewById(R.id.contactPhone)).setText(c.getContactPhone());
    TextView contactCategory = (TextView) view.findViewById(R.id.contactCategory);
    contactCategory.setText(c.getContactCategory());
    TextView contactOrg = (TextView) view.findViewById(R.id.contactOrg);
    contactOrg.setText(c.getContactOrgName());

    if (c.getContactCategory().equalsIgnoreCase("Private")) {
        contactOrg.setVisibility(View.GONE);
    }

    Button buttonCall = (Button) view.findViewById(R.id.buttonCall);
    buttonCall.setFocusable(false);
    buttonCall.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {...}

@Override
public Filter getFilter() {
    if (contactFilter == null) {
        contactFilter = new ContactFilter();
    }
    return contactFilter;
}

private class ContactFilter extends Filter {

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults filterResults = new FilterResults();
        if (constraint !=null  && constraint.length() > 0) {
            ArrayList<Contact> tempList = new ArrayList<>();
            for (Contact contact : contactList) {
                if (contact.getContactName().toLowerCase().contains(constraint.toString().toLowerCase())) {
                    tempList.add(contact);
                }
            }
            filterResults.count = tempList.size();
            filterResults.values = tempList;
        } else {
            filterResults.count = contactList.size();
            filterResults.values = contactList;
        }
        return filterResults;
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        filteredContactList = (ArrayList<Contact>) results.values;
        notifyDataSetChanged();
    }
}

这将删除行号和空格:

 grep -in "todo" test.html

 1:            <span>todo:test<span>

就是这样:

grep -in "todo" test.html | sed -e "s/^[1-9]\d*:[ \t]*//"

但我想要这个:

<span>todo:test<span>

1 个答案:

答案 0 :(得分:2)

您可以使用此sed

sed '/todo/s/^\([1-9]\+:\)[ \t]*/\1/' file.html

<强>测试

$ echo "1:         <span>todo:test<span>" | sed '/todo/s/^\([1-9]\+:\)[ \t]*/\1/'
1:<span>todo:test<span>