extracting line numbers from a file and redirecting them to another file

i have applied the following command on a file named unix.txt that contains the string "linux from the text"

grep -n -i -w "linux from the text" unix.txt

and the result is

5:Today's Linux from the text systems are split into various branches, developed over time by AT&T as well as various commercial vendors and non-profit organizations.
11:The second edition of Linux from the text was released on December 6th, 1972.
106:Linux From THE text is a good book actually

Now i want to copy the line numbers namely 5, 11, 106 into a new file.. If i use the cut command, i wud have to know beforehand the range of columns which is not possible as the line numbers maybe 1 digit, 2 digit, 3 digit or even more perhaps...

Pls advice

Nope, not necessarily; you do not have to know the range of columns beforehand.
You could cut the pattern based on a delimiter and then pick up the first column.
For example, if the pattern is "abc~def", and you set the delimiter to "~", then your 1st column would be "abc", whose length could be arbitrary.
Check the man page of your system for the syntax.

tyler_durden

awk -vVar="linux from the text" 'tolower($0)~Var{print NR}' file > new.file
1 Like

Try this :

 grep -n -i -w "linux from the text" unix.txt | awk -F ":" '{print $1}' > newfile.txt
1 Like

using cut

 
grep -n -i -w "linux from the text" unix.txt | cut -d":" -f1 > newfile.txt

1 Like

thanks a lot guys.