如何让awk在第二组单引号之间抓取字符串

时间:2018-02-12 21:49:42

标签: linux bash awk

请帮助,这让我很生气。

我有一个标准的Wp-config.php文件,我试图让awk只在一行输出数据库名称,数据库用户名和密码,但无论我尝试什么吐出无关的无意义或语法错误。

define('DB_NAME', 'pinkywp_wrdp1');

/** MySQL database username */
define('DB_USER', 'pinkywp_user1');

/** MySQL database password */
define('DB_PASSWORD', 'Mq2uMCLuGvfyw');

期望的输出:

pinkywp_wrdp1 pinkywp_user1 Mq2uMCLuGvfyw

实际输出:

./dbinfo.sh: line 28: unexpected EOF while looking for matching `''
./dbinfo.sh: line 73: syntax error: unexpected end of file

3 个答案:

答案 0 :(得分:3)

使用GNU awk:

使用'作为字段分隔符,如果当前行包含5列,请使用尾随空白打印第4列的内容。

awk -F "'" 'NF==5 {printf("%s ",$4)}' file

输出:

pinkywp_wrdp1 pinkywp_user1 Mq2uMCLuGvfyw 

答案 1 :(得分:2)

private void 

    createShortcutIcon() {
            // Checking if ShortCut was already added

            SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);

            boolean shortCutWasAlreadyAdded = sharedPreferences.getBoolean(
                    PREF_KEY_SHORTCUT_ADDED, false);

            if (shortCutWasAlreadyAdded)
                return;

            Intent shortcutIntent = new Intent(getApplicationContext(),
                    SplashScreen.class);
            shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            Intent addIntent = new Intent();
            addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
            addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "YourAppName");
            addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
                    Intent.ShortcutIconResource.fromContext(
                            getApplicationContext(), R.drawable.ic_launcher));
            addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
            getApplicationContext().sendBroadcast(addIntent);

            // Remembering that ShortCut was already added
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putBoolean(PREF_KEY_SHORTCUT_ADDED, true);
            editor.commit();
        }

答案 2 :(得分:1)

一些 awk 解决方案:

1)使用GNU flavor

awk -v RS="');" '{ printf "%s%s", (NR==1? "":OFS), substr($NF, 2) }END{ print "" }' file

2)引用 - 独立解决方案:

awk -F', ' '/define/{ 
                gsub(/^["\047]|["\047]\);$/, "", $2);
                printf "%s%s", (NR==1? "":" "), $2
            }
            END{ print "" }' file

输出(两种方法):

pinkywp_wrdp1 pinkywp_user1 Mq2uMCLuGvfyw