Visit Our UNIX and Linux User Community


awk: Eliminating white space while setting variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk: Eliminating white space while setting variable
# 1  
Old 10-29-2010
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


Code:
# Row 03: Customer / field position  3059 +20 
WOFABNAM=substr( $0, 3059, 20 );

and deleting the trailing whitespaces before and after with that

Code:
sub( /^ +/, "", WOFABNAM );
sub( / +$/, "", WOFABNAM );

Is here a better way to do that like pseudo

Code:
WOFABNAM=substr( $0, 3059, 20 ) | sed 's/ //g' ;

thanks in advance

Celal

Last edited by Celald; 10-29-2010 at 06:11 AM.. Reason: spelling
# 2  
Old 10-29-2010
Hi,

Removing leading and trailing spaces?

Try:
Code:
WOFABNAM=substr( $0, 3059, 20 ) | sed 's/^ *// ; s/ *$//' ;

Regards,
Birei
# 3  
Old 10-29-2010
Code:
gsub(/ /, "", WOFABNAM)

The above will remove any space in the string (not only leading/trailing).
Is that OK?
# 4  
Old 10-29-2010
Quote:
Originally Posted by radoulov
Code:
gsub(/ /, "", WOFABNAM)

The above will remove any space in the string (not only leading/trailing).
Is that OK?
Thanks, that works.

I wonder (and that was the reason for my post) that the same gsub command I tried before didn't work.

It's sad that following doesn't work:

Code:
WOFABNAM=gsub(/ /, "", substr( $0, 3059, 20 ) );


nawk: syntax error at source line 18
 context is
        OFABNAM=gsub(/ /, "", >>>  substr <<< ( $0, 3059, 20 ) );
nawk: illegal statement at source line 18

thanks again,
celal
# 5  
Old 10-29-2010
In your case it doesn't work because gsub third parameter is not a changeable object.

However, this won't work with a static string either, because sub and gsub do not return the modified string, they rather return the number of substitutions made.

The GNU awk's gensub returns the modified string.

Previous Thread | Next Thread
Test Your Knowledge in Computers #590
Difficulty: Easy
In a linear search algorithm, the input size is the number of elements in the collection (n) divided by 2.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing white space in awk

Hi How to remove white space from this input:|blue | 1| |green| 4| |black| 2| I like to search for green and get 4not 4 How to modify this to work correct:awk -F"|" '/green/ {print $3} (7 Replies)
Discussion started by: Jotne
7 Replies

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

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

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

5. Shell Programming and Scripting

Eliminating space constraint in grep

here in the below code just a space between 'Info' and '(' is showing that the patter doesnt match... echo "CREATE TABLE Info (" | grep -i "CREATE TABLE Info (" | wc | awk -F' ' '{print $1}' 1 echo "CREATE TABLE Info (" | grep -i "CREATE TABLE Info (" | wc | awk -F' ' '{print $1}' 0 ... (9 Replies)
Discussion started by: vivek d r
9 Replies

6. Shell Programming and Scripting

using awk for setting variable but change the output of this variable within awk

Hi all, Hope someone can help me out here. I have this BASH script (see below) My problem lies with the variable path. The output of the command find will give me several fields. The 9th field is the path. I want to captured that and the I want to filter this to a specific level. The... (6 Replies)
Discussion started by: Cowardly
6 Replies

7. Shell Programming and Scripting

Setting path variable with a space.

Hi I am using MKS Toolkit c shell. I am trying to set a path variable something like c:/Program Files/blah/blah so set path=(c:/Program Files/blah/blah) this, however, does not work as it splits this thing up into 'c:/Program' and 'Files/blah/blah'. Does anyone have any ideas on... (9 Replies)
Discussion started by: vas28r13
9 Replies

8. Shell Programming and Scripting

nested double quota and white space inside variable

I have a question about nested double quotes. Any help is appreciated. Here are my commands on Mac OS. # string="Ethernet \"USB Ethernet\" \"Bluetooth DUN\" AirPort FireWire \"Bluetooth PAN\"" # echo $string Ethernet "USB Ethernet" "Bluetooth DUN" AirPort FireWire "Bluetooth PAN" #... (3 Replies)
Discussion started by: lindazhou
3 Replies

9. AIX

Eliminating paging space and interpreting memory utilization

I just want to inquire on one of our DB Servers. Currently, we are running on 26GB of memory and 6 CPUs. Though our memory is 70-80 utilized, I can see some paging of around 8-10%. Is there any effective way we can lessen/eliminate paging? Here is our current VMO Settings: vmo: ... (1 Reply)
Discussion started by: depam
1 Replies

10. UNIX for Dummies Questions & Answers

setting environment variable in awk

Dear all, I have a data sample... Dose: Summed ROI: Bladder ************************** Bin Dose Volume 001 0.700 100.000 002 0.715 99.998 168 3.142 0.368 169 3.157 0.338 170 3.171 0.292 Dose: Summed ROI:... (2 Replies)
Discussion started by: tintin72
2 Replies

Featured Tech Videos