How can I get awk to print without white space?
Omit the ,
s
awk -F\, '{print $2 ":" $1}'
Remove space separator between awk printf / print
Just change the parameters of the awk
command as shown below. Currently, it has an extra linebreak after the byte count, and it also still prints a space in " KMGT"
, but that space is necessary for the way that particular awk
command displays the right denomination of bytes.
This command assumes you are using the gawk
variant, as it uses the gawk
-specific function gensub
.
echo $disk_space_in_bytes | awk '{ suffix=" KMGT"; for(i=1; $1>1024 && i < length(suffix); i++) $1/=1024; printf("%.2f", $1); print(gensub(/ /, "", "g", substr(suffix, i, 1))"B")}'
Shown in pieces to make it make more sense:
echo $disk_space_in_bytes |
awk '{
suffix=" KMGT";
for(i=1; $1>1024 && i < length(suffix); i++)
$1/=1024;
printf("%.2f", $1);
print(gensub(/ /, "", "g", substr(suffix, i, 1))"B")
}'
Basically, there are just two changes here:
- Line break (
\n
) removed from theprintf
command - The
gensub
command as used here strips whitespace off of the suffix that gets appended to the byte denomination, which is a space if it is bytes, and not kilobytes or so on.
Extra space in awk output
This might work for you:
awk -F'^' '{print $1":"$2}' SERVER_2012-02-29-12-15-00
3969:1272
3969:1272
3969:1272
To concatenate the fields with a :
remove the ,
's.
Or change the output field separator OFS
to null.
awk -F'^' -vOFS='' '{print $1",:,"$2}' SERVER_2012-02-29-12-15-00
3969:1272
3969:1272
3969:1272
Using awk to print all columns from the nth to the last
Print all columns:
awk '{print $0}' somefile
Print all but the first column:
awk '{$1=""; print $0}' somefile
Print all but the first two columns:
awk '{$1=$2=""; print $0}' somefile
How to preserve the original whitespace between fields in awk?
I know this is an old question but I thought there had to be something better. This answer is for those that stumbled onto this question while searching. While looking around on the web, I have to say @Håkon Hægland has the best answer and that is what I used at first.
But here is my solution. Use FPAT. It can set a regular expression to say what a field should be.
FPAT = "([[:space:]]*[[:alnum:][:punct:][:digit:]]+)";
In this case, I am saying the field should start with zero or more blank characters and ends with basically any other character except blank characters. Here is a link if you are having trouble understanding POSIX
bracket expressions.Also, change the output field to OFS = "";
separator because once the line has been manipulated, the output will add an extra blank space as a separator if you don't change OFS from its default.
I used the same example to test.
$ cat example-output.txt
-rw-r--r-- 1 jack jack 8 Jun 19 2013 qunit-1.11.0.css
-rw-r--r-- 1 jack jack 56908 Jun 19 2013 qunit-1.11.0.js
-rw-r--r-- 1 jack jack 4306 Dec 29 09:16 test1.html
-rw-r--r-- 1 jack jack 5476 Dec 7 08:09 test1.js
$ awk 'BEGIN { FPAT = "([[:space:]]*[[:alnum:][:punct:][:digit:]]+)"; OFS = ""; } { $6 = substr( $6, 1, 2); print $0; }' example-output.txt
-rw-r--r-- 1 jack jack 8 J 19 2013 qunit-1.11.0.css
-rw-r--r-- 1 jack jack 56908 J 19 2013 qunit-1.11.0.js
-rw-r--r-- 1 jack jack 4306 D 29 09:16 test1.html
-rw-r--r-- 1 jack jack 5476 D 7 08:09 test1.js
Keep in mind. The fields now have leading spaces. So if the field needs to be replaced by something else, you can do
len = length($1);
$1 = sprintf("%"(len)"s", "-42-");
$ awk 'BEGIN { FPAT = "([[:space:]]*[[:alnum:][:punct:][:digit:]]+)"; OFS = ""; } { if(NR==1){ len = length($1); $1 = sprintf("%"(len)"s", "-42-"); } print $0; }' example-output.txt
-42- 1 jack jack 8 Jun 19 2013 qunit-1.11.0.css
-rw-r--r-- 1 jack jack 56908 Jun 19 2013 qunit-1.11.0.js
-rw-r--r-- 1 jack jack 4306 Dec 29 09:16 test1.html
-rw-r--r-- 1 jack jack 5476 Dec 7 08:09 test1.js
How to remove awk space while printing with values
$ sed 's|[^.]*\.||' test.csv
b.c.d
b
b.c
b.c
b
[^.]
means anything but a .
character. \.
is the .
character (needs to be escaped because it has a special meaning in regexes).
White space in awk print and match
This should do the trick:
awk 'NR==2 {gsub(/;/,\"\"\\);print (match($3,/^ch/\\) ? \"\ \":\"\"\\),$2,$4}'
You need to use a comma to separate the output with the OFS
(output field separator) in awk
which by default is a single space. Without the comma you're doing string concatenation.
Related Topics
How to Prevent Remote Branch Deletion in Git Without Using Gitolite
Linux Support 802.1Ag and Y1731
Linux Kernel Code in Memory Check with Sha256 Sum
Echo - Syntax Error: Bad Substitution
Why Does Utf-8 Text Sort in Different Order Between Os X and Linux
Why Strace Shows Eagain (Resource Temporarily Unavailable)
Ffmpeg Img to Video = Could Find No File with Path
How Is an Error Reported from Async Socket Connect
Process Niceness (Priority) Setting Has No Effect on Linux
Some Questions About "-Set-Xmark" in Iptables
What Means "Atomic" System Call
Why 'Read' Command in Shell Script Is Missing Initial Characters
Catch Signal in Bash But Don't Finish Currently Running Command
.Net Core 3.1 Deploy on Centos 7
How to Add a Directory to The Perl Library Path at The System Level
Generate Disk Usage Graphs/Charts with Cli Only Tools in Linux