我想删除与测试文件中指定模式匹配的行。我想在linux shell脚本中使用命令sed /'lines来匹配'/ d。这是代码:
#Define input parameters
SCRIPT_NAME=$(basename $0)
PATHFILE=$1
NAMEFILE1=$2
NAMEFILE2=$3
HEADEREXPRESSION=$4
#Define input file
INPUTFILE1=$PATHFILE$NAMEFILE1
INPUTFILE2=$PATHFILE$NAMEFILE2
TEMPFILE=${INPUTFILE2}_TMP
touch $TEMPFILE
echo $INPUTFILE2
echo $HEADEREXPRESSION
echo $TEMPFILE
#Remove header line from the second document
sed '/${HEADEREXPRESSION}/d' $INPUTFILE2 >> $TEMPFILE
#rm -f $INPUTFILE2
#echo $TEMPFILE >> $INPUTFILE1
在这段代码中,我想从测试文件的第一行中删除与变量HEADEREXPRESSION中的表达式匹配的内容。然后结果将放置tmp变量 这是文本文件:
$ $ SYSDATE | PARENT_ELID | PARENT_ID_NUMBER | PARENT_REV | PARENT_LASTUPDATE_DATE | CHILD_ELID | CHILD_ID_NUMBER | CHILD_REV | CHILD_STATE | CHILD_LASTUPDATE_DATE | TSITEM | QUANTITY_DMS | OPTION_NAME |
2016/03/09 05:00:11 | AQJ2DJTH | 185273-13 | NC | 2016/03/09 01:56:14 | AQJ2DJTL | 185273-14 | NC | RELEASED | 2016/03/09 01:49:58 | 1 | 1 ||
2016/03/09 05:00:11 | AQJ2DJTL | 185273-14 | NC | 2016/03/09 01:49:58 | AZEUQ03Z0047AR | 185273-15 | A | RELEASED | 2003/01/15 09:46:18 | 1 | 1 ||
2016/03/09 05:00:11 | C3PK881C6A5JK0 | 201200-142 | M | 2016/03/09 03:20:26 | C3PK881BU135M0 | 201200-142EX | NC | RELEASED | 2016/03/09 03:21:26 | 1 | 1 ||
2016/03/09 05:00:11 | C3PK881C6A5JK0 | 201200-142 | M | 2016/03/09 03:20:26 | C3PK881BU2HVF0 | 201200-142RL | NC | RELEASED | 2014/07/23 09:35:14 | 1 | 1 | 1 |
2016/03/09 05:00:11 | BU9WPBTYLTYC60 | 3112535-02 | NC | 2016/03/09 04:58:35 | BU9WPBTYMATB60 | 3112865-02 | NC | CREATE | 2016/02/26 02:40:14 | 101 | 1 ||
2016/03/09 05:00:11 | BU9WPBTYLTYC60 | 3112535-02 | NC | 2016/03/09 04:58:35 | DN09DMYM1ROY70 | 3112892 | NC | RELEASED | 2011/05/13 21:09:16 | 102 | 1 ||
2016/03/09 05:00:11 | BU9WPBTYLTYC60 | 3112535-02 | NC | 2016/03/09 04:58:35 | DN09DMYM2CRJP0 | 3112897 | D2 | RELEASED | 2011/07/24 13:41:16 | 103 | 1 ||
2016/03/09 05:00:11 | BU9WPBTYLTYC60 | 3112535-02 | NC | 2016/03/09 04:58:35 | DN09DMYL210V60 | 1900169-1205 | NC | RELEASED | 2011/06/02 08:14:33 | 104 | 2 ||
我想使用模式表达式删除第一行,如:
$SYSDATE$|PARENT_ELID|PARENT_ID_NUMBER|PARENT_REV|PARENT_LASTUPDATE_DATE|CHILD_ELID|CHILD_ID_NUMBER|CHILD_REV|CHILD_STATE|CHILD_LASTUPDATE_DATE|TSITEM|QUANTITY_DMS|OPTION_NAME|
我使用此行命令调用此脚本:
./delete_headeer.sh /wmextdata/scripts/DEV/primavera/SS_IPP_SOSLOAD/ SSP6_LINKS.TXT-20160309-000341 SSP6_LINKS.TXT-20160309_121000 "\$SYSDATE$|PARENT_ELID|PARENT_ID_NUMBER|PARENT_REV|PARENT_LASTUPDATE_DATE|CHILD_ELID|CHILD_ID_NUMBER|CHILD_REV|CHILD_STATE|CHILD_LASTUPDATE_DATE|TSITEM|QUANTITY_DMS|OPTION_NAME|"
但是,与模式匹配的第一行不会在输出文件中删除。 我该如何解决这个问题?
答案 0 :(得分:1)
您可以使用以下sed命令从文件中删除模式
sed -i.bak "/$pattern/d" $FILE_NAME