How to remove white spaces from the beginning an end of a string in unix?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to remove white spaces from the beginning an end of a string in unix?
# 1  
Old 10-05-2010
Question How to remove white spaces from the beginning an end of a string in unix?

Suppose, I have a variable var=" name is ".
I want to remove the blank spaces from the begining and endonly, not from the entire string.
So, that the variable/string looks like following
var="name is".
Please look after the issue.
# 2  
Old 10-05-2010
hi,
If it is in variable , use echo to output the variable var.

Code:
a=" Test this "
echo $a
Test this

or something like this:
Code:
echo " value is " | sed -e 's/^ \(.*\) $/\1/'


Last edited by dragon.1431; 10-05-2010 at 11:42 AM.. Reason: typo
# 3  
Old 10-05-2010
Code:
var2=$(echo "$var" | { read x; echo "$x" ;})

bash/ksh93:
Code:
read var2<<<"$var"

# 4  
Old 10-05-2010
set $var
var="$*"
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] Remove White spaces from teh beginnning from a particular row

Hi All, $sed -n "58p" sample 1222017 --- Its has to display like: 1222017 (5 Replies)
Discussion started by: Anamica
5 Replies

2. Shell Programming and Scripting

Putting white Space at the end of the string

Hi Guys, Hope, you all are doing good out there. I am writing a shell script and currrint in need of your help. This is what I need to do; I have position based plain file. One of the fields is 15 character long. I need to fill that field. The problem is that the value is dynamic, it could... (4 Replies)
Discussion started by: singh.chandan18
4 Replies

3. Shell Programming and Scripting

Remove white spaces from flat file generated from Oracle table...

I have to export data from table into flat file with | delimited. In the ksh file, I am adding below to do this activity. $DBSTRING contains the sqlplus command and $SQL_STRING contains the SQL query. File is created properly with the data as per SQL command. I am getting white spaces in the... (1 Reply)
Discussion started by: mgpatil31
1 Replies

4. UNIX for Advanced & Expert Users

Tcl script for seaching a string ignoring white spaces

Hi , I want to search a string in a file ignoring white spaces in TCL. My string is as follows Ouput-Maps " 1 1 1 0 " 1i am doing following set a *1*1*1*0* " }1 abc.log}] but it is not working. What is the wrong with the tcl script Thanks Gouranga Video... (0 Replies)
Discussion started by: mybapa3000@gmai
0 Replies

5. Post Here to Contact Site Administrators and Moderators

Want a tcl script to compare a string in a file ignoring white spaces

Hi , I want a tcl script to search a string ignoring whitespaces in a .log file . It should correctly match . The string are as follows "Output-Maps 1 1 0 0 0" 1 and Active Intermediate-Maps 0 0 0 ... (1 Reply)
Discussion started by: kulua
1 Replies

6. Shell Programming and Scripting

Unix remove white spaces/tabs before & after pattern

Hi All, I wanted to know is there any way we can remove white spaces/tabs before & after some pattern { eg. before & after "," }. Please find below sample data below, Sat Jul 23 16:10:03 EDT 2011 , 12345678 , PROD , xyz_2345677 , testuuyt , ... (3 Replies)
Discussion started by: gr8_usk
3 Replies

7. Shell Programming and Scripting

Remove comma and next rows beginning from the end

Hello friends, I have a file which consists of many rows, I use a couple of commands to convert it so i can use in a database query for filtering. I need the first columns (msisdns) in a row, seperated with commas, 9855162267,4,5,2010-11-03 17:02:07.627 9594567938f,5,5,2010-11-02... (9 Replies)
Discussion started by: EAGL€
9 Replies

8. Shell Programming and Scripting

Remove spaces in the beginning of a string

Hi, I am trying to remove spaces from the beginning of a string (which i am using in my shell script). For ex - my string looks like this - " no rows selected" and i want the string to look like this - "no rows selected" How can i achieve this? Thanks! (18 Replies)
Discussion started by: shrutihardas
18 Replies

9. Shell Programming and Scripting

Remove white space at the beginning of lines

Hi! I store some data obtained with grep or awk in a file. The problem is that some lines have white space at the begining : line1 line2 line3 I use something like grep WORD INFILE >> OUTFILE awk >> OUTFILE I would love if it were possible to remove the white whitout parsing the... (4 Replies)
Discussion started by: tipi
4 Replies

10. Shell Programming and Scripting

Two or more white spaces in string

Hi, Can anybody suggest me how to combine two strings with two or more white spaces and assign it to a variable? E.g. first=HAI second=HELLO third="$first $second" # appending strings with more than one white spaces echo $third this would print HAI HELLO Output appears... (2 Replies)
Discussion started by: harish_oty
2 Replies
Login or Register to Ask a Question