Deleting Blank Lines After Loop

how to 'for f loop' and remove blank lines

I suspect the root problem is that some of the Info.plist files are in the binary form of property list format, rather than the XML form, so grep is printing a bunch of lines like:

Binary file ./some/path/to/Info.plist matches

... and then awk tries to print the third field (sort of) but there isn't anything relevant there.

In order to solve this, you need to stop using grep and awk, and use a tool that actually understands the property list format (in all its forms), like defaults or PlistBuddy. defaults has some issues with how file paths are specified, so I'll vote for PlistBuddy.

Also, as @chepner and @EdMorton pointed out, the for loop over find's output is not a safe way to handle filenames, especially on macOS where spaces in filenames are common. PlistBuddy would be easy to use directly with find ... -exec, but since you want a blank line after each file it's more complicated. Probably the easiest way to do that is with a while read loop (using null-delimited filenames to avoid trouble with spaces etc):

find . -name 'Info.plist' -print0 |
while IFS= read -r -d '' file; do
/usr/libexec/PlistBuddy -c "print CFBundleIdentifier" -c "print :CFBundleVersion" "$file" 2>/dev/null
echo
done

Note that this will not print the "CFBundleIdentifier" and "CFBundleVersion" lines, just the data in those property list elements. If you want those names, yet another complication because you have to test whether those items are actually present in the plist.

remove blank lines from for loop python

Filter out empty lines:

def compose_contents(self, *lines):
self.contents = '\n'.join([line for line in lines if line.strip()])
return self.contents

This supports an arbitrary number of input lines by using the *args arbitrary positional argument syntax.

How to delete all blank lines in the file with the help of python?

import fileinput
for line in fileinput.FileInput("file",inplace=1):
if line.rstrip():
print line

Removing Extra blank lines Python

It looks like what you meant is this:

line = input("Line: ")
while line:
line = line.replace('s','sss')
line = line.replace('S','Sss')
print(line)
line = input("Line: ")

So the print is inside the loop, and it inputs a new line at the end of your loop, right before the while condition checks if it is empty.

removing blank lines on R

First we could replace "" to NAand then filter:

library(dplyr)

df %>%
mutate(Code.3 = na_if(Code.3, "")) %>%
filter(!is.na(Code.3))

Delete blank/empty lines in text file for Python

If you want to remove blank lines from an existing file without writing to a new one, you can open the same file again in read-write mode (denoted by 'r+') for writing. Truncate the file at the write position after the writing is done:

with open('file.txt') as reader, open('file.txt', 'r+') as writer:
for line in reader:
if line.strip():
writer.write(line)
writer.truncate()

Demo: https://repl.it/@blhsing/KaleidoscopicDarkredPhp

Remove blank lines in div

Trim and filter out empty lines.

// get the pre tagvar pre = document.querySelector('pre');
pre.innerHTML = pre.innerHTML // split by new line .split('\n') // iterate and trim out .map(function(v) { return v.trim() // filter out non-empty string }).filter(function(v) { return v != ''; // join using newline char }).join('\n')
<pre>     this is a
this is b

this is c</pre>

What's a quick one-liner to remove empty lines from a python string?

How about:

text = os.linesep.join([s for s in text.splitlines() if s])

where text is the string with the possible extraneous lines?

How to remove empty lines with or without whitespace in Python

Using regex:

if re.match(r'^\s*$', line):
# line is empty (has only the following: \t\n\r and whitespace)

Using regex + filter():

filtered = filter(lambda x: not re.match(r'^\s*$', x), original)

As seen on codepad.



Related Topics



Leave a reply



Submit