The /./
is removing blank lines for the first condition { print "a"$0 }
only, how would I ensure the script removes blank lines for every condition ?
awk -F, '/./ { print "a"$0 } NR!=1 { print "b"$0 } { print "c"$0 } END { print "d"$0 }' MyFile
We can remove blank lines using awk: $ awk NF < myfile.
The d command in sed can be used to delete the empty lines in a file. Here the ^ specifies the start of the line and $ specifies the end of the line. You can redirect the output of above command and write it into a new file.
A shorter form of the already proposed answer could be the following:
awk NF file
Any awk
script follows the syntax condition {statement}
. If the statement block is not present, awk
will print the whole record (line) in case the condition
is not zero.
NF
variable in awk
holds the number of fields in the line. So when the line is non empty, NF
holds a positive value which trigger the default awk
action (print the whole line). In case of empty line, NF
is zero and the condition is not met, so awk
does nothing.
Note that you don't even need quote because this 2 letters awk script doesn't contain any space or character that could be interpreted by the shell.
or
awk '!/^$/' file
^$
is the regex for an empty line. The 2 /
is needed to let awk
understand the string is a regex. !
is the standard negation.
Awk command to remove blank lines from a file:
awk 'NF > 0' filename
if you want to ignore all blank lines, put this at the beginning of the script
/^$/ {next}
Put following conditions inside the first one, and check them with if
statements, like this:
awk -F, '
/./ {
print "a"$0;
if (NR!=1) { print "b"$0 }
print "c"$0
}
END { print "d"$0 }
' MyFile
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With