|
Search Forums:
|
|||||||
| Forums | Register | Forum Rules | Linux and Unix Links | Man Pages | Albums | FAQ | Users | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
trim spaces in unix for variable
HI Guys I have written a script using awk to split a file based on some identifier and renaming the file based on two values from specific length. ts a fixed width file. When I am trying to fetch the values Code:
a = substr($0,11,10) b = substr($0,21,5); i am getting spaces in a and b values . I have to form a file like a_b.txt but the file is forming as "a _ b .txt" Please help guys. Last edited by Franklin52; 02-09-2012 at 04:29 AM.. Reason: Please use code tags for code and data samples, thank you |
| Sponsored Links | |
|
|
|
#2
|
||||
|
||||
|
To remove all the spaces in $a and $b Code:
a=${a// /}
b=${b// /} |
| Sponsored Links | ||
|
|
|
#3
|
|||
|
|||
|
Hi when i tried using the way you provided Code:
a = substr($0,11,10)
a1= ${a// /}i got this error Code:
The error context is
a= >>> ${ <<<
awk: 0602-502 The statement cannot be correctly parsed.Last edited by Franklin52; 02-09-2012 at 04:29 AM.. Reason: Please use code tags for code and data samples, thank you |
|
#4
|
|||
|
|||
|
To trim the spaces in your awk program from the a variable: left spaces: Code:
gsub(/^[ \t]+/, "", a) right spaces: Code:
gsub(/[ \t]+$/, "", a) |
| Sponsored Links | |
|
|
#5
|
||||
|
||||
|
is it inside the awk ?
if it is inside the awk, use the franklin suggestion. |
| Sponsored Links | |
|
|
#6
|
|||
|
|||
|
my data is coming as Code:
INDIAINDIABACD 11 INDIAINDIABACD 12 and i split the data into multiple files based in INDIAINDIA and file name will be BACD_11.txt and BACD_12.txt but in this case it is forming as Code:
"BACD 12 .txt" and "BACD 11 .txt" a = substr($0,11,10) a2=gsub(/[ \t]+$/, "", a) b = substr($0,21,5) b2=gsub(/[ \t]+$/, "", b) fn = a2 "_Src" b2 ".txt" using gusb i am not able to trim also it is returning blank to a2 and b2 respectively. This whole code is inside awk ---------- Post updated at 07:01 AM ---------- Previous update was at 05:19 AM ---------- Thanks guys I got that solved. Last edited by Franklin52; 02-09-2012 at 07:03 AM.. Reason: Code tags |
| Sponsored Links | |
|
|
#7
|
||||
|
||||
|
Try losing the $ signs in the gsub statements.
You cannot assign gsub to a variable, it only returns the exit status What is a sample input line? |
| Sponsored Links | ||
|
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| trim spaces and replacing value | badrimohanty | Shell Programming and Scripting | 2 | 06-29-2009 04:30 PM |
| trim spaces in a file | badrimohanty | Shell Programming and Scripting | 7 | 06-22-2009 01:53 PM |
| TRIM spaces in shell | anumkoshy | UNIX for Advanced & Expert Users | 3 | 08-31-2007 04:13 AM |
| To Trim spaces at the end of line | sbasetty | Shell Programming and Scripting | 1 | 01-31-2007 09:01 PM |
| Trim white spaces using awk | mona | Shell Programming and Scripting | 2 | 07-28-2006 04:47 AM |
|
|