remove blank lines and merge lines in shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting remove blank lines and merge lines in shell
# 8  
Old 03-08-2011
Quote:
Originally Posted by Corona688
Good to know. How about awk 'BEGIN { RS="" ; FS="\n" } { for(N=1; N<=NF; N++) printf("%s,", $N); printf("\n"); }' < csvrec.csv then?
This works perfect. Thanks a lotSmilie

---------- Post updated at 12:46 AM ---------- Previous update was at 12:02 AM ----------

Quote:
Originally Posted by Corona688
Good to know. How about awk 'BEGIN { RS="" ; FS="\n" } { for(N=1; N<=NF; N++) printf("%s,", $N); printf("\n"); }' < csvrec.csv then?
I'm facing a error for few files.

Here is the error that i got -

awk: Input line xxxxx cannot be longer than 3,000 bytes.
The source line number is xxxxx.

Do u have any idea wat the problem here wud be?

Thanks!
This User Gave Thanks to dvah For This Post:
# 9  
Old 03-08-2011
Please don't bump this up with a new thread if we don't respond fast enough.

The error means exactly what it says: the input data is too long. Some implementations of awk have this annoying limit. If your system has nawk or gawk that should work better.
# 10  
Old 03-08-2011
explanation

Quote:
Originally Posted by dvah
This really works good, but i cudnt exactly understand the logic behind it. I'm aware NF is for no. of fields in each line and \n is for newline.

But i cudnt understand the whole logic, if u cud explain me, that be really helpful for me to make changes to this in future.

Grateful to your comments.

Many Thanks!

---------- Post updated at 11:36 PM ---------- Previous update was at 11:31 PM ----------

Thanks for all ur comments. It works weel and good.

....
printf print without a newline
NF?$0:"\n" a conditional expression.
test_condition ? true_result : false_result
NF return 0 for blank lines. Zero is considered false.
$0 returns the whole line/record being considered
"\n" adds a newline to the output when NF is zero

---------- Post updated at 05:53 PM ---------- Previous update was at 04:46 PM ----------

I wrote my own long winded versions of the concise:
Code:
awk '{printf NF?$0:"\n"}'

This one is just a verbose version of the above version:
Code:
awk 'NF > 0{printf $0};NF == 0{printf "\n"}'

This one actually breaks the fields down properly in case you need to process them but outputs field and record separators manually:
Code:
awk 'BEGIN{RS="";FS=",[\n]*";OFS="";ORS=""}{for(i=1;i<NF;i++) print $i ",";print $NF "\n"}'

# 11  
Old 03-09-2011
Bug

Quote:
Originally Posted by Corona688
Please don't bump this up with a new thread if we don't respond fast enough.

The error means exactly what it says: the input data is too long. Some implementations of awk have this annoying limit. If your system has nawk or gawk that should work better.
My system doesn't seem to support gawk. I tried the same command with nawk, and i got the same error. Is there any other way to handle this situation. Please help me on this. If im thru with this, my whole script would be ready to deploy.

Thanks!Smilie

---------- Post updated at 04:25 PM ---------- Previous update was at 03:21 PM ----------

Quote:
Originally Posted by freegnu
printf print without a newline
NF?$0:"\n" a conditional expression.
test_condition ? true_result : false_result
NF return 0 for blank lines. Zero is considered false.
$0 returns the whole line/record being considered
"\n" adds a newline to the output when NF is zero

---------- Post updated at 05:53 PM ---------- Previous update was at 04:46 PM ----------

I wrote my own long winded versions of the concise:
Code:
awk '{printf NF?$0:"\n"}'

This one is just a verbose version of the above version:
Code:
awk 'NF > 0{printf $0};NF == 0{printf "\n"}'

This one actually breaks the fields down properly in case you need to process them but outputs field and record separators manually:
Code:
awk 'BEGIN{RS="";FS=",[\n]*";OFS="";ORS=""}{for(i=1;i<NF;i++) print $i ",";print $NF "\n"}'

I'm facing a error with few files.

awk: There are not enough parameters in printf statement


I tried to print the whole file(without doing any tailoring) with this statement - awk '{printf $0}' < xxx I got the same error, and i found the error reported line has a character '%' in it.

So, i guess printf considers the % symbol in file as a special character. But im not sure this wud be the problem.

Thanks

Last edited by dvah; 03-09-2011 at 07:19 AM..
# 12  
Old 03-09-2011
The correct way to print an arbitrary string with printf is printf "%s", $0
This User Gave Thanks to alister For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove Blank lines in VI

Hi, Which option is used to remove blank lines in VI (AIX). ? Regards, Siva (6 Replies)
Discussion started by: ksgnathan
6 Replies

2. UNIX and Linux Applications

remove all blank lines

When I 'vi' my test file I see some blank lines. However once I do :set list to display hidden characters, I see the empty lines literally like this: ^I$ How do I remove them? I cannot find a regex to match them. (3 Replies)
Discussion started by: alexsuv
3 Replies

3. Shell Programming and Scripting

How to remove blank lines

Hi, I am facing a problem related to removing blank lines from a text document. Input Error 17-05-2011 11:01:15 VisualSVN Server 2.1 1001 The following information was included with the event: line3 line4 Error 17-05-2011 11:00:25 VisualSVN Server 2.1 ... (13 Replies)
Discussion started by: mayursingru
13 Replies

4. Shell Programming and Scripting

Delete blank lines, if blank lines are more than one using shell

Hi, Consider a file named "testfile" The contents of file are as below first line added for test second line added for test third line added for test fourth line added for test fifth line added for test (5 Replies)
Discussion started by: anil8103
5 Replies

5. Shell Programming and Scripting

Remove blank lines

I really hope someone can help me with this. I have several php files from a forum that I run, that now for some reason have blank lines after every line. Is there an easy way to make a script that does the following: * If there are consecutive blank lines, delete all of them except one. * If... (9 Replies)
Discussion started by: KidCactus
9 Replies

6. Shell Programming and Scripting

remove blank lines

I have joined 2 files. Join command worked fine. but the result showing extra blank lines. I tried to remove blank spaces by using awk (-- -42 RS= ORS="\n\n" file.txt) and sed (sed '/^ *$/d' file.txt)commands but didn't remove any Any suggestions plz:D 123 tab ....... ......tab .......234... (3 Replies)
Discussion started by: repinementer
3 Replies

7. Shell Programming and Scripting

Shell script: Remove blank lines

Hi gurus, I have this file with blank lines in it. How do i remove them in shell? I tried these commands but not working: sed '/^ *$/d' or sed '/^$/d' Anybody has a better idea pls? Also there are lines which starts with a single space, how do we remove the space in those lines?... (3 Replies)
Discussion started by: gholdbhurg
3 Replies

8. Shell Programming and Scripting

Remove all blank lines in shell or awk.

Hi there, I want to trim space between lines in unix. I have a file named abc.txt with 2,00,000 lines.But useful are only a few. Please tell me how to delete the blank lines.:o (1 Reply)
Discussion started by: tushar_tus
1 Replies

9. UNIX for Dummies Questions & Answers

How to count lines - ignoring blank lines and commented lines

What is the command to count lines in a files, but ignore blank lines and commented lines? I have a file with 4 sections in it, and I want each section to be counted, not including the blank lines and comments... and then totalled at the end. Here is an example of what I would like my... (6 Replies)
Discussion started by: kthatch
6 Replies

10. UNIX for Dummies Questions & Answers

Remove blank lines

¿How can i remove blank lines between all lines in a long text file? Example WrongFile.txt : Line 1 Line 2 Line 3 CorrectFile.txt : Line 1 Line 2 Line 3 Thanks in advance :confused: (4 Replies)
Discussion started by: osymad
4 Replies
Login or Register to Ask a Question