I want to know how one can get the extracted content from a file using line number. I have crores of lines in the file and sometimes while importing it in some database, like mongodb, it gives trouble. So I need to edit it. But opening GB's of file again and again just to edit a single line is not worthy.
Hence, I would like to know how I can extract the content from a file using its line number and then edit anything, that I want to edit and save it to the file again, without opening the file completely. Say for examples:

command line_number 20000 /path/to/input/file


This your first testing text.
edit line_number 20000 /path/to/input/file

Then the line appear and I move to specific characters, say your in the line and change it to my. Now my new content on the line becomes:

This is my first testing text.

Please note that I do not know whether the line has the word or not. So, first I need to check the line. Display the line using line number. and then edit it.
Is there a way to achieve it.
Let me clear everyone, I am using UBUNTU 16.04. This question is completely UBUNTU based and no other OS or Linux distribution. Please not this.


You can use sed or perl to print line X:

$ cat -n input.txt                                                                                    
     1  roses are red
     2  violets are blue
     3  sed is interesting
     4  and perl is too

$ perl -ne 'print $_ if $. == 3'  input.txt                                                           
sed is interesting
$ sed -n '3p' input.txt                                                                               
sed is interesting

Then you can use sed 'NUMBERs/WORD/NEWWORD/' file.txt

$ sed '3s/interesting/fun/'  input.txt                                                                
roses are red
violets are blue
sed is fun
and perl is too

You can use -i flag as well to edit file in-place.

Same with Perl:

$ perl -pe 's/interesting/fun/ if $. == 3'  input.txt                                                 
roses are red
violets are blue
sed is fun
and perl is too

NOTE: With the two editing examples, you don't need to know whether the line contains word or not. Both perl and sed will replace the word if and only if the line contains the word/pattern. If it doesn't - the line will remain the same.

  • I do not know what is there on the line. In question I just gave an example. I need to first display the line to see what is there. How can I find a word if I don't know whether the line contain it or not? – Jaffer Wilson Feb 10 '17 at 6:02
  • @JafferWilson OK. I'll edit my answer. Your original question wasn't very clear. – Sergiy Kolodyazhnyy Feb 10 '17 at 6:04
  • I have edited the question you can check now. – Jaffer Wilson Feb 10 '17 at 6:05
  • Are you searching for a particular word, or are you checking and maybe modifying whatever is in a particular line [number]? Or some kind of combination of particular words and line numbers? – sudodus Feb 10 '17 at 7:02
  • @sudodus It is simple. I am looking for the error in the line. While inserting in database like mongodb if any error occurs then it will show you the line number and the error. I want to see that line with line number. And if there is any error in that line I want to edit that particular line. May be replacing few characters or word is what required many a times. Hence, to know the proper error I need to know the line and check it. Then only I can make edit, right? – Jaffer Wilson Feb 10 '17 at 7:08

You can use vi command to edit files without opening them:

vi -c "%s/<old_value>/<new_value>/g|wq" <file_path>

for example

vi -c "%s/<100>/<200>/g|wq" /home/aditya/sample.txt

Update 1:

sed '<line_number>!d' <filepath> ( this will print the line)

sed -i '<line_number>s/.*/<new_text>/' <filepath>  ( this is replace everthing in line with 'new_text')

Update 2 :

sed -i '<line_number>s/<old_text>/<new_text>/' <filepath>  ( this is replace 'old_text' with 'new_text')

See if this helps.

  • I do not use vim. I do not know how to interact with it. Can you show some commandline method for such task? – Jaffer Wilson Feb 10 '17 at 7:09
  • that is the terminal command I gave you. :P you have to put that command in terminal, it internally calls 'vi' and do the find & replace for you. if the old_value is found, it will be replaced by new_value. :) – Aditya T Feb 10 '17 at 7:19
  • How can I know what is therein the line? First I should know the line then only I can edit it. I have only line number to see the line. Blindly I cannot put any value. What do you think? :) – Jaffer Wilson Feb 10 '17 at 7:23
  • well, that's a tough requirement you have got there :P I will update answer after checking out few things. – Aditya T Feb 10 '17 at 7:27
  • Sure.. I know it is a bit tough. Trying from one week to get out of it. At last have posted the question here to know. – Jaffer Wilson Feb 10 '17 at 7:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.