sh, ksh: command to remove front spaces from a string?


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
sh, ksh: command to remove front spaces from a string?

dear pro-coders,
is there any command out there that takes out the front spaces from a string?

sample strings:

Code:
 4 members
  5 members
   3 members

but it has to be like so:

Code:
4 members
5 members
3 members

# 2  
The Korn shell has the typeset -L option which can left justify a string
i.e. remove leading spaces.
# 3  
Hi.

Here are two methods:
Code:
#!/usr/bin/env ksh

# @(#) s1       Demonstrate front-trim blanks.

set -o nounset
echo

## Use local command version for the commands in this demonstration.

echo "(Versions of codes used in this script -- local code \"version\")"
echo "pdksh version: $KSH_VERSION"
version cat sed

cat >data1 <<EOF
 4 members
   5    members
      3 members
EOF

echo
echo " Input file:"
cat -vet data1

echo
echo " Front-trimming with variable pattern:"
while IFS= read line
do
  echo " Original line:    |$line|"
  t1=${line##+( )}
  echo " Transformed line: |$t1|"
done < data1

echo
echo " Fast front-trimming with sed:"
sed -e 's/^ *//' data1

exit 0

Producing:
Code:
% ./s1

(Versions of codes used in this script -- local code "version")
pdksh version: @(#)PD KSH v5.2.14 99/07/13.2
cat (coreutils) 5.2.1
GNU sed version 4.1.2

 Input file:
 4 members$
   5    members$
      3 members   $

 Front-trimming with variable pattern:
 Original line:    | 4 members|
 Transformed line: |4 members|
 Original line:    |   5    members|
 Transformed line: |5    members|
 Original line:    |      3 members   |
 Transformed line: |3 members   |

 Fast front-trimming with sed:
4 members
5    members
3 member

See man pages for details. The O'Reilly book "Learning the Korn Shell" would be useful if extensive use of ksh is anticipated ... cheers, drl
# 4  
MySQL

Code:
echo " Fast front-trimming with sed:"
sed -e 's/^ *//' data1

oh my god! it works! that's exactly what i was looking for Smilie
thank you for your great examples, i will study it's other parts later.
and thank you for the O'Reilly hint.

kind regards Smilie
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #43
Difficulty: Easy
A serial interface transmits two bits of information at the same time, both a one and a zero.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[sed]: syntax to insert N spaces in front of a string

Dear all, I would like to insert N blankspaces in front of a string using sed command To give an example (N=10), I tried that code: $ echo "abcd" | sed 's/^/ \{10,\}&/' but I failed, by obtaining that result: {10,}abcd Any help would be greatly appreciated, Thanks in advance,... (18 Replies)
Discussion started by: dae
18 Replies

2. Shell Programming and Scripting

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. (3 Replies)
Discussion started by: mady135
3 Replies

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

4. Shell Programming and Scripting

How to remove extra spaces from a string??

Hi, I have a string like this and i want to remove extra spaces that exists between the words. Here is the sentence. $string="The small DNA genome of hepadnaviruses is replicated by reverse transcription via an RNA intermediate. This RNA "pregenome" contains ... (2 Replies)
Discussion started by: vanitham
2 Replies

5. Shell Programming and Scripting

remove trailing and leading spaces using tr command

Dear All, can you please advice how do i remove trailing and leading spaces from a pipe-delimited file using "tr" command the below cmd, i tried removed all spaces tr -d ' '<s1.txt>s2.txt1 Many thx Suresh (5 Replies)
Discussion started by: sureshg_sampat
5 Replies

6. Shell Programming and Scripting

sed - remove spaces before 1rst occurence of string

seems easy but havent found in other posts... i want to delete any spaces if found before first occurence of ${AI_RUN} sed 's/ *\\$\\{AI_RUN\\}/\\$\\{AI_RUN\\}/' $HOME/temp1.dat i think i'm close but can't put my finger on it. :rolleyes: (6 Replies)
Discussion started by: danmauer
6 Replies

7. Shell Programming and Scripting

bash - add backslash in front of variables w/ spaces

Hello, Im writing a script that works by recursively going into directories with find. But I have some directories that have spaces in them.. so I need to parse the variables to add a backslash before the spaces. Im not exactly sure how how to do this in bash, and honestly I dont think I know... (3 Replies)
Discussion started by: trey85stang
3 Replies

8. Shell Programming and Scripting

remove blank spaces in a string

can any help how to remove blank spaces in a string? STR="GOOD BYE" by removing blank spaces, the string should be GOOD,BYE thanks in advance (2 Replies)
Discussion started by: spandu
2 Replies

9. Shell Programming and Scripting

Variable has spaces around the string, need to remove them

Hi all, I'm a newbie to the Linux world and I got a couple of shell script questions: (1) How do combine two variables and make it equal to a third variable? For example, I got a variable $A=FirstName, $B=LastName, and I want to combine the variable into one variable so when you echo the final... (4 Replies)
Discussion started by: mikey20
4 Replies

10. Shell Programming and Scripting

how to remove spaces in a string using sed.

Hello, I have the following to remove spaces from beginning and end of a string. infile=`echo "$infilename" | sed 's/^ *//;s/ *$//` How do I modify the above code to remove spaces from beginning, end and in the middle of the string also. ex: ... (4 Replies)
Discussion started by: radhika
4 Replies

Featured Tech Videos