|
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
|
||||
|
||||
|
Trimming Spaces in Unix
Hi All, I am using following script to name the file base of some values Code:
#!/bin/sh
sourcefile=$1
awk '
BEGIN{ n = 1; name = "FILEFILE12" n ".txt"; }
{
if (substr($0,1,10) == "FILEFILE12")
{
close (name)
n++
a = substr($0,11,10);
b = substr($0,21,5);
name = b "_Src_" a ".txt"
}
print > name
}
' $sourcefileValues in a and b are coming with spaces and hence the file which is forming is like "test1 _Src_test2 .txt" . Can you please help me in trimming the spaces.
Last edited by vbe; 02-08-2012 at 09:19 AM.. |
| Sponsored Links | |
|
|
|
#2
|
||||
|
||||
|
Remove extra spaces by below trick Say l Code:
ampm="a b c"
lampms=${lampm//[[:space:]]}
echo $lampms
abc--Shirish Shukla
Last edited by Franklin52; 02-08-2012 at 10:05 AM.. Reason: Please use code tags for code and data samples, thank you |
| Sponsored Links | ||
|
|
|
#3
|
||||
|
||||
|
Code:
tr -d [:SPACE:] is the simplest solution for this |
|
#4
|
||||
|
||||
|
You just need to tweak the substr function parameters to avoid the space. Provide the input file you are using so that we can also have a look.
--ahamed |
| Sponsored Links | |
|
|
#5
|
|||
|
|||
|
I know.. but.. above for simple understand ... as per que asked ..
Note: SPACE here always have to be in lower case .... [:space:] # a="a b c" # echo $a | tr -d [:SPACE:] tr: invalid character class `SPACE' Also # echo $a | tr -d [:space:] b c <-- Missing 1st char ... # echo $a | tr -d "[:space:]" <-- Must be quoted abc There are multiple ways ... using sed, awk, tr etc... -Shirish |
| Sponsored Links | |
|
|
#6
|
||||
|
||||
|
Quote:
Code:
pandeeswaran@ubuntu:~$ echo a b c|tr -d [[:space:]] abc |
| Sponsored Links | |
|
|
#7
|
|||
|
|||
|
my data is coming as
INDIAINDIABACD 11 XYZ INDIAINDIABACD 12 XYZ 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 "BACD _Src_12 .txt" and "BACD _Src_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:02 AM ---------- Previous update was at 05:33 AM ---------- Thanks guys , I got the solution. Thanks for your help |
| 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 |
| Trimming spaces from a variable | manish8484 | Shell Programming and Scripting | 0 | 12-13-2011 07:50 AM |
| How can I stop the unix script from trimming extra spaces? | Pramit | Shell Programming and Scripting | 4 | 11-02-2011 06:22 AM |
| trimming out spaces in solaris | achak01 | Shell Programming and Scripting | 1 | 10-16-2009 07:40 AM |
| Trimming the spaces | sharif | UNIX for Advanced & Expert Users | 3 | 07-07-2008 06:26 AM |
| trimming white spaces | briskbaby | Shell Programming and Scripting | 9 | 09-23-2006 06:10 PM |
|
|