Hot answers tagged

47

can't test right now, but ... | paste - - should do it


46

You can do pretty much anything with apache log files with awk alone. Apache log files are basically whitespace separated, and you can pretend the quotes don't exist, and access whatever information you are interested in by column number. The only time this breaks down is if you have the combined log format and are interested in user agents, at which point ...


34

You can use nmap to run a ping scan. nmap -sP 192.168.254.* Starting Nmap 5.00 ( http://nmap.org ) at 2011-03-09 11:32 GMT Host xyzzy.lan (192.168.254.189) is up (0.00022s latency). MAC Address: 00:0C:29:5B:A5:E0 (VMware) Host plugh.lan (192.168.254.196) is up (0.00014s latency). MAC Address: 00:0C:29:2E:78:F1 (VMware) Host foo.lan (192.168.254.200) is up. ...


33

$ nc localhost 9571 | awk -F: '/rating/ { print $2 }'


25

It's at /sys/class/net/eth0/address (or more precisely /sys/devices/pciXXXX:XX/XXXX/net/eth0/address where the XXX is your PCI bus ID, but this varies between systems). (Incidentally, I found this with find /sys -name eth0 and looking at the files in the directories identified.)


23

One thing I've never seen anyone else do, for reasons that I can't imagine, is to change the Apache log file format to a more easily parseable version with the information that actually matters to you. For example, we never use HTTP basic auth, so we don't need to log those fields. I am interested in how long each request takes to serve, so we'll add that ...


17

awk '{ if (length($0) < 16384) print }' yourfile >your_output_file.txt would print lines shorter than 16 kilobytes, as in your own example. Or if you fancy Perl: perl -nle 'if (length($_) < 16384) { print }' yourfile >your_output_file.txt


17

Here's another hack using plain bash without temporary files: while read l; do [[ -n $l && ${l###} = $l ]] && ssh-keygen -l -f /dev/stdin <<<$l; done < .ssh/authorized_keys You can easily make it a function in your .bashrc: function fingerprints() { local file="$1" while read l; do [[ -n $l && ${l###} = $l ]] ...


15

Use nmap. Important to run it as root so you get the MAC addresses. Example: sudo nmap -sP 192.168.1.0/24 Will scan 192.168.1.1 - 192.168.1.255. Look up CIDR notation on wiki if you're not familiar with this subnet notation. You should be able to get nmap from the repos of any recentish Linux distro, e.g. sudo apt-get install nmap or sudo yum ...


14

Quanta beat me to it, but I'll include a sed variant if you're that way inclined: nc localhost 9571 | sed -ne 's/^rating://p' Ditto what MadHatter said, though. Your current solution is perfectly sound. (Although I'd grep for "^rating:" rather than just the word to ensure you only get the line you want.)


14

echo "121.122.121.111] other characters in logs from sendmail...." | sed 's/].*//' So if you have a file full of lines like that you can do sed 's/].*//' filename


13

Forget about awk and grep. Check out asql. Why write unreadable scripts when you can use sql like syntax for querying the logfile. Eg. asql v0.6 - type 'help' for help. asql> load /home/skx/hg/engaging/logs/access.log Loading: /home/skx/hg/engaging/logs/access.log sasql> select COUNT(id) FROM logs 46 asql> alias hits SELECT COUNT(id) FROM logs ...


12

From https://www.gnu.org/software/gawk/manual/html_node/Numeric-Functions.html CAUTION: In most awk implementations, including gawk, rand() starts generating numbers from the same starting number, or seed, each time you run awk. Thus, a program generates the same results each time you run it. The numbers are random within one awk run but predictable from ...


11

The issue here is parsing with ls. Consider to take a look here: Why you shouldn't parse the output of ls. The reason why you shouldn't do it is since UNIX allows almost any character in a filename, including whitespace, newlines, commas, pipe symbols, and pretty much anything else you'd ever try to use as a delimiter except NUL. In its default mode, if ...


11

This was previously answered on StackOverflow : awk ' split_after == 1 {n++;split_after=0} /-----END CERTIFICATE-----/ {split_after=1} {print > "cert" n ".pem"}' < $file Edit 29/03/2016 : See @slugchewer answer


10

The different alternatives exist, not because each can do the same thing as the other (although this is true for localized cases, or for full programming languages such as Perl), but rather because they can do different things. sed is not very suitable for processing tabular data, and using pure bash for that is usually more pain than one would like. awk ...


10

huh what a crazy script :) lets try like this :) #!/bin/bash cat accounts | while read account do echo -e "\n Account: $account \n"; echo sudo ./backup_maildir "$account"; done if everything looks fine and sudo line works out of script as expected drop the echo and voila


10

I think this will split the line as you want sed -e 's/\]/\]\n/g' log | sed -e 's/^ *//g' | awk '/^\[/ {print}' First put a newline after each ] then remove any leading spaces and finally print the lines beginning with [. Your input line becomes [Wed Aug 08 11:39:41 2012] [error] [client 155.94.70.224] [line "271"] [id "960020"] [rev "2.2.5"] [msg ...


9

The awk snippet works for extracting the different parts, but you still need to know which section is the key / cert / chain. I needed to extract a specific section, and found this on the OpenSSL mailinglist: http://openssl.6102.n7.nabble.com/Convert-pem-to-crt-and-key-files-tp47681p47697.html # Extract key openssl pkey -in foo.pem -out foo-key.pem # ...


9

This one command will do what you want: grep -o '\[[^]]*\]' inputfile


8

You can also just use the shell: nc localhost 9571 | while IFS=: read key val; do [[ $key = "rating" ]] && echo "$val"; done


8

How about cut instead: cat logfile | cut -d "]" -f1


8

There is operatingsystemmajrelease % facter operatingsystemmajrelease 6 If you have redhat-lsb-core package installed, facter will get as well the family of lsb-provided facts (which includes lsbmajdistrelease): % facter |grep ^lsb lsbdistcodename => Final lsbdistdescription => CentOS release 6.4 (Final) lsbdistid => CentOS lsbdistrelease => ...


7

Use tail -n +X filename to print from the Xth line to end of file.


7

As long as you run this command from a host in the same network segment, nmap will report all of the MAC address for each host is discovers. For example: sudo nmap 192.168.1.0/24 -sP Starting Nmap 4.76 ( http://nmap.org ) at 2011-03-09 06:29 EST Host old.net (192.168.1.1) appears to be up. MAC Address: 00:18:39:C5:A1:DC (Cisco-Linksys) For the MAC ...


7

yes, you can (and should) use (one) awk instead of (two) grep and cut: $ nc localhost 9571 | awk -F: '/^rating:/ { print $2 }' Be sure to match you line as good as you can to avoid ugly bugs. /rating/ works, /^rating/ is better (safer), /^rating:/ is best (in this case).


7

One possibility is: awk 'ORS=NR%2?" ":"\n"' If the line number is evenly divisible by 2, end with a new line, otherwise, end with a space. (Tested on: CentOS 6, GNU Awk 3.1.7) Using sed (see explanation): sed ':a;N;$!ba;s/\nGroup/ Group/g' Further reading: Removing new line characters


7

If you want to use sed, there's no reason to read the whole file into memory. You can merge every other line like this: sed 'N;s/\n/ /' inputfile Use any character you'd like instead of the space. Here's another way using awk: awk '{printf "%s", $0; if (getline) print " " $0; else printf "\n"}' inputfile The if/else handles the case where there is an ...


7

Oh my god, that's awful. Your script uses bash; I suggest you do this instead: #!/bin/bash for i in *.wav; do mv "${i}" "${i%.wav}.ext"; done See the Bash Guide for more details on parameter expansion.


6

I would use simple python script for this job since it has built in INI parser: #!/usr/bin/env python import sys, ConfigParser config = ConfigParser.ConfigParser() config.readfp(sys.stdin) for sec in config.sections(): print "declare -A %s" % (sec) for key, val in config.items(sec): print '%s[%s]="%s"' % (sec, key, val) and then in ...



Only top voted, non community-wiki answers of a minimum length are eligible