• Skip to primary navigation
  • Skip to main content

Luc Russell

Data Engineering and Full Stack Development

  • Home
  • Blog

Awk Snippet for Extracting XML Messages with a Given ID from a Log File

03/27/2015 by Luc

I recently had occasion to extract XML messages with a specific id from log files, resulting in the following gnarly awk snippet. The sed statement at the end is for stripping out some unwanted debug statements. There must be a better way to do this, let me know in the comments (except don’t, because they are disabled, because apparently only spammers read this blog).

1
2
3
4
5
6
7
8
9
10
11
12
#$1 id tag (e.g. if the identifier is specified with <id>1234</id>, use id)
#$2 id (e.g. if the identifier is enclosed with <id>1234</id>, use id)
#$3 message separator (e.g. </message>)
#$4 file prefix (e.g. if there are multiple messages in the file with the same id, use this prefix when writing out the resulting files)
#$5 log file (the name of the file to read)
extract_message(){
awk -v var="<${1}>${2}<\\\/${1}>"  \
-v ORS="</${3}>\n" \
-v RS="<\\\/${3}>"  \
'$0 ~ var {print}' $5 | sed 's/^[0-9][0-9].*DEBUG.*</</' \
| awk -v pre="${4}" '{print $0 > pre NR}' RS='\\n\\n' -
}

Filed Under: code snippets Tagged With: bash

Java Collections Quick Reference

03/21/2015 by Luc

A cheat sheet for my frequently used/forgotten Java Collections snippets.

Quickly Create a List

1
2
3
4
import static java.util.Arrays.asList;
asList("hello", "goodbye");
import static java.util.Collections.unmodifiableList;
unmodifiableList(asList("hello", "goodbye"));

Empty Collection Syntax

1
List<String> requiredInputs = Collections.<String>emptyList();

Also see here.

Safe Empty Array

1
2
3
public static String[] safe(String[] other ) {
    return other == null ? new String[0] : other;
}

Convert Array to List

1
Arrays.asList(values)

Collection to Array

1
values.toArray(new String[values.size()])

Loop Over Keys And Values In A Map

1
2
3
4
5
6
for (Iterator<Map.Entry<String, Object>> it = resultsData.entrySet().iterator(); it.hasNext();) {
            Map.Entry<String, Object> entry = it.next();
            String key = entry.getKey();
            Object value = entry.getValue();
            System.out.println(String.format("key: %s, value: %s", key, value));
        }

Filed Under: code snippets, quick reference Tagged With: collections, java

Convert a Collection to Another Type With Commons Collections

06/19/2013 by Luc

1
2
3
4
5
6
7
8
9
10
11
12
private Collection<Apple> convert(final Collection<Orange> values) {
    final Collection<Apple> converted = CollectionUtils.collect(values, new Transformer(){
      public Object transform(final Object target){
        Orange result = null;
        if(target != null){
            result = new Apple(target.getName());
        }
        return result;
      }
    });
    return converted;
}

Filed Under: code snippets Tagged With: collections, java

How To Select Elements From a Collection With Apache Commons Collections

06/18/2013 by Luc

1
2
3
4
5
6
7
8
9
10
11
private List<Vegetable> getVegetablesLike(List<Vegetable> allVegetables,
                final Vegetable vegetable) {
    return (List<Vegetable>)
        select(allVegetables, new Predicate() {
        @Override
        public boolean evaluate(Object o) {
            Vegetable other = (Vegetable) o;
            return other.equals(vegetable);
        }
    });
}

Filed Under: code snippets Tagged With: collections, java

Script to Update WordPress Secret Keys in Every wp-config.php

05/05/2013 by Luc

1
2
3
4
5
6
7
8
#!/bin/bash
find . -name wp-config.php -print | while read line
do
curl http://api.wordpress.org/secret-key/1.1/salt/ > wp_keys.txt
sed -i.bak -e '/put your unique phrase here/d' -e '/AUTH_KEY/d' -e '/SECURE_AUTH_KEY/d' -e '/LOGGED_IN_KEY/d' -e '/NONCE_KEY/d' -e '/AUTH_SALT/d' -e '/SECURE_AUTH_SALT/d' -e '/LOGGED_IN_SALT/d' -e '/NONCE_SALT/d' $line
cat wp_keys.txt >> $line
rm wp_keys.txt
done

Filed Under: code snippets Tagged With: bash, wordpress

  • Go to page 1
  • Go to page 2
  • Go to Next Page »

Copyright ©2019 Luc Russell | Site by Sproutee

Copyright © 2019 · Aspire Pro on Genesis Framework · WordPress · Log in