Computing.Net > Forums > Unix > String manipulation

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

String manipulation

Reply to Message Icon

Name: PaulDavidSumner
Date: April 27, 2006 at 11:00:29 Pacific
OS: Windows XP Build 2600
CPU/Ram: Pentium 4 2.26 GHz/256 MB
Product: Dell GX260 Optiplex
Comment:

I need to do some string manipulation to ensure the final way I put together a feed file trailer isn't done with clumsy coding. Anyone out there who knows Unix string manipulation well, please read on...

1. I am assigning an environment variable based on the count of records in a file. As an example:

MY_VAR=`wc -l $MY_FILE | awk '{printf $1}'`

But, as well as containing the number, MY_VAR has to be right-padded with spaces so it is 4 characters in length. e.g. if MY_FILE contains 10 records, MY_VAR must be '10 '. Is there a neat way of doing this?

2. I have a couple of variables containing many repeated instances of the same character. FILLER_1 needs to be 60 instances of '9', and FILLER_2 needs to be 184 spaces.

Currently I have my prototype using assignments like the following example, just to give me something that is up and running:

FILLER_1='999999999999999999999999999999999999999999999999999999999999' # 60 instances of '9'

This is a bit clumsy, so I don't want to include it in the final live system. Does anyone know of a function which can be passed a character and number and output the character the specified number of times?



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: April 27, 2006 at 12:24:30 Pacific
Reply:

#!/bin/ksh

#1)
MY_FILE=myfile
MY_VAR=`wc -l < $MY_FILE`
MY_VAR=`echo $MY_VAR ` # change int to string
MY_VAR=`printf "%-4.4s" "$MY_VAR"` # pad with 4 spaces
echo "|$MY_VAR|"

#2)
# variation of Eric Pement's center routine at:
# http://www.student.northpark.edu/pemente/sed/sed1line.txt
# STR=9
# echo $STR|sed -e :a -e 's/^.\{1,59\}$/&9/;ta'

# paramaterize it:
LEN=60
CHAR=9
LEN=$((LEN-1)) # subtract 1

STR=$CHAR
sed_cmd="echo \$STR|sed -e :a -e 's/^.\{1,${LEN}\}$/&${CHAR}/;ta'"
FILLER_1=`eval $sed_cmd`
echo $FILLER_1 # should be 60-9s


0

Response Number 2
Name: lchi2000g
Date: April 27, 2006 at 14:01:59 Pacific
Reply:

Another solution for #1 question:

#!/bin/ksh
VAR=$(echo `wc -l < input.txt`" " | cut -c1-4)
echo "|$VAR|"

Luke Chi


0

Response Number 3
Name: lchi2000g
Date: April 27, 2006 at 14:33:00 Pacific
Reply:

I'm sorry.

The # of spaces between the double quotes are four in the following line:
VAR=$(echo `wc -l < input.txt`" " | cut -c1-4)

For some reason, it only showed one space when I copied/pasted/posted it. It should be four spaces.

Luke Chi


0

Response Number 4
Name: lchi2000g
Date: April 27, 2006 at 14:47:52 Pacific
Reply:

How to insert 100 chars of 9 in vi?

1. press i - to go to the vi insert mode
2. press 9 - to insert the char 9
3. press Esc - to go to the vi command mode
4. type 99. - to repeat 99 times of the previous vi edit command which was "inserting an i char at the current cursor location"
5. done - your're in the vi command mode now


Luke Chi


0

Response Number 5
Name: lchi2000g
Date: April 27, 2006 at 17:48:35 Pacific
Reply:

The answer to #2 question:
(creating a line with one hundred 9 chars)

#!/bin/ksh
typeset -Z100 VAR=0
echo $VAR | tr 0 9


Luke Chi


0

Related Posts

See More



Response Number 6
Name: lchi2000g
Date: April 27, 2006 at 18:15:59 Pacific
Reply:

Another answer for #1 question:

#!/bin/ksh
typeset -L4 VAR=`wc -l < input.txt`
echo "|$VAR|"


Luke Chi


0

Response Number 7
Name: PaulDavidSumner
Date: April 28, 2006 at 04:05:12 Pacific
Reply:

Thanks for the various solutions - it's working well now.


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: String manipulation

substring of a text string www.computing.net/answers/unix/substring-of-a-text-string/5608.html

Unix String Manipulation www.computing.net/answers/unix/unix-string-manipulation/6067.html

search & insert text into a string www.computing.net/answers/unix/search-amp-insert-text-into-a-string/5013.html