shell - I want to delete a batch from file -
i have file , contents :
|t1234 010000000000 02123456878 05122345600000000000000 07445678920000000000000 09000000000123000000000 10000000000000000000000 .t1234 |t798 013457829 0298365799 05600002222222222222222 09348977722220000000000 10000057000004578933333 .t798
here 1 complete batch means start |t , end .t. in file have 2 batches.
i want edit file delete batch record 10(position1-2),if position 3 till position 20 0 delete batch.
please let me know how can achieve writing shell script or syncsort or sed
or awk
.
i still little unclear want, think have enough give outline on bash solution. part unclear on line contained first 2 characters of 10
, remaining 0
's, looks last line in each batch. not knowing how wanted batch (with matching 10
) handled, have written remaining wanted batch(es) out file called newbatch.txt
in current working directory.
the basic outline of script read each batch temporary array. if during read, 10
, 0
's match found, sets flag delete batch. after last line read, checks flag, if set outputs batch number delete. if flag not set, writes batch ./newbatch.txt
.
let me know if requirements different, should close solution. code commented. if have questions, drop comment.
#!/bin/bash ifn=${1:-dat/batch.txt} # input filename ofn=./newbatch.txt # output filename :>"$ofn" # truncate output filename declare -i bln=0 # batch line number declare -i delb=0 # delete batch flag declare -a ba # temporary batch array [ -r "$ifn" ] || { # test input file readable printf "error: file not readable. usage: %s filename\n" "${0//*\//}" exit 1 } ## read each line in input file while read -r line || test -n "$line"; printf " %d %s\n" $bln "$line" ba+=( "$line" ) # add line array ## if chars 1-2 == 10 , chars 3 on == 00... if [ ${line:0:2} == 10 -a ${line:3} == 00000000000000000000 ]; delb=1 # set delete flag fi ((bln++)) # increment line number ## if line starts '.' if [ ${line:0:1} == '.' ]; ## if delete batch flag set if [ $delb -eq 1 ]; ## nothing (but show batch no. delete) printf " => deleting batch : %s\n" "${ba[0]}" ## if delb not set, write batch output file else printf "%s\n" ${ba[@]} >> "$ofn" fi ## reset line no., flags, , uset array. bln=0 delb=0 unset ba fi done <"$ifn" exit 0
output (to stdout)
$ bash batchdel.sh 0 |t1234 1 010000000000 2 02123456878 3 05122345600000000000000 4 07445678920000000000000 5 09000000000123000000000 6 10000000000000000000000 7 .t1234 => deleting batch : |t1234 0 |t798 1 013457829 2 0298365799 3 05600002222222222222222 4 09348977722220000000000 5 10000057000004578933333 6 .t798
output (to newbatch.txt)
$ cat newbatch.txt |t798 013457829 0298365799 05600002222222222222222 09348977722220000000000 10000057000004578933333 .t798
Comments
Post a Comment