0

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.

wtrdk
  • 131
  • 2
  • 12
  • Use double quotes ins shell to use variables – anubhava Mar 30 '15 at 14:14
  • The question is duplicate because sed isn't expanding variables. bash is. – Joshua Mar 30 '15 at 15:42
  • Your question **is** a duplicate. You are simply not following the answer. In particular you have changed the `${first}` to `'${first}'` for no reason, which creates a syntax error. – Bakuriu Mar 30 '15 at 16:29

0 Answers0