I am facing an issue that I have File that every time have different row count and it ends with sqlldr output that shows amount of rows exported to file
40968 rows selected.
Since this file will be picked up by next process I want to get rid of this additional text. So I came up with idea like this:
# Remove exported record count from end of file
remove_count=$(( $(wc -l < ${output_dir}/file.dat)-3 1 ))
echo "Removing extra lines from spooled file"
sed '$remove_count, $ d' file.dat >> file.dat
The problem is that it seems that sed
cannot use variable as amount of lines after which it should start deleting. I could not find better solution.
Is there something I can do better or anyone sees mistake?
Thanks in advance!
CodePudding user response:
You don't need such a complicated solution; head
can do this:
$ echo -e 'first\nsecond\nthird\nfourth' | head -n-3
first
See the manual:
-n, --lines=[-]NUM
print the first NUM lines instead of the first 10; with the
leading '-', print all but the last NUM lines of each file
CodePudding user response:
sed
can include variables expansion.
It is a matter of correct bash
quoting.
'
prevents any bash
expansion in string.
"
allow all bash
expansion in string.
Suggesting to try:
# Remove exported record count from end of file
remove_count=$(( $(wc -l < ${output_dir}/file.dat)-3 1 ))
echo "Removing extra lines from spooled file"
sed -i "$remove_count, \$ d" file.dat
CodePudding user response:
Thank you guys! All solutions worked!
CodePudding user response:
This might work for you (GNU sed):
sed '1N;N;$d;P;D' file
This will delete the last 3 lines of a file.
A programmatic version would be:
sed ':a;N;s/[^\n]*/&/3;Ta;d$;P;D' file
Where 3
can be any number of lines from the end of the file.