32

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

Example: command /home/.../file.conf -add 'abcd' to the 23rd line and so on. And finally, save it.

Is it possible to search a specific word in that config file and add new text to the next line of that config file using only the command?

2
  • 1
    Looks like sed can come to help.
    – nitishch
    Mar 17, 2014 at 6:33
  • 1
    Ask a single question. Mar 17, 2014 at 6:33

5 Answers 5

55

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

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

1
  • 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, 2014 at 11:24
23

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
6

You can also use the printf command.

To add lines to your file

$ printf "\nThis is a new line to your document" >> file.txt

To overwrite the file

$ printf "This overwrites your file" > file.txt
1
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.
0

I found a solution to my own question using the ed command

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

Text can contain 27 lines. You can copy 27 lines from a text file and paste 27 lines to your config file. But I need to run the ed command simultaneously in order to add more text to the same config file.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .