Bash删除变量字段

时间:2015-11-17 18:11:42

标签: bash

我正在编写一个bash脚本,根据其中一个字段的值将.txt文件拆分为两个单独的文件。我需要在写入文件之前删除company_id列中包含的值,如下所示:

输入:input.txt

id|fname |lname    |company_id
1 |Bob   |Jones    |1234
2 |Mary  |Stewart  |5678    
3 |Miguel|Velazquez|5678
4 |Sara  |Brooks   |1234

输出1:Company_A.txt

id|fname |lname    
1 |Bob   |Jones    
4 |Sara  |Brooks   

输出2:Company_B.txt

id|fname |lname      
2 |Mary  |Stewart      
3 |Miguel|Velazquez

当我想在另一个文件中使用这个bash脚本用于相同目的时,问题出现了,而另一个文件中的company_id列而不是第4列可能是第15列。我已经想出了一种方法来将列号存储在变量COMANY_ID_COL中,但随后想要使用该值来删除输出中的字段。这是我到目前为止所做的:

   while read p; do

    # If this is the first pass through the loop grab the column heading and save it
    if [ "$COUNTER" = "0" ]; then
        ((COUNTER++))
        COL_HEADING=$p

        COMPANY_ID_COL_NUM="$(echo $p | awk -F\| '{for(i = 1; i <= NF; i++) {if($i == "company_id") {print i}}}')"

        # remove the correct column from line by using value stored in $COMPANY_ID_COL_NUM
        ?????????

    else 
        # Grab the COMPANY_ID from the column in the input file
        # Note: not yet set up to use the variable COMPANY_ID_COL_NUM which will change to a variable instead of the last col
        COMPANY_ID="$(echo $p | awk -F\| '{print $(NF)}')"

            #If COMPANY_ID matches value, format filename for ouptut
                if [ $COMPANY_ID -eq 1234 ]
                then 
                    COMP="CompanyA"
                    FILENAME="${COMP}.txt"
                    echo $FILENAME;
                elif [ $COMPANY_ID -eq 5678 ]
                then 
                    COMP="CompanyB";
                    FILENAME="${COMP}.txt"
                    echo $FILENAME;
                else
                    COMP="Neither";
                    FILENAME="${COMP}.txt"
                    echo $FILENAME;
                fi

        # If there isn't a file already create it and add the column heading to it
        if [ ! -f $FILENAME ]; then
            echo $COL_HEADING >> $FILENAME          
        fi

        # Output current line into output files
        echo $p >> $FILENAME 

    fi

# File to use as input
done < input.txt

2 个答案:

答案 0 :(得分:0)

我想这个命令行可以解决这个问题:

for i in `cat input.txt | awk -F'|' {' print $4'} OFS="|" | grep -e [0-9]`; do head -n1 input.txt |  awk -F'|' {' print $1, $2, $3'} OFS="|" > company_$i.txt; grep $i input.txt |  awk -F'|' {' print $1, $2, $3'} OFS="|" >> company_$i.txt;  donee

或者如果你想要一个脚本文件:

#!/bin/sh
for i in `cat $1 | awk -F'|' {' print $4'} OFS="|" | grep -e [0-9]`; do
head -n1 $1 | awk -F'|' {' print $1, $2, $3'} OFS="|" > output_$i.txt
grep $i $1 | awk -F'|' {' print $1, $2, $3'} OFS="|" >> output_$i.txt
done

干杯!

答案 1 :(得分:0)

如果你对awk没问题,那就非常直接了:

// Convert seconds since 01-01-1970 00:00:00 to Gregorian date.

void rtc_time64_to_tm(time64_t time, struct rtc_time *tm)