Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I'm new to Linux. I need to edit .conf file from open terminal only and not using any text editors. That is, can I add words and sentences to a config file from open terminal?

Example: command /home/.../file.conf -add 'abcd' to 23rd line and so on. And finally save it. Is it possible to search a specific word in that config file and add a new text to next line of that config file using only command?

share|improve this question
    
Looks like sed can come to help. –  hatter Mar 17 at 6:33
1  
Ask a single question. –  Avinash Raj Mar 17 at 6:33

4 Answers 4

Adding words and sentences to a config file from open terminal can be easily achieved with sed.

sed -i '23iabcd' file.conf

inserts at line 23 the text abcd into file file.conf

-i does the modification directly to file file.conf.

If you want to use awk then:

awk -v n=23 -v s="abcd" 'NR == n {print s} {print}' file > file.conf

The following adds one line after SearchPattern.

sed -i '/SearchPattern/aNew Text' SomeFile.txt

It inserts New Text one line below each line that contains SearchPattern.

To add two lines, you can use a \ and enter a newline while typing New Text.

 sed -i '/pattern/a \
line1 \
line2' inputfile
share|improve this answer

I usually do this way when I am programming my script to do same what you are asking but programmatically.

echo "Hello you!" >> myfile.txt
echo "this is 2nd line text" >> file.txt
echo "last line!" >> file.txt

Viola! You got it. Important to note >> means adding new line to existing file meanwhile > just simply overwrite everything.

share|improve this answer
    
thank you for your prompt reply. But i need to add like more than 3000words to 23rd line of my .conf file sequentially. Is there any easier way? –  user259060 Mar 17 at 11:24
awk '{if ($1 ~ /regex/) print $1 "content to be added"; else print $1}' < inputfile > outputfile

Notes:

  • regex is a regular expression (also known as regex), it defines the search criteria. Regular expressions allow for very customizable searches and the syntax understood by awk is in the manual. In the simplest case - search a string "as it is", character by character - just put a backslash before special characters (see manual for the list of special characters)

How it works:

  • open inputfile for reading the input lines, clear outputfile and open it for writing the output lines
  • for each line, run the block in braces:
    • if the line matches the regular expression, then output the line with content appended
    • otherwise, output the very same line.
share|improve this answer

i got the solution to my own question guys.

just use ed command ed -s /home/.../abc.conf <<< $'23i\ntext\n.\nwq'

text can contain 27 lines . i.e you can copy 27 lines from a text file and paste 27 lines to your config file. but i need to run ed command simultaniously in order to add few more texts to the same config file.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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