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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting extracting line numbers from a file and redirecting them to another file
# 1  
Old 08-21-2011
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"
Code:
grep -n -i -w "linux from the text" unix.txt

and the result is
Code:
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

Last edited by Franklin52; 08-22-2011 at 04:30 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 08-21-2011
Quote:
Originally Posted by arindamlive
...
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...
...
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
# 3  
Old 08-22-2011
Quote:
Originally Posted by arindamlive
Now i want to copy the line numbers namely 5, 11, 106 into a new file.
Code:
awk -vVar="linux from the text" 'tolower($0)~Var{print NR}' file > new.file

This User Gave Thanks to danmero For This Post:
# 4  
Old 08-22-2011
Try this :

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

This User Gave Thanks to Jairaj For This Post:
# 5  
Old 08-22-2011
using cut

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

This User Gave Thanks to itkamaraj For This Post:
# 6  
Old 08-23-2011
Bug

thanks a lot guys.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to find all the multi line pattern and redirecting it to a file?

I've a file like this {multi line ....... ....... pattern} { some other stuff ......... } {multi line ....... ....... pattern} { some other stuff ......... } and so on (2 Replies)
Discussion started by: aamir_raihan
2 Replies

2. Shell Programming and Scripting

Extracting values based on line-column numbers from multiple text files

Dear All, I have to solve the following problems with multiple tab-separated text file but I don't know how. Any help would be greatly appreciated. I have access to Linux mint (but not as a professional). I have multiple tab-delimited files with the following structure: file1: 1 44 2 ... (5 Replies)
Discussion started by: Bastami
5 Replies

3. Shell Programming and Scripting

Extracting lines from text files in folder based on the numbers in another file

Hello, I have a file ff.txt that looks as follows *ABNA.txt 356 24 36 112 *AC24.txt 457 458 321 2 ABNA.txt and AC24.txt are the files in the folder named foo1. Based on the numbers in the ff.txt file, I want to extract the lines from the corresponding files in the foo1 folder and... (2 Replies)
Discussion started by: mohamad
2 Replies

4. UNIX for Dummies Questions & Answers

Extracting lines from a text file based on another text file with line numbers

Hi, I am trying to extract lines from a text file given a text file containing line numbers to be extracted from the first file. How do I go about doing this? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

5. UNIX for Dummies Questions & Answers

problem with extracting line in file

My file looks like this and i need to only extract those with PDT_AP21_B and output it to another file. Can anyone help? Thanks. PDT_AP21_R,,, 11 TYS,,,,T17D1207230742TYO***T17DS,,C PDT_AP21_L,,,9631166650001 ,,,,T17D1207230903TYOTYST17DS ,,C... (3 Replies)
Discussion started by: Alyssa
3 Replies

6. UNIX for Dummies Questions & Answers

Extracting line1 from a file with certain file pattern in line 7

Hello there, I am new to unix and would like to do the following, hoping someone would give some guide, thanks in advance. Lets say i have a file like this: A w x y w x 0.1 B w x y w x 0.3 C w x y w x 0.7 D w x y w x 0.9 E w x y w x 0.2 So i would like to extract line 1 data where line... (2 Replies)
Discussion started by: seiksoon
2 Replies

7. Shell Programming and Scripting

Extracting line from a file

Hi, I would like to extract a certain portion of sentences from a particular sentence i.e. to extract the last section embrace by input output Please advice. Cheers (6 Replies)
Discussion started by: dwgi32
6 Replies

8. UNIX for Dummies Questions & Answers

Help: Sorting and extracting a line from a file

I was proposed with the following problem: The file 'numbers' contains a list of numbers. Write a command to place the largest one of those numbers in the file 'largest' (there should be nothing else in that file). Do not use the 'head' command in your answer. I think if i used the sort... (1 Reply)
Discussion started by: stava
1 Replies

9. Shell Programming and Scripting

Extracting a line in a text file

If my file looks like this…. 10 20 30 and I want to take each line individually and put it in a variable so it can be read later in it's on individual test statement, how can I do that? I guess what I'm asking is how can I extract each line individually. Thanks (5 Replies)
Discussion started by: terryporter51
5 Replies

10. Shell Programming and Scripting

Extracting specified line from a file using awk

Hi, I am just trying to extract a line at a time from a file using awk and can't yet extract even the first line using the following:- awk '/(^) && (NR==1)/ {print $1}' file1 Is there an obvious silly error here? Thanks (3 Replies)
Discussion started by: sirtrancealot
3 Replies
Login or Register to Ask a Question