0
I want to split my $file containing x lines in half, and check how many lines contain "dead" in a log. I started off with the following:
half=`expr $(egrep -c . $file) / 2`
sed -n 1,${half}p $file |
xargs echo $file $half $(egrep -c dead $I) > log_1
sed -n ${half},${egrep -c . $file}p |
xargs echo $file $half $(egrep -c dead $I) > log_2
output for the first sed
command is ok, but when substituting egrep
in the range of sed
it goes wrong:
DeadOrAlive 5 2
-bash: ${half},${egrep -c . $file}p: bad substitution
Is there a more efficient way of splitting the file in bash
?
$(...)
and${...}
are different constructs. The former is command substitution, the latter is parameter expansion. – choroba – 2018-04-10T19:42:48.393Your first
egrep
counts non-empty lines.grep -c ^ file
would produce the total line count, including empty lines. (If your file doesn't contain any empty lines, then of course both are equivalent.)wc -l <file
is probably faster because it doesn't need to do any regex matching. If you want to specifically count non-empty lines, then of course you do have to check for matches. – tripleee – 2018-04-11T06:20:25.953What do you expect
$I
to contain? – tripleee – 2018-04-11T06:28:45.503sed "$half,\$"
will select lines from$half
through to the end of file, though your code will include the middlemost file (line number$half
) in both the first and the second half. – tripleee – 2018-04-11T06:30:08.513sed "1,${half}d" file
will delete the first$half
lines, and print the rest. With that, you can get the file properly split into two non-overlapping partitions. – tripleee – 2018-04-11T10:12:21.997