Using Bash I want to look for a string within a specific file and if it isn't there, I want to add it.

Posted on 2005-04-22
Last Modified: 2010-04-22
Using Bash I want to look for a string within a specific file and if it isn't there, I want to add it.

I want to check the file /etc/my.cnf and if the string skip-innodb is not there, I want to add it. Here's the catch, I want to add it directly after the line socket=/var/lib/mysql/mysql.sock

How do you do that?
Question by:heavenlydishes
    LVL 22

    Expert Comment

    /etc/my.cnf is usually a configuration file for the MySQL database.  It is actually a composite configuration file for several different classes of programs, with several different sections labeled like [client], [mysqld], [mysqldump], etc.  The socket= line can appear multiple times in the file, appearing at least in the [client] and [mysqld] sections.

    Which sections do you want to modify, and how?
    LVL 8

    Accepted Solution


    x=$(egrep -c "skip-innodb" text.txt)
    if ((x == 0))
    perl -i -pe 's/socket=(.*)$/socket=$1\nskip-innodb/' text.txt
    LVL 8

    Expert Comment

    If you have multiple lines in the file with socket=....
    change the reg ex i mentioned above accoringly so that you select the correct line you want
    LVL 51

    Expert Comment

    grep skip-innodb my.cnf && perl -i.bak -pe 's/(socket=.*)$/$1\nskip-innodb/' my.cnf
    LVL 8

    Expert Comment

    Thanks for the grade :)

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
    The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now