I have files that are over 150000 lines. Using sed I would like to grab lines wich contains a certain string. I would like to divide the file in 3 parts, and grab the line containing the string in each part. For now I have this:
linenumbertotal=$(wc -l < $filename)
echo -e ${linenumbertotal}
linenumberdivided=$((linenumbertotal / 3))
echo ${linenumberdivided}
first=$((linenumbertotal - linenumberdivided - linenumberdivided))
second=$((linenumbertotal - linenumberdivided))
third=$linenumbertotal
echo $first
echo $second
echo $third
This results in the correct amount of lines for each part.
Now I would like to do something like this:
sed '1,${first}d' $filename > first.tmp
grep 2003 first.tmp
sed '${first},${second}d' > second.tmp
grep 2004 second.tmp
sed -'${second},${third}d' > third.tmp
grep 2005 third.tmp
But the variable in the sed parameter results in an error.
I tried:
sed "1,'${first}'!d" $filename
but it results in sed: expression #1, character 3: unexpected ','
Some users marked this question as a duplicate of another question, but my question is not the same... My question is about extracting lines from a file, the other question is about replacing text.