Issue
I am working on displaying the following file:
V 220504203222Z 553A21806C0B711ED9E112E0B6680BED unknown /C=DE/ST=BY/L=FU/O=a.com/OU=msc-dev/CN=ovpn-server-2020/[email protected]
R 220925164608Z 211103011032Z E5D625C06F3D6DA4D3A92C092E8D12A0 unknown /C=DE/ST=BY/L=FU/O=a.com/OU=msc-dev/CN=grom/[email protected]
V 220925165555Z 185B52B7D6ABFF011FB0D7F826DE8851 unknown /C=DE/ST=BY/L=FU/O=a.com/OU=msc-dev/CN=bind9/[email protected]
V 220925171609Z 8764585492AE0097F76DFD7C9E2E75C7 unknown /C=DE/ST=BY/L=FU/O=a/OU=msc-dev/CN=bind-9/[email protected]
V 220925171854Z 5B7D28350F750F5745553E0647B79F63 unknown /C=DE/ST=BY/L=FU/O=a.com/OU=msc-dev/CN=qnap/[email protected]
V 220929131353Z 99E85C7B92BFF646480854B282288E2A unknown /C=DE/ST=BY/L=FU/O=a.com/OU=msc-dev/CN=ftp/[email protected]
V 221012163300Z 39403EA3995349FF8FEC487BD59A1F98 unknown /C=DE/ST=BY/L=FU/O=akaritech.com/OU=msc-dev/CN=masc-mbp/[email protected]
V 221012163553Z 80668723276AB0B93CF6765F5EA56837 unknown /C=DE/ST=BY/L=FU/O=akaritech.com/OU=msc-dev/CN=masc-ipad/[email protected]
V 221016205831Z 7ABD06B289E35D084D19EA170B8F2EC0 unknown /C=DE/ST=BY/L=FU/O=a.com/OU=msc-dev/CN=monitoring.brave-vesperia.com/[email protected]
What I want is the first, second and fifth column separated by a space on the same line. I'm using awk:
#!/bin/bash
index="/opt/easy-rsa/easyrsa3/pki/index.txt"
for line in $(awk '{printf "%c %i %s\n", $1, $2, $5} ' ${index}); do
if ! [[ ${line} =~ ^R.* ]]; then
printf "%s\n" $line ;
fi
done
However, the output I get is:
V
220504203222
/C=DE/ST=BY/L=FU/O=a.com/OU=msc-dev/CN=ovpn-server-2020/[email protected]
220925164608
unknown
V
220925165555
/C=DE/ST=BY/L=FU/O=a.com/OU=msc-dev/CN=bind9/[email protected]
V
220925171609
/C=DE/ST=BY/L=FU/O=a.com/OU=msc-dev/CN=bind-9/[email protected]
V
220925171854
/C=DE/ST=BY/L=FU/O=a.com/OU=msc-dev/CN=qnap/[email protected]
V
220929131353
/C=DE/ST=BY/L=FU/O=a.com/OU=msc-dev/CN=ftp/[email protected]
V
221012163300
/C=DE/ST=BY/L=FU/O=a.com/OU=msc-dev/CN=masc-mbp/[email protected]
V
221012163553
/C=DE/ST=BY/L=FU/O=a.com/OU=msc-dev/CN=masc-ipad/[email protected]
V
221016205831
/C=DE/ST=BY/L=FU/O=a.com/OU=msc-dev/CN=monitoring.brave-vesperia.com/[email protected]
I don't understand where the extra line breaks are coming from.
Solution
The spaces are being added because for line in $(awk ...)
splits the output of awk
at whitespace, not newlines.
You could solve this by setting IFS
to just contain newlines, or pipe awk
to a while read line
loop. But it's simpler to just do the whole thing in awk
.
awk '$1 != "R" {printf "%c %i %s\n", $1, $2, $5} ' "${index}"
Answered By - Barmar Answer Checked By - Dawn Plyler (WPSolving Volunteer)