Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash删除变量字段_Bash - Fatal编程技术网

Bash删除变量字段

Bash删除变量字段,bash,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|fn

我正在编写一个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
读取p时
;做
#如果这是第一次通过循环,抓取列标题并保存它
如果[“$计数器”=“0”];然后
((计数器++))
COL_HEADING=$p
COMPANY_ID_COL_NUM=“$(echo$p | awk-F\|”{for(i=1;i>$FILENAME
fi
#将当前行输出到输出文件中
echo$p>>$FILENAME
fi
#要用作输入的文件
完成
我想这个命令行会起作用:

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

干杯!

我想这个命令行会起作用:

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

干杯!

如果你对awk没意见,那就直截了当地说:

awk -F'|' -v OFS='|' '
    NR == 1 {header = $1 OFS $2 OFS $3; next}
    { 
        f = "Company_" $4 ".txt"; 
        if (!seen[f]) {
            print header > f
            seen[f] = 1
        }
        print $1,$2,$3 > f 
    }
' input.txt

如果你对awk没意见,那就直截了当地说:

awk -F'|' -v OFS='|' '
    NR == 1 {header = $1 OFS $2 OFS $3; next}
    { 
        f = "Company_" $4 ".txt"; 
        if (!seen[f]) {
            print header > f
            seen[f] = 1
        }
        print $1,$2,$3 > f 
    }
' input.txt

“<代码>公司的代码也在行的中间吗?什么样的脚本做列删除/分裂看起来像现在?”AubHaVaA:是的,公司的ID也可以在行的中间,如:ID.fNeNo.LNEXY地址,PosiyYid id MordNo.OrthyNo.*总的@ eTaReSiNeNeNo.<代码>如果Apple yid属于,创建文件名到输出。如果[$COMPANY_ID-eq 1234],则COMP=“CompanyA”FILENAME=“{COMP}.txt”echo$FILENAME;elif[$COMPANY_ID-eq 5678],则COMP=“CompanyB”FILENAME=“4{COMP}.txt”echo$FILENAME;否则COMP=“两者都不存在”;FILENAME=“${COMP}.txt”“echo$FILENAME;fi#如果不存在,则创建文件并在[!-f$FILENAME]时添加标题”请将$FILL标题输出到输出文件回音$P>>文件名<代码>编辑到你的文章中,这样格式化就保持理智了。<代码>公司的ID 也在行的中间吗?ID也可以在行的中间,如:ID.fNeNo.MyNo.OrthyNo.yUM号“ETANRIESNER <代码> >,如果PosiyYid属于,则创建文件名,如果[$FuffyIID-EQ 1234 ],则COMP =“FuffyA”文件名=“{COMP}.TXT”ECHO $文件名;ELIF [ $公司YID-EQ 5678 ],然后COMP =“Cuffyb”FILENAME=“4{COMP}.txt”echo$FILENAME;else COMP=“note”;FILENAME=“${COMP}.txt”;echo$FILENAME;fi#如果不存在,则创建文件并在[!-f$FILENAME]时添加标题;然后echo$COL_HEADING>>$FILENAME fi#将当前行输出到输出文件echo$p>>$FILENAME请将其编辑到您的帖子中,以便格式保持正常。OP表示列数和哪个列是
公司id
不是固定的详细信息。这需要找到正确的列,只删除该列.OP表示列数和哪个列是
公司id
不是固定的详细信息。这需要找到正确的列,只删除该列。