Removing white space in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removing white space in awk
# 1  
Old 11-13-2012
Removing white space in awk

Hi

How to remove white space from this input:
Code:
|blue |   1|
|green|   4|
|black|   2|

I like to search for green and get
Code:
4

not
Code:
   4

How to modify this to work correct:
Code:
awk -F"|" '/green/ {print $3}

# 2  
Old 11-13-2012
Try this:

Code:
awk -F"|" '/green/ {print $3/1}

These 2 Users Gave Thanks to ripat For This Post:
# 3  
Old 11-13-2012
Nice, never seen before. Where is this documented?
# 4  
Old 11-13-2012
Thanks, this shows the power of awk Smilie
Works fine as long as its number.
# 5  
Old 11-13-2012
Quote:
Originally Posted by zaxxon
Nice, never seen before. Where is this documented?
It's a forced type casting that I occasionally use in spread sheets to force a numeric value. I don't think it is documented anywhere. I just gave it a try in awk and it works.
This User Gave Thanks to ripat For This Post:
# 6  
Old 11-13-2012
Quote:
Originally Posted by Jotne
Works fine as long as its number.
You can do also like this..

Code:
awk -F "|" '{gsub("^[ \t]*","",$3);print $3}' file

and ripat's code can be written also as (for numeric values)

$3+0, $3*1.
# 7  
Old 11-13-2012
building on pamu's awk suggestion:
Code:
awk -F "|" '/green/ {sub("^[ \t]*","",$3);sub("[ \t]*$","",$3);print $3}' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing White spaces from a huge file

I am trying to remove whitespaces from a file containing sample data as: 457 <EOFD> Mar 1 2007 12:00:00:000AM <EOFD> Mar 31 2007 12:00:00:000AM <EOFD> system <EORD> 458 <EOFD> Mar 1 2007 12:00:00:000AM<EOFD>agf <EOFD> Apr 20 2007 9:10:56:036PM <EOFD> prodiws<EORD> . Basically these... (11 Replies)
Discussion started by: amvip
11 Replies

2. Shell Programming and Scripting

Add white space

hi guys how can i add spacein file name with sed if strings have no space around dash input 19-20 ( 18-19 ) ABC-EFG output after add white space 19 - 20 (18 - 19 ) ABC - EFG thx in advance (2 Replies)
Discussion started by: mhs
2 Replies

3. Shell Programming and Scripting

awk - trim white space from a field / variable

Hi, Consider the data (FS = |): 1| England |end 2| New Zealand |end 3|Australia|end 4| Some Made Up Country |end 5| West Indies|end I want the output to be (i.e. without the leading and trailing white space from $2) England New Zealand Australia Some Made Up Country West... (4 Replies)
Discussion started by: Storms
4 Replies

4. Shell Programming and Scripting

Undesired removal of white space with awk

Hi, I'm fairly new to scripting and I have a problem that I am having difficulty solving. What I'd like to do is run an awk script to adjust the string in the first field depending on the string in another field. This is best explained with an example: Here is my script: cat... (4 Replies)
Discussion started by: calbrex
4 Replies

5. Shell Programming and Scripting

AWK - Ignoring White Space with FS

I have an AWK script that uses multiple delimiters in the FS variable. FS="+" My awk script takes a file name such as this: 12345_smith_bubba_12345_20120215_4_0.pdf and parses it out based on the under score. Each parsed field then has some code for data validation etc. This script has... (12 Replies)
Discussion started by: reno4me
12 Replies

6. UNIX for Dummies Questions & Answers

[Solved] Help with using tr - Removing white spaces

Hi, I have a file that contains whitespaces with spaces and spaces and tabs on each line and am wanting to remove the whitespaces. My version of sed is one that does not recognize \t etc. The sed and awk one-liners below that I found via Google both does not work. So my next best... (3 Replies)
Discussion started by: newbie_01
3 Replies

7. Shell Programming and Scripting

awk: Eliminating white space while setting variable

Hi, I have a large flat file from host without delimiter. I'm transforming this file to a csv file using statements like # Row 03: Customer / field position 3059 +20 WOFABNAM=substr( $0, 3059, 20 ); and deleting the trailing whitespaces before and after with that sub( /^ +/, "",... (4 Replies)
Discussion started by: Celald
4 Replies

8. Shell Programming and Scripting

sed + white space

Hi, What sed command (if sed is the right command) can remove ALL white space from my file. I have a csv, except I want to remove all white space between commas and characters. My idea (without testing) sed 's/ //g' Is there a better way? (18 Replies)
Discussion started by: mcclunyboy
18 Replies

9. Shell Programming and Scripting

ksh: removing all white spaces

'String' file contains the following contents, D11, D31, D92, D29, D24, using ksh, I want to remove all white spaces between characters no matter how long the string is. Would you please give me some help? (1 Reply)
Discussion started by: yoonius
1 Replies

10. Shell Programming and Scripting

stripping white space...

Hi All; Having a problem with a file.. the file contains the following data... (a snapshot) 1331F9E9DB7C2BB80EAEDE3A8F043B94,AL7 1DZ,M,50 186FDF93E1303DBA217279EC3671EA91,NG5 1JU,M,24 3783FFAF602015056A8CD21104B1AAAF,CH42 4NQ,M,17 It has 3 columns sepreated by a , the second column... (7 Replies)
Discussion started by: Zak
7 Replies
Login or Register to Ask a Question