Awk Remove Blank Lines

AWK remove blank lines

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

How to remove blank lines from a Unix file


sed -i '/^$/d' foo

This tells sed to delete every line matching the regex ^$ i.e. every empty line. The -i flag edits the file in-place, if your sed doesn't support that you can write the output to a temporary file and replace the original:

sed '/^$/d' foo > foo.tmp
mv foo.tmp foo

If you also want to remove lines consisting only of whitespace (not just empty lines) then use:

sed -i '/^[[:space:]]*$/d' foo

Edit: also remove whitespace at the end of lines, because apparently you've decided you need that too:

sed -i '/^[[:space:]]*$/d;s/[[:space:]]*$//' foo

Removing duplicate blank lines with awk


$ awk 'NF{c=1} (c++)<3' file
Sample Line 1

Sample line 2

Sample line 3

or if you don't mind an extra blank line at the end:

$ awk -v RS= -v ORS='\n\n' '1' file
Sample Line 1

Sample line 2

Sample line 3

Remove lines from a file corresponding to blank lines of another file

Could you please try following, written and tested with shown samples in GNU awk.

awk '
BEGIN{
FS=OFS=";"
}
FNR==NR{
arr[FNR]=$0
next
}
!/^;+$/{
print arr[FNR],$0
}
' file_a file_b

Explanation: Adding detailed explanation for above.

awk '                 ##Starting awk program from here.
BEGIN{ ##Starting BEGIN section from here.
FS=OFS=";" ##Setting field separator and output field separator as ; here.
}
FNR==NR{ ##Checking condition if FNR==NR which will be TRUE when file_a is being read.
arr[FNR]=$0 ##Creating arr with index FNR and value is current line.
next ##next will skip all further statements from here.
}
!/^;+$/{ ##Checking condition if line NOT starting from ; till end then do following.
print arr[FNR],$0 ##Printing arr with index of FNR and current line.
}
' file_a file_b ##Mentioning Input_file names here.

Why is awk removing empty lines

Your script isn't removing newlines, it's removing some empty (possibly including lines of all spaces) lines. Given input like this:

$ printf 'foo\n\nbar\n'
foo

bar

this script will remove newlines (not your case):

$ printf 'foo\n\nbar\n' | awk -v ORS= '1'
foobar$

while this script will remove empty lines (your case):

$ printf 'foo\n\nbar\n' | awk 'NF'
foo
bar

Your script will only remove an empty line if there was another empty line before it, e.g. if the first input line was empty, as it only prints lines with unique $1 values across ALL lines, empty or not. To keep all empty lines use:

$ awk -F= '!NF || !a[$1]++' file

"cart.title" = "Cart";
"cart.subtitle" = "Price";

"checkout.title" = "Checkout";

Removing all lines after blank line with sed and awk

You can do this fairly easily with awk

awk '/^$/{exit}1' ./input/file

How to remove blank lines from a Unix file


sed -i '/^$/d' foo

This tells sed to delete every line matching the regex ^$ i.e. every empty line. The -i flag edits the file in-place, if your sed doesn't support that you can write the output to a temporary file and replace the original:

sed '/^$/d' foo > foo.tmp
mv foo.tmp foo

If you also want to remove lines consisting only of whitespace (not just empty lines) then use:

sed -i '/^[[:space:]]*$/d' foo

Edit: also remove whitespace at the end of lines, because apparently you've decided you need that too:

sed -i '/^[[:space:]]*$/d;s/[[:space:]]*$//' foo


Related Topics



Leave a reply



Submit