Remove CR only on empty lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove CR only on empty lines
# 1  
Old 09-12-2012
Remove CR only on empty lines

Dear community,
I have two output files that contains some CR

Code:
# cat first.out 

1234567890           598679857648566               9
1234567234           365837465873465               4

2342343243           289374982374894               4

Code:
# cat second.out 

2342342342           567456456456546      462342342423443               4
4564645665           768768678678768      678678678678678              14

2342343244           353453453453453      456456456456456               5
3435345345           435345345345354      564564564564565               6

7534242344           879789879899087      234261012537152               4
3453453453           435345435345355      345345345353455               5

Is there a fast way to remove the CR only on empty lines?
So the results should be (as example for the second output):

Code:
# cat second.out 
2342342342           567456456456546      462342342423443               4
4564645665           768768678678768      678678678678678              14
2342343244           353453453453453      456456456456456               5
3435345345           435345345345354      564564564564565               6
7534242344           879789879899087      234261012537152               4
3453453453           435345435345355      345345345353455               5

The lines starts with numbers, so awk should be the sulution, but I cannot figured out how remove the CR ONLY on empty lines....

Thanks
Lucas
# 2  
Old 09-12-2012
You seem to want to remove empty lines. Really empty lines contain nothing, not even white space, so the following will do the trick:

Code:
sed '/^$/d' /path/to/infile > /path/to/outfile

In case your lines are not that empty - that is, they may contain white space (space or tab) but nothing else (replace "<spc>" and "<tab>" by literal tabs/spaces):

Code:
sed '/^[<spc><tab>]*$/d' /path/to/infile > /path/to/outfile

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 3  
Old 09-12-2012
Thanks, the first command works great! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove empty line.?

Hi gurus, I have a script which works fine. https://www.unix.com/shell-programming-and-scripting/239347-how-pass-string-into-sql-query.html while read p do && para="'${p}'" || para="${para},'${p}'" done < filePlease use code tags as required by forum rules! a few days... (6 Replies)
Discussion started by: ken6503
6 Replies

2. Shell Programming and Scripting

Joining broken lines and removing empty lines

Hi - I have req to join broken lines and remove empty lines but should NOT be in one line. It has to be as is line by line. The challenge here is there is no end of line/start of line char. thanks in advance Source:- 2003-04-34024|04-10-2003|Claims|Claim|01-13-2003|Air Bag:Driver;... (7 Replies)
Discussion started by: Jackceasar123
7 Replies

3. 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

4. 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

5. 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

6. Shell Programming and Scripting

What's the command to remove empty lines with sed?

3 10 20 10 100 100 10000 Output: 3 10 20 10 100 100 10000 ---------- Post updated at 07:59 AM ---------- Previous update was at 07:56 AM ---------- sed '/^$/d' file doesn't work. (8 Replies)
Discussion started by: cola
8 Replies

7. 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

8. Shell Programming and Scripting

perl or awk remove empty lines when condition

Hi Everyone, # cat 1 a b b cc 1 2 3 3 3 4 55 5 a b (2 Replies)
Discussion started by: jimmy_y
2 Replies

9. Shell Programming and Scripting

Using sed to remove lines where field is empty

I was just looking at this post: https://www.unix.com/shell-programming-scripting/22893-delete-multiple-empty-lines.html. and I am looking to achieve the same with sed. So the idea is to delete lines from a file where a certain field has no value. Inputfile: EMID MMDDYY HOURS JOB EMNAME 0241... (4 Replies)
Discussion started by: figaro
4 Replies

10. Shell Programming and Scripting

Removing empty lines(space) between two lines containing strings

Hi, Please provide shell script to Remove empty lines(space) between two lines containing strings in a file. Input File : A1/EXT "BAP_BSC6/07B/00" 844 090602 1605 RXOCF-465 PDTR11 1 SITE ON BATTERY A2/EXT... (3 Replies)
Discussion started by: sudhakaryadav
3 Replies
Login or Register to Ask a Question