• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need to remove the line which starts with empty

 
Krish Yeruva
Ranch Hand
Posts: 58
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
[Thumbnail for EmptyStaringLines.PNG]
 
Maneesh Godbole
Saloon Keeper
Posts: 10971
11
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Krish Yeruva
Ranch Hand
Posts: 58
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sed can do that as well, as it uses regular expressions to determine which lines to match.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12015
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
S. Nitesh
Ranch Hand
Posts: 36
1
Chrome Linux VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 17616
39
Android Eclipse IDE Linux
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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.
 
Krish Yeruva
Ranch Hand
Posts: 58
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12015
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic