如何使用带有regex的linux sed查找和删除文本文件中的长模式

时间:2018-10-29 17:23:27

标签: regex sed

我正在将许多bibtex文件解析为R,以进行一些数据分析。但是,摘要会定期引起问题,我希望事先使用sed删除它们。

我找到了sed 's/Abstract\s\=\s[{][{]//' < file.bib

要成功删除Abstract条目,然后

sed 's/[}][}]\,//' < file.bib删除右括号和逗号。

但是,我无法以任何方式将两者结合在一起,也无法删除两者之间的所有内容。例如,尝试:

sed 's/^Abstract\s\=\s[{][{][\s\S]*[}][}]\,$//' < file.bib

这是bibtex引用的样子:

@article{ ISI:000072671200001,
Author = {Edmondson, A and Moingeon, B},
Title = {{From organizational learning to the learning organization}},
Journal = {{MANAGEMENT LEARNING}},
Year = {{1998}},
Volume = {{29}},
Number = @article{ ISI:000072671200001,
Author = {Edmondson, A and Moingeon, B},
Title = {{From organizational learning to the learning organization}},
Journal = {{MANAGEMENT LEARNING}},
Year = {{1998}},
Volume = {{29}},
Number = @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_reservation);

    firebaseAuth = FirebaseAuth.getInstance();
    textViewUserid = findViewById(R.id.addReservation_userId_edittext);

    FirebaseUser user = firebaseAuth.getCurrentUser();
    textViewUserid.setText(user.getEmail());

    gestureDetector = new GestureDetector(this);

    mDisplayDate = findViewById(R.id.addReservation_fromTime_edittext);
    mDisplayDate2 = findViewById(R.id.addReservation_toTime_edittext);


    mDisplayDate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Calendar cal = Calendar.getInstance();
            int year = cal.get(Calendar.YEAR);
            int month = cal.get(Calendar.MONTH);
            int day = cal.get(Calendar.DAY_OF_MONTH);
            int hour = cal.get(Calendar.HOUR_OF_DAY);
            int minute = cal.get(Calendar.MINUTE);

            DatePickerDialog datePickerDialog = new DatePickerDialog(
                    AddReservationActivity.this,
                    android.R.style.Theme_Light,
                    mDateSetListener,
                    year,month,day);

            TimePickerDialog timePickerDialog = new TimePickerDialog(
                    AddReservationActivity.this,
                    android.R.style.Theme_Light,
                    mTimeSetListener,
                    hour,minute,true);

            timePickerDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            timePickerDialog.show();
            datePickerDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            datePickerDialog.show();
        }
    });
    mDateSetListener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
            month = month + 1;
            final String date = dayOfMonth + "/" + month + "/" + year;

            mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                    if (minute < 10) {
                        String time = "" + hourOfDay + ":" + "0"+minute;
                        mDisplayDate.getText().toString();
                        mDisplayDate.setText(date + " " + time);
                    }else {
                        String time = "" + hourOfDay + ":" + minute;
                        mDisplayDate.getText().toString();
                        mDisplayDate.setText(date + " " + time);

                    }
                }
            };
        }
    }
,
Pages = {{5-20}},
Month = {{MAR}},
DOI = {{10.1177/1350507698291001}},
ISSN = {{1350-5076}},
Unique-ID = {{ISI:000072671200001}},
}
,
Pages = {{5-20}},
Month = {{MAR}},
Abstract = {{This article reviews theories of organizational learning and presents a
   framework with which to organize the literature. We argue that unit of
   analysis provides one critical distinction in the organizational
   learning literature and research objective provides another. The
   resulting two-by-two matrix contains four categories of research, which
   we have called: (2) residues (organizations as residues of past
   learning); (2) communities (organizations as collections of individuals
   who can learn and develop); (3) participation (organizational
   improvement gained through intelligent activity of individual members),
   and (4) accountability (organizational improvement gained through
   developing individuals' mental models). We also propose a distinction
   between the terms organizational learning and the learning organization.
   Our subsequent analysis identifies relationships between disparate parts
   of the literature and shows that these relationships point to individual
   mental models as a critical source of leverage for creating learning
   organizations. A brief discussion of the work of two of the most visible
   researchers in this field, Peter Senge and Chris Argyris, provides
   additional support for this type of change strategy.}},
DOI = {{10.1177/1350507698291001}},
ISSN = {{1350-5076}},
Unique-ID = {{ISI:000072671200001}},
}

这就是我想要的样子:

using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
//Adapted from Unity3DCollege YouTube Video Tutorial https://www.youtube.com/watch?v=pdDrY8Mc2lU
[InitializeOnLoad]
public class CustomHierarchy : MonoBehaviour
{
    private static Vector2 offset = new Vector2(0, 2);
    public static Color gameObjectFontColor = Color.black;
    public static Color prefabOrgFontColor = Color.black;
    public static Color prefabModFontColor = Color.white;
    public static Color inActiveColor = new Color(0.01f, 0.4f, 0.25f);
    public static Color meshRendererColor = Color.yellow;
    public static List<string> componentsList = new List<string>();

    static CustomHierarchy()
    {
        EditorApplication.hierarchyWindowItemOnGUI += HandleHierarchyWindowItemOnGUI;
    }
    private static void HandleHierarchyWindowItemOnGUI(int instanceID, Rect selectionRect)
    {
        Color fontColor = gameObjectFontColor;
        Color backgroundColor = new Color(.76f, .76f, .76f);
        FontStyle styleFont = FontStyle.Normal;
        var obj = EditorUtility.InstanceIDToObject(instanceID);
        GameObject gameObj = EditorUtility.InstanceIDToObject(instanceID) as GameObject;

        if (Selection.instanceIDs.Contains(instanceID))
        {
            backgroundColor = new Color(0.24f, 0.48f, 0.90f);
        }
        if (obj != null)
        {
            var prefabType = PrefabUtility.GetPrefabType(obj);
            if (gameObj.activeInHierarchy == false)
            {
                backgroundColor = inActiveColor;
            }

            if (prefabType == PrefabType.PrefabInstance)
            {
                styleFont = FontStyle.Bold;
                PropertyModification[] prefabMods = PrefabUtility.GetPropertyModifications(obj);
                foreach (PropertyModification prefabMod in prefabMods)
                {
                    if (prefabMod.propertyPath.ToString() != "m_Name" && prefabMod.propertyPath.ToString() != "m_LocalPosition.x" && prefabMod.propertyPath.ToString() != "m_LocalPosition.y" && prefabMod.propertyPath.ToString() != "m_LocalPosition.z" && prefabMod.propertyPath.ToString() != "m_LocalRotation.x" && prefabMod.propertyPath.ToString() != "m_LocalRotation.y" && prefabMod.propertyPath.ToString() != "m_LocalRotation.z" && prefabMod.propertyPath.ToString() != "m_LocalRotation.w" && prefabMod.propertyPath.ToString() != "m_RootOrder" && prefabMod.propertyPath.ToString() != "m_IsActive")
                    {
                        if (gameObj.GetComponent<MeshRenderer>() == true)
                            fontColor = meshRendererColor;
                        else
                            fontColor = prefabModFontColor;

                        break;
                    }
                }
                if (fontColor != prefabModFontColor)
                {
                    if (gameObj.GetComponent<MeshRenderer>() == true)
                        fontColor = meshRendererColor;
                    else
                        fontColor = prefabOrgFontColor;
                }
            }
            else
            {
                if (gameObj.GetComponent<MeshRenderer>() == true)
                    fontColor = meshRendererColor;
            }
            Rect offsetRect = new Rect(selectionRect.position + offset, selectionRect.size);
            EditorGUI.DrawRect(selectionRect, backgroundColor);
            EditorGUI.LabelField(offsetRect, obj.name, new GUIStyle()
            {
                normal = new GUIStyleState() { textColor = fontColor },
                fontStyle = styleFont
            }
            );
        }
    }
}

2 个答案:

答案 0 :(得分:0)

您可以尝试将sed命令按顺序相互传递。像这样:

sed 's/Abstract\s\=\s[{][{]//' < file.bib | sed 's/[}][}]\,//'

您还可以尝试在模式中使用OR Regex运算符,例如:

sed 's/Abstract\s\=\s[{][{]|[}][}]\,//' < file.bib

这些都应该起作用。我希望这会有所帮助。

答案 1 :(得分:0)

这可能对您有用(GNU sed):

sed '/^Abstract = {{/,/.*}},$/d' file

这使用范围运算符,,结合删除命令d删除以Abstract = {{开头到以}},结尾的行。