aspose file tools*
The moose likes Linux / UNIX and the fly likes Need to remove the line which starts with empty Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "Need to remove the line which starts with empty" Watch "Need to remove the line which starts with empty" New topic
Author

Need to remove the line which starts with empty

Krish Yeruva
Ranch Hand

Joined: Sep 17, 2008
Posts: 58
Hi Ranchers,
I have one file which is having the 20K+ lines text like this:

If we see the lines #3, 5 and 7.
These line are starting with empty spaces. So here my requirement is, I need to remove those entire lines from the file using UNIX script.

So anybody can help me out on this.

Thanks in advance.





[EmptyStaringLines.PNG]



Thanks & Regards
KITTU
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10519
    
    9

I do not know scripting but a little googling indicates the sed is your friend.
What have you tried so far and where are you stuck?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Krish Yeruva
Ranch Hand

Joined: Sep 17, 2008
Posts: 58
Yes We can remove with Sed if the line is having the empty spaces only.
But the thing here is those lines are starting with spaces along with string. So i got hang in this situation.

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42594
    
  65
sed can do that as well, as it uses regular expressions to determine which lines to match.


Ping & DNS - my free Android networking tools app
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11475
    
  16

sed, perl, heck...you could even do it with plain old grep.

If it needs to be in a formal unix script, I think it would be two lines - one for the grep and re-direct to a temp file, and one that renames the temp file to the original file name, assuming that is what's needed.

What have you tried?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
S. Nitesh
Ranch Hand

Joined: Jan 15, 2010
Posts: 36
    
    1

If your file has the fixed width columns and the second field always starts with letter-U then following can help you. Just put the same number of spaces before letter-U as the length of first field of your file.

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16228
    
  21



I put in an arbitrary number of spaces (about 8) for the pattern match at the beginning of the line, but if it's every line that has even one leading space, this works:


Although the shell and grep may get into fights about who owns the backslash.

An alternative for some regex matchers is:



Also there are variants in case the "white space" is actually a tab character or something like that.


Customer surveys are for companies who didn't pay proper attention to begin with.
Krish Yeruva
Ranch Hand

Joined: Sep 17, 2008
Posts: 58
Hi All,
Thanks to all for your response.

Hi fred,
I am trying to do with SED and regular expression: ^ \{9\}[a-zA-Z0-9]
Which means if the line is having 9 spaces in starting and that spaces followed by a-z, A-Z and 0-9 then remove those lines.
Is my process is correct. Can any one suggest me for the better approach if you are aware?

Thanks in advance
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11475
    
  16

sorry - i missed you had written back to this thread.

I don't know SED very well at all. I would still use grep, but that's just me.

I can't say if your regex is right or not. You originally stated:
These line are starting with empty spaces. So here my requirement is, I need to remove those entire lines from the file using UNIX script.

Your regex will not find lines that start with four spaces, or eleven, or just one.

It will also not find lines that are only blank. It won't find lines like this:
" #ignore this line"
[edit - can't get it to show the leading nine spaces right now...putting in quote to try to force at least one.]

I can't say if your process is right or not, since your specs aren't clear to me.
Arun Peter
Greenhorn

Joined: Jan 13, 2014
Posts: 3
If all you want to do is to remove those lines which start with space, use this:
grep -v "^ .*" test.txt > results.txt
Note the space b/w ^ and .
If you need to do the same in the same file, then use this:
sed -i '/^ .*/d' test.txt
Krish Yeruva
Ranch Hand

Joined: Sep 17, 2008
Posts: 58
Hi Arun,
Thanks for your response. And your solution is generic to all issues like doesn't matter how many spaces in starting of the line.

sed -i '/^ .*/d' test.txt

-i means iterate through the all lines in the file
/^ means starting of the lines
test.txt is the destination file.

Correct me if I am wrong and may I know the meaning for the rest of the command? Because I am new to the unix commands.

Thanks in advance





Arun Peter
Greenhorn

Joined: Jan 13, 2014
Posts: 3
Yes, it doesnt matter how many spaces are there. It checks if the first character is a space and is followed by some stuff.

-i means in place. The results replace the contents of the file. If you want to redirect the results to another file, you may use the grep or sed without -i
^ means starting of a line. / is a delimiter mandatory for sed to recognize what is to be deleted.
test.txt is the source file. If -i is used it will be the destination file as well.

. represents a single character. * matches the preceeding character. I guess you can remove the .* too. It may not be required. Just try out.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Need to remove the line which starts with empty