count字符串并打印到新的outfile

时间:2017-11-15 18:37:14

标签: bash sed find

我试图计算特定字符串的出现次数" PhD"在文件' X'中,将该计数#(例如3),并将/ cat打印到另一个文件的开头,3次,此特定字符串

Graduate StudentID 1
Graduate StudentID 2
Graduate StudentID 3

StudentID之后的数字反映了计数。

我对这一点毫无希望的尝试是($ OUT应该写入文件)并且我不确定如何解决(明显的)结果错误。

find /home/college/Applications/Graduate -name "*.inp" -exec sed 's/[PhD]//g' input | uniq -c print >$OUT {$1} \;

1 个答案:

答案 0 :(得分:1)

我将如何做到这一点:

    <?xml version='1.0' encoding='utf-8'?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    <hibernate-mapping default-cascade="all">
        <class name="entity.RoomEntity" table="room" schema="" catalog="medicine">
            <id name="id">
                <column name="id" sql-type="int unsigned" not-null="true"/>
                <generator class="native"/>
            </id>
            <property name="roomNumber">
                <column name="room_number" sql-type="int unsigned" not-null="true"/>
            </property>
            <property name="numberOfBeds">
                <column name="number_of_beds" sql-type="int unsigned"/>
            </property>
            <property name="idResponsibleDoctor">
                <column name="id_responsible_doctor" sql-type="int unsigned" not-null="true"/>
            </property>
            <property name="id_department">
                <column name="id_department" sql-type="int unsigned" not-null="true"/>
            </property>       
            <set name="beds">
                <key column="id_room"></key>
                <one-to-many class="entity.OccupiedBedsEntity"/>
            </set>
        </class>
    </hibernate-mapping>

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" enter code here"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping default-cascade="all">
<class name="entity.OccupiedBedsEntity" table="occupied_beds" schema="medicine" catalog="medicine">
    <id name="id">
        <column name="id" sql-type="int unsigned" not-null="true"/>
        <generator class="native"/>
    </id>
    <property name="since">
        <column name="since_" sql-type="datetime" not-null="true"/>
    </property>
    <property name="to">
        <column name="to_" sql-type="datetime" not-null="true"/>
    </property>
    <property name="id_room">
        <column name="id_room" sql-type="int unsigned" not-null="true"></column>
    </property>
</class>
</hibernate-mapping>

这假设您的输出文件名是#!/bin/bash # Count number of occurrences # Use -o | wc -l instead of -c to count multiple occurrences in same line count=$(grep -ro 'PhD' --include='*.inp' /home/college/Applications/Graduate | wc -l) # Intermediate file tmp=$(tempfile) # Output file out=outfile.txt { # Print header lines for (( i = 1; i <= count; ++i )); do printf '%s %d\n' 'Graduate StudentID' "$i" done # Print existing contents cat "$out" } > "$tmp" # Rename intermediate file mv "$tmp" "$out"