Print a specific part in an output

Yes, you can do that. Your command would be:

cat /boot/config-3.19.0-32-generic | grep CONFIG_ARCH_DEFCONFIG | awk -F'"' '{print $2}'

which would return only:

arch/x86/configs/x86_64_defconfig

The awk command uses field separators with the -F command, and to set it to use double-quotes, you type it in with single-quotes around it like -F'"'. Then the '{print $2}' tells awk to print the second set after the field separator.

Hope this helps!


You can do that with a single grep command:

grep -Po '^CONFIG_ARCH_DEFCONFIG="\K[^"]*' /boot/config-3.19.0-32-generic

Or (a bit longer and more convulted):

grep -Po '^CONFIG_ARCH_DEFCONFIG="\K.*?(?=")' /boot/config-3.19.0-32-generic
  • -P: tells grep to interpret the pattern as a PCRE (Perl Compatible Regular Expression);
  • -o: tells grep to print only the match;
  • ^CONFIG_ARCH_DEFCONFIG=": matches a CONFIG_ARCH_DEFCONFIG=" string at the start of the line;
  • \K: discards the previously matched substring;

#1:

  • [^"]*: matches any number of any character not " (greedily).

#2:

  • .*?: matches any number of any character (lazily);
  • (?="): lookahead (zero-lenght assertion, it doesn't match any character); matches if the following character is a ".
% grep -Po '^CONFIG_ARCH_DEFCONFIG="\K[^"]*' /boot/config-4.2.0-16-generic
arch/x86/configs/x86_64_defconfig
% grep -Po '^CONFIG_ARCH_DEFCONFIG="\K.*?(?=")' /boot/config-4.2.0-16-generic
arch/x86/configs/x86_64_defconfig

There are many ways to skin this cat, here is a sed way:

sed -nr 's/^CONFIG_ARCH_DEFCONFIG="([^"]+)"$/\1/p' /boot/config-3.19.0-32-generic

Here we are matching the line and capturing the desired portion i.e. the portion within double quotes (([^"]+)) and then replacing the whole line with the captured group (\1) only.

Example:

% sed -nr 's/^CONFIG_ARCH_DEFCONFIG="([^"]+)"$/\1/p' /boot/config-3.13.0-32-generic
arch/x86/configs/x86_64_defconfig