awk for removing spaces


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk for removing spaces
# 1  
Old 05-16-2011
awk for removing spaces

HI,

I have a file with lot of blank lines, how can I remove those using awk??

Thanks,
Shruthi
# 2  
Old 05-16-2011
hey you can use
Code:
awk '/./' infile for removing blank lines

# 3  
Old 05-16-2011
can you please explain me how this command works...I mean I don't have any '.' in my file to search for ??
# 4  
Old 05-16-2011
awk '!/^$/ {print $0}' test.txt
# 5  
Old 05-16-2011
Quote:
Originally Posted by shruthidwh
can you please explain me how this command works...I mean I don't have any '.' in my file to search for ??
In this context the '.' is not a literal dot/period, but a regex special character/metacharacter that matches (almost) any single character ( reference: Regex Tutorial - The Dot Matches (Almost) Any Character )

So, here it matches all lines that contain any character (ie: that aren't blank). Note if lines contain spaces these are counted as characters so will still be printed, eg:
Code:
$ printf 'line1\n \nline3\nline4 with spaces\n\nline6\n'|awk '/./'
line1

line3
line4 with spaces
line6

(line 2 consists of a space, so is still printed. line 5 is a blank line, so it is omitted)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk print string with removing all spaces

Hi all, I want to set 10 set of strings into a variable where: removing all spaces within each string change the delimiter from "|" to "," Currently, I've the below script like this:Table=`ten character strings with spaces in-between and each string with delimiter "|" | tr -d ' ' |... (7 Replies)
Discussion started by: o1283c
7 Replies

2. UNIX for Dummies Questions & Answers

Removing spaces in the second field alone

Consider my input string as "abc|b f g|bj gy" I am expecting the output as "abc|bfg|bj gy". Please let me know how to achieve this in unix? Thanks (8 Replies)
Discussion started by: pandeesh
8 Replies

3. Shell Programming and Scripting

Removing spaces within Filename

Hello, I have a Folder (myfile) which contain the following files: P$12789865KR +N+01+OM+16102009165416.nu P$M1-508962GD +N+01+ALP+14102009094417.nu Is there a sed command(s) that will loop through this folder and remove the spaces that exists in the filename? Any help would be... (7 Replies)
Discussion started by: Fishn
7 Replies

4. Shell Programming and Scripting

Removing spaces in a line

Hi All, I have a line like this " field1;field2;field3 " (single space after and before double quotes). Now i have to remove these single space . Kindly help me. Thanks in advance (2 Replies)
Discussion started by: krishna_gnv
2 Replies

5. Shell Programming and Scripting

Removing blank spaces, tab spaces from file

Hello All, I am trying to remove all tabspaces and all blankspaces from my file using sed & awk, but not getting proper code. Please help me out. My file is like this (<b> means one blank space, <t> means one tab space)- $ cat file NARESH<b><b><b>KUMAR<t><t>PRADHAN... (3 Replies)
Discussion started by: NARESH1302
3 Replies

6. UNIX for Dummies Questions & Answers

Removing spaces...

Hey, I'm using the command from this thread https://www.unix.com/unix-dummies-questions-answers/590-converting-list-into-line.html to convert vertical lines to horzontal lines. But I need to remove the spaces that is created. Unfortunately I can't figure out where the space is in the code.. I... (2 Replies)
Discussion started by: lost
2 Replies

7. Shell Programming and Scripting

removing spaces

hey.. i had a problem with the unix command when i want to remove the white spaces in a string..i guess i cud do it with a sed command but i get an error when i give space in the square brackets.. string="nh hjh llk" p=`echo $string | sed 's/ //g'` i donno how to give space charater and... (2 Replies)
Discussion started by: sahithi_khushi
2 Replies

8. Shell Programming and Scripting

removing spaces after sperator

Hi friends i have problem 6000000001| CDC049| 109| CDC| 02/02/2006| Auto| New Add| 02/03/2006 6000000002| CDC033| 109| CDC| 02/02/2006| Auto| New Add| 02/03/2006 6000000003| CDC037| 109| CDC| 02/02/2006| Auto| New Add| 02/03/2006 6000000004| CDC031| ... (6 Replies)
Discussion started by: vishnu_vaka
6 Replies

9. UNIX for Dummies Questions & Answers

removing spaces from variables?

I stored results like this VAR=`wc -l < ls.txt` But the value of the wc gave me a padded number. How do I strip the padding from $VAR? Do you think I could use SED? Except instead of a file input, have a variable redirection input? (2 Replies)
Discussion started by: yongho
2 Replies

10. Shell Programming and Scripting

removing spaces in a file

I have output a file and need to remove blank spaces in the first lines only so the file is left end justified. I need to do this while keeping the rest of the file intact. Example of file that needs spaces removed: space space space space space space space... (3 Replies)
Discussion started by: tioray
3 Replies
Login or Register to Ask a Question