简体   繁体   中英

awk/sed/grep/perl using field separator then only showing specific lines

Recently I learned that shairport (airplay emulator) now creates a fifo file that pipes metadata in this format:

   artist=Justin Bieber
   title=Baby
   album=My World 2.0
   artwork=cover-2de1df4b978034bcd789ea10b1111a.jpg
   genre=Pop
   comment=

I have an lcd display I'd like to send it to, but I'm trying to parse it out to show basically this:

   Justin Bieber
   Baby
   My World 2.0

I tried using awk to get me the above results, but if anyone has any suggestions using sed, grep, perl or anything else, I'm open to it!

Anyways, here's a bunch of stuff that I've tried so far:

awk -F"=" '{print $2}'

to get it to show:

   Justin Bieber
   Baby
   My World 2.0
   cover-2de1df4b978034bcd789ea10b1111a.jpg
   Pop

Cool almost there, but I don't really need to show the artwork, genre, and comment.

This is the command I used to filter out those extra fields:

awk '!/artwork=/ && !/genre=/ && !/comment=/ && /./'

To print this:

   artist=Justin Bieber
   title=Baby
   album=My World 2.0

Now I just need to combine the two, so I tried this (not to mention a bunch of other variations):

awk '!/artwork=/ && !/genre=/ && !/comment=/ && /./' && -F"=" '{print $2}'

But I get the same results as above:

   artist=Justin Bieber
   title=Baby
   album=My World 2.0

I know I'm missing something very basic, but I'm just completely stuck.

You're almost there, you need to put -F"=" at the start.

$ awk -F"=" '!/artwork=/ && !/genre=/ && !/comment=/{print $2}' file
Justin Bieber
Baby
My World 2.0

OR

The below awk would take only the first three rows,

$ awk -F"=" 'NR<4{print $2}' file
Justin Bieber
Baby
My World 2.0

只需使用正则表达式功能即可将所有条件组合为一个:

awk -F"=" '!($1 ~ /^(artwork|genre|comment|)$/) {print $2}' file

Perl版本

perl -F/=/ -lane 'print $F[1] if $F[0] =~ /artist|title|album/' file

Using a perl one-liner

perl -ne 'print if s/(artwork|genre|comment)=//' file

Explanation:

Switches :

  • -n : Creates a while(<>){...} loop for each “line” in your input file.
  • -e : Tells perl to execute the code on command line.

Using grep and cut :

grep -Ev "(artwork|genre|comment)" file | cut -d= -f 2
Justin Bieber
Baby
My World 2.0

Using perl :

perl -ne  '!/(artwork|genre|comment)/ && s/.*=(.*)/$1/g && print' file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM