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
Match All Files Under All Nested Directories with Shell Globbing
Nasm Linux Assembly Printing Integers
Configure and Build Opencv to Custom Ffmpeg Install
How to Update Cudnn to a Newer Version
Determine Usb Device File Path
Cannot --Enable-Pcregrep-Libbz2 Because Bzlib.H Was Not Found
/Tmp VS. /Dev/Shm for Temp File Storage on Linux
How to Do Versioning of Shared Library
Using --Http2.0 Option with Curl 7.33.0 Gives Unsupported Protocol
Curl Progress Bar: How to Pipe and Extract Numbers Only Using Grep
Can't Run Uwsgi as Root, "Bind(): Permission Denied"
Packet Sniffing Using Raw Sockets in Linux in C
Using a User's .Bashrc in a Systemd Service
Need a Simple Linux C++ Ide (Android Ndk)
Start Rails Server Automatically When Ever I Start My Ubuntu MAChine
Command for Finding Process Using Too Much Cpu
I Want to Use "Awk" or Sed to Print All the Lines That Start with "Comm=" in a File