Remove Space and blank line from file in UNIX shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove Space and blank line from file in UNIX shell script
# 1  
Old 09-19-2013
Remove Space and blank line from file in UNIX shell script

I have below file. I want to remove space at begining of every line and then after also remove blank line from file.
I use below code for each operation.

sed -e 's/^[ \t]*//' < check.txt > check1.txt
sed '/^\s*$/d' < check1.txt > check2.txt






above code not remove all the space and blank line. any one can help in this.

Last edited by Mohin Jain; 09-19-2013 at 06:50 AM..
# 2  
Old 09-19-2013
try

Code:
sed 's/^[ \t]\+//;/^$/d' file

And Please use code tags for code and data sample.
# 3  
Old 09-19-2013
Using awk
Code:
cat check.txt
CREATE TABLE $SC.TMP_TBL --- first step (
 ALTER TABLE $SC.TMP_TBL LOCKSIZE TABLE APPEND ON
ERROR: While creating temp table TMP_TBL!
ERROR: While loading records into table Z_KD!
Loading of table Z_KD is successfull!
CREATE TABLE $SC.TMP_TBL AS
 ALTER TABLE $SC.TMP_TBL LOCKSIZE TABLE APPEND ON
ERROR: While creating temp table TMP_TBL!
ule3-Trial 3 to update RECHTSFORM
MERGE INTO $SC.Z_KD a USING (

ERROR: While loading records into table Z_KD(Rule3-Trial3)!
CREATE TABLE $SC.TMP_TBL
 ALTER TABLE $SC.TMP_TBL


Code:
awk 'NF {$1=$1;print}' check.txt
CREATE TABLE $SC.TMP_TBL --- first step (
ALTER TABLE $SC.TMP_TBL LOCKSIZE TABLE APPEND ON
ERROR: While creating temp table TMP_TBL!
ERROR: While loading records into table Z_KD!
Loading of table Z_KD is successfull!
CREATE TABLE $SC.TMP_TBL AS
ALTER TABLE $SC.TMP_TBL LOCKSIZE TABLE APPEND ON
ERROR: While creating temp table TMP_TBL!
ule3-Trial 3 to update RECHTSFORM
MERGE INTO $SC.Z_KD a USING (
ERROR: While loading records into table Z_KD(Rule3-Trial3)!
CREATE TABLE $SC.TMP_TBL
ALTER TABLE $SC.TMP_TBL

# 4  
Old 09-19-2013
Code:
awk '$1=$1' infile

--ahamed
# 5  
Old 09-19-2013
If line starts with 0 or 0.0 this will not work, line will be removed.
# 6  
Old 09-19-2013
Quote:
Originally Posted by Jotne
If line starts with 0 or 0.0 this will not work, line will be removed.
That's right Jotne.
The OP's data doesn't seem to have it. Smilie

--ahamed
# 7  
Old 09-19-2013
Still code is not working on my pc.
i think its not a space its a tab.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

grep to remove blank space

Hi All, Need help to grep blank and copy to file. I have a file in below format dns1dm06_10, dns2dm02_04, dbidub,10000000c9a46d0c gbpuhci,10000000c948b00a ibtur001,10000000c9a1ccda yubkbtp1,10000000c93fec5b I need to copy to all lines which doesn't have wwn >> no-wwn.txt 1... (2 Replies)
Discussion started by: ranjancom2000
2 Replies

2. Shell Programming and Scripting

How to remove blank line from a text file?

Hi All, I am creating a text file using perl. The first record I am writing as "$line" and all the other as "\n$line". At the end the file is having N number of lines. I am using this file for MLOAD (Teradata), which is reading N+1 lines in the file and failing.I am not able to find new line... (2 Replies)
Discussion started by: unankix
2 Replies

3. Shell Programming and Scripting

Remove line breaks in csv file using shell script

Hi All, I've a csv file in which the record is getting break into 1 line or more than one line. I want to combine those splits into one line and remove the unwanted character existing in the record i.e. double quote symbol ("). The line gets break only when the record contains double... (4 Replies)
Discussion started by: rajak.net
4 Replies

4. Shell Programming and Scripting

How to remove particular line from file through shell script?

Hi, I am pasring a file line by line. I need to check each field in line and remove particular line. input file lines are, 02;ABC;PQR 03;aaa;rrr 04;ABC;ggg 09;eee;ABC 04;lmn;stu I am looking for line containing "ABC" as field value. Now How can I remove this line from input file... (7 Replies)
Discussion started by: Poonamol
7 Replies

5. Shell Programming and Scripting

Blank Space is not appending in each row of CSV File - Shell Script

I am calling SQL script in my UNIX Shell script and trying to create the CSV file and my last column value of each row is 23 blank spaces. In my SQL script,the last column is like below. RPAD(' ',23,' ') -- Padding 23 blank Spaces The CSV file is generated but the sapce(23 spaces) is... (2 Replies)
Discussion started by: praka
2 Replies

6. Shell Programming and Scripting

Remove last blank line of file

I have a number of files (arranged in directories) which have last line blank, I am trying to synchronize my code with other env and due to this blank lines, all files error out as different although only difference is that of balnk line at end of file. Is there a way I can recursively... (2 Replies)
Discussion started by: ruchimca
2 Replies

7. Shell Programming and Scripting

shell script to remove all lines from a file before a line starting with pattern

hi,, i hav a file with many lines.i need to remove all lines before a line begginning with a specific pattern from the file because these lines are not required. Can u help me out with either a perl script or shell script example:- if file initially contains lines: a b c d .1.2 d e f... (2 Replies)
Discussion started by: raksha.s
2 Replies

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

9. UNIX for Dummies Questions & Answers

Want display a line with space in file by unix script

HI , I am having a file as -----------------a.out------------------- Hi I am unix developer using hp unix this is a test --------------------------------------- i need to read each line by a unix script of the file and to print in console with the space in the line as ... (9 Replies)
Discussion started by: arunkumar_mca
9 Replies

10. UNIX for Dummies Questions & Answers

To remove Continous blank spaces from a file in UNIX

All... I want to remove blank spaces in file . I just leraned that we can use " cat <Input filename> | tr -s > <Target file name> " i also know with SED we can replace a blank space by other character by sed s/ /*/g filename. Please let me know how can i do that by... (1 Reply)
Discussion started by: arunkumar_mca
1 Replies
Login or Register to Ask a Question