How to remove empty line.?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to remove empty line.?
# 1  
Old 06-17-2016
How to remove empty line.?

Hi gurus,

I have a script which works fine.

How to pass string into sql query?


Code:
while read p
do
        [ -z "$para" ] && para="'${p}'" || para="${para},'${p}'"
done < file

Moderator's Comments:
Mod Comment Please use code tags as required by forum rules!


a few days ago, there was issue pop up. in the file, there is empty line at end of the file which causes the list became 'a', 'b', 'c', ''.
I know I can remove the line with sed or other command. but just wondering if it is possible to add condition if p is '' then don't concanated.

thanks in advance.

Last edited by RudiC; 06-18-2016 at 05:28 AM.. Reason: Added code tags.
# 2  
Old 06-17-2016
Skip to the next loop cycle if $p is empty
Code:
while read p
do
  [ -z "$p" ] && continue
  para="${para:+$para,}'$p'"
done < file

The para= is a simple variant of your complex expression and works in all Posix compatible shells.

Last edited by MadeInGermany; 06-18-2016 at 12:18 PM.. Reason: Added missing $para
# 3  
Old 06-18-2016
Another variation
Code:
while read p
do
  [ -n "$p" ] && para="$para,'$p'"
done < file
para=${para#,}


---
Small correction to MadeInGermany's suggestion:
Code:
para="${para:+"${para}",}'$p'"


Last edited by Scrutinizer; 06-18-2016 at 10:58 AM..
These 2 Users Gave Thanks to Scrutinizer For This Post:
# 4  
Old 06-18-2016
Thanks, I have updated my code in post#2.

Last edited by MadeInGermany; 06-19-2016 at 05:19 AM..
This User Gave Thanks to MadeInGermany For This Post:
# 5  
Old 06-21-2016
Quote:
Originally Posted by Scrutinizer
Another variation
Code:
while read p
do
  [ -n "$p" ] && para="$para,'$p'"
done < file
para=${para#,}


---
Small correction to MadeInGermany's suggestion:
Code:
para="${para:+"${para}",}'$p'"

Thanks Scrutinizer,
the code works. one more question raised while running the scrpt.
when running code, using "$p". it works.
Code:
while read p
do
[ -n "$p" ] && para="$para,'$p'"
done < file
para=${para#,}

when running code
Code:
 [ -n $p ],

it doesn't work. the blank line is still there.

what's different between
Code:
[ -n $p ]

and
Code:
[ -n "$p" ]

?

Last edited by Scrutinizer; 06-21-2016 at 03:30 PM..
# 6  
Old 06-21-2016
You are welcome. If you try:
Code:
( set -x; [ -n $p ] && [ -n "$p" ])
+ '[' -n ']'
+ '[' -n '' ']'

You'll notice that the first one evaluates to -n and the second one to -n ''

In the first case the there is no protection of variable p, which contains an empty field. Since there is not protective quotes around $p, the result is field split and since $p is empty this results in 0 fields. So wat remains "-n" which is a test if the string "-n" is non empty
([ "$somestring" ] if the same as [ -n "$somestring"], I prefer the latter for clarity, so in fact [ -n ] is equivalent to [ -n -n ])


In the second case the result of "$p" is not field split and so the -n is interpreted as an operator with one field as parameter, which contains an empty variable, and this evaluates to false .

So that is yet another reason to put quotes around variable expansions.

I hope this clarifies things...

Last edited by Scrutinizer; 06-21-2016 at 03:55 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 06-21-2016
-n tests the following token for non-empty.
But the [ ] is a test command, and does not see the empty value of $p at all, but it sees a "" as an empty token.
The result for -n without a following token can be an error or the -n itself is seen as a token (and results in a true).
So far the [ ] test command.
For the [[ ]] compound the parser is different:
the $p is seen as a token regardless if its value is empty or not.
[[ -n $p ]] should work as you expect.
This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Using awk to remove duplicate line if field is empty

Hi all, I've got a file that has 12 fields. I've merged 2 files and there will be some duplicates in the following: FILE: 1. ABC, 12345, TEST1, BILLING, GV, 20/10/2012, C, 8, 100, AA, TT, 100 2. ABC, 12345, TEST1, BILLING, GV, 20/10/2012, C, 8, 100, AA, TT, (EMPTY) 3. CDC, 54321, TEST3,... (4 Replies)
Discussion started by: tugar
4 Replies

2. Shell Programming and Scripting

Need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line...

Hello, I need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line... An example of entries in the file would be: SRVXPAPI001 ERRO JUN24 07:28:34 1775 REASON= 0000, PROCID= #E506 #1065: TPCIPPR, INDEX= 003F ... (8 Replies)
Discussion started by: Ferocci
8 Replies

3. Shell Programming and Scripting

Remove CR only on empty lines

Dear community, I have two output files that contains some CR # cat first.out 1234567890 598679857648566 9 1234567234 365837465873465 4 2342343243 289374982374894 4 # cat second.out 2342342342 ... (2 Replies)
Discussion started by: Lord Spectre
2 Replies

4. Shell Programming and Scripting

Remove empty records

Hello: Is there a simple way to remove empty records of FASTA format file? A FASTA format consists of two parts: header and sequence (for non-biologist, Wiki for details of course!). The header always start with ">" for the name of the sequence. The header must be in this ONLY single line.... (9 Replies)
Discussion started by: yifangt
9 Replies

5. UNIX for Dummies Questions & Answers

remove empty field

Hi all ! I'm sure it is a basic question but I didn't find any threads that fit my need. How to remove empty fields with awk? Or in other words, how to shift all the fields after an empty field on the left? input: 1|2||3|4|5||6 wanted: 1|2|3|4|5|6 I tried: awk '{for(i=1; i<=NF;... (7 Replies)
Discussion started by: lucasvs
7 Replies

6. Shell Programming and Scripting

Remove empty line and the next one

Hi all, I'm trying to remove when this condition is met: an empty and the next one I'm using this command: sed '/^$/N; s/&//' file Which searches for an empty line, N attaches it to the next line, and substituing the combination with nothing.... but it is not working. What I'm missing... (1 Reply)
Discussion started by: meuser
1 Replies

7. Shell Programming and Scripting

Fill the empty line by adding line before blank line

FIle A "A" 2 aa 34 3 ac 5 cd "B" 3 hu 67 4 fg 5 gy output shud be A"" 2 aa 34 "A" 3 ac 34 "A" 5 cd 34 "B" 3 hu 67 "B" 4 fg 67 "B" 5 gy 67 (6 Replies)
Discussion started by: cdfd123
6 Replies

8. Shell Programming and Scripting

using vi -c to remove empty lines

Hello: I searched here for "vi -c" but found no hits. How can I use vi -c to remove ALL empty lines, regardless of how many? I tried <code> vi -c ":g/^$/d | wq" filename </code> but I have to run it several times. This is NOT homework. :) Thanks for your time. (3 Replies)
Discussion started by: Habitual
3 Replies

9. UNIX for Dummies Questions & Answers

Remove only Empty Directories

I know this one was answered before in forum below - https://www.unix.com/unix-dummies-questions-answers/58210-removing-empty-folders-using-find-command.html But that one is closed & I have a question so here it goes. I want to delete all 2006 files. Now if along with the files, if the... (2 Replies)
Discussion started by: kedar.mehta
2 Replies

10. Shell Programming and Scripting

remove empty directory

Hi, I need to delete an empty directory in a temp directory except "dir5" (keep everything that is not empty). Plese advise. Here is an example of my directory. /dir/temp/ dir1 - delete if this is empty dir2 - delete if this is empty dir3 - delete if this is empty dir4 - delete if this... (7 Replies)
Discussion started by: sirrtuan
7 Replies
Login or Register to Ask a Question