Hot answers tagged

37 votes

How to split a PEM file

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 ...
32 votes
Accepted

iptables show just one chain

I almost deleted this question. D'oh! From man iptables: -L, --list [chain] List all rules in the selected chain.
  • 951
29 votes

How to split a PEM file

The split command is available on most systems, and its invocation is likely easier to remember. If you have a file collection.pem that you want to split into individual-* files, use: split -p "--...
16 votes
Accepted

Why don't random numbers in AWK change after the first run?

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, ...
  • 4,457
15 votes

How to make in SSH private key from one line, three lines

I would personally base64-encode the key, store it, then base64 decode it when you need it. Encode: echo "-----BEGIN RSA PRIVATE KEY----- my_super_secret_password -----END RSA PRIVATE KEY-----" | ...
  • 9,440
11 votes

How to get all fingerprints for .ssh/authorized_keys(2) file

A one-liner based on the /dev/stdin trick from ℝaphink's answer and man xargs → EXAMPLES: egrep '^[^#]' ~/.ssh/authorized_keys | xargs -n1 -I% bash -c 'ssh-keygen -l -f /dev/stdin <<<"%"...
  • 347
9 votes

How to split a PEM file

If you want to get a single certificate out of a multi-certificate PEM bundle, try: $ openssl crl2pkcs7 -nocrl -certfile INPUT.PEM | \ openssl pkcs7 -print_certs | \ awk '/subject.*CN=host....
  • 1,263
8 votes

Extract a ids from text file

The most straightforward answer is to not try and utilize grep, sed, awk, etc for this - even though they will do it, you will most assuredly end up making assumptions and having to constantly shuffle ...
  • 914
6 votes

How to split a PEM file

If you are handling full chain certificates (i.e. the ones generated by letsencrypt / certbot etc), which are a concatenation of the certificate and the certificate authority chain, you can use bash ...
  • 171
6 votes
Accepted

Calculate sum of several sizes of files in Bash

Use stat instead of du: #!/bin/bash for i in `grep -v ^# ~/cache_temp | grep -v "dovecot.index.cache"`; do [ -f "$i" ] && totalsize=$[totalsize + $(stat -c "%s" "$i")] done echo ...
  • 1,227
6 votes

Calculate sum of several sizes of files in Bash

If you need to use the file this snippet is hopefully efficient. xargs -a cache_file stat --format="%s" | paste -sd+ | bc -l The xargs is to prevent overflowing the argument limit but getting the ...
6 votes

Calculate sum of several sizes of files in Bash

According to du(1), there is a -c option whose purpose is to produce the grand total. % du -chs * /etc/passwd 92K ABOUT-NLS 196K NEWS 12K README 48K THANKS 8,0K TODO 4,0K /etc/passwd 360K ...
  • 61
4 votes

bash/sed/awk/etc remove every other newline

The most idiomatic way to do it in awk is as follows: awk 'ORS=NR%2?FS:RS' file It outputs: Runtime Name: vmhba2:C0:T3:L14 Group State: active Runtime Name: vmhba3:C0:T0:L14 Group State: active ...
  • 248
4 votes

iptables show just one chain

For those who might want to look at the mangle or nat tables with chains you can do so like iptables -L mychainname -t mangle Then it will show you that chain in the mangle table.
4 votes

Calculate sum of several sizes of files in Bash

If you remove the "-h" flag from your "du" command, you'll get the raw byte sizes. You can then add them with the ((a += b)) syntax: a=0 for i in $(find . -type f -print0 | xargs -0 du -s | awk {'...
  • 3,188
3 votes

Running CAT in SCREEN no output to file

The reason this won't work is that the shell splits the command into two parts like this: screen -d -m /bin/cat /var/www/html/filter/unfiltered.txt awk '{print $1}' >/var/www/html/filter/first....
  • 1,569
3 votes

How to get all fingerprints for .ssh/authorized_keys(2) file

ssh-keygen -l -f - <authorized_keys produces a nice listing for you: # ssh-keygen -l -f - <authorized_keys 2048 SHA256:GzZ7.................................RqTEag foo (RSA) 2048 SHA256:/y0.........
3 votes

Replace a string once another string has been found

Even if this can probably be solved by a sed command, it would be simpler to use the proper command to change yum configuration: yum install yum-utils yum-config-manager --enable remi-php56 And ...
  • 2,096
3 votes
Accepted

How to extract the last two (or any number) directories from a path with sed?

echo "/your/directory/here/bla" | awk -F"/" '{ print "/"$(NF-1)"/"$NF }' should work with a find, i didn't try, but something like this should be working : find /yourfolder/structure -type d | awk -...
  • 512
3 votes

How to show specific luns from lsscsi command?

Try this: lsscsi 4 0 1 | sed -r '/\[4:0:1:(1|12|13)\].*/!d' Output will be: [4:0:1:1] /dev/sdab [4:0:1:12] /dev/sdo [4:0:1:13] /dev/sdp
3 votes

text processing linux using grep awk or anything else

IMHO Typically date/time manipulation works better when you treat a time string as time and not a text string. Use the formatting options of the date command to get the timestamp to display in ...
  • 73k
3 votes

sed/awk - find match by one string and change different string on the same line

Sure you can solve your issue with one-liner, but it isn't reusable. So I suggest try to use this quickly written script and try to figure out, what it does, to improve your skills. #!/bin/sh ...
3 votes

How to add the filename as prefix to each line of the text file? (batch processing; subdirectories)

You can use: grep -r . . The first . is RegExp patern matching any character. It will match all lines. The second . is for the current folder, but you can replace it with any valid path. E.g.: ...
3 votes

How do I cut just the file name from from ls -l output?

/bin/ls -1 That leaves out all the goo :) The problem being that ls in most distributions is aliased to something like “ls -la”. You can check that with “alias ls” and see if anything comes up ...
3 votes
Accepted

How do I cut just the file name from from ls -l output?

It's simpler to use a glob instead of ls to work with a list of file names for f in *; do echo file \'$f\' done
  • 2,743
2 votes

How to parse and convert ini file into bash array variables?

Here's a pure bash solution. This is a new and improved version of what chilladx posted: https://github.com/albfan/bash-ini-parser For a really easy to follow initial example: After you download ...
  • 399
2 votes

Color awk output on commandline?

For the convenience of future querents, here's a set of functions that one could put into a file called, let's say color.awk. function BLACK(X) { return "\033[30m" X "\033[0m" } ...
2 votes

AWK and file names with space in it.

You could try this. awk '{print substr($0, index($0,$9))}' For example this is the output of ls command: -rw-r--r--. 1 root root 73834496 Dec 6 10:55 File with spaces 2 If you use simple awk ...
2 votes
Accepted

Split large log file WITHOUT keeping the original (splitting in-place)

What might work is to stream parts of it directly into xz - I guess you can compress a log file good enough to fit both the original and the compressed parts into your space left. Get the number of ...
  • 97.6k
2 votes
Accepted

Print last column containing spaces and special characters

The error is here: awk '$2 == '"$PID" if $PID is not strictly numeric, then you're doing an equality check against an unquoted string, so really, who knows how awk will interpret it? $ PID="3000)" $ ...

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