简体   繁体   中英

Sed Command to delete a line

I want to remove a line which matches a specified pattern inside a test file. I want to use the command sed /'lines to match'/d inside a linux shell script. This is the code:

#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

In this code i want to remove from the first line in the test file that matches with the expression inside the variable HEADEREXPRESSION. Then the result will be placed tmp variable.
This is the text file:

$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||

I want to remove the first line using a pattern expression like:

$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|

I invoke this script using this line command:

./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|"

However the first line that match with the pattern will not remove in the output file. How can i fix the issue?

您可以使用以下sed命令从文件中删除模式

sed -i.bak "/$pattern/d" $FILE_NAME

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM