Computing.Net > Forums > Unix > String manipulation

String manipulation

Reply to Message Icon

Original Message
Name: PaulDavidSumner
Date: April 27, 2006 at 11:00:29 Pacific
Subject: String manipulation
OS: Windows XP Build 2600
CPU/Ram: Pentium 4 2.26 GHz/256 MB
Model/Manufacturer: 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?


Report Offensive Message For Removal


Response Number 1
Name: nails
Date: April 27, 2006 at 12:24:30 Pacific
Subject: String manipulation
Reply: (edit)

#!/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


Report Offensive Follow Up For Removal

Response Number 2
Name: lchi2000g
Date: April 27, 2006 at 14:01:59 Pacific
Subject: String manipulation
Reply: (edit)

Another solution for #1 question:

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

Luke Chi


Report Offensive Follow Up For Removal

Response Number 3
Name: lchi2000g
Date: April 27, 2006 at 14:33:00 Pacific
Subject: String manipulation
Reply: (edit)

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


Report Offensive Follow Up For Removal

Response Number 4
Name: lchi2000g
Date: April 27, 2006 at 14:47:52 Pacific
Subject: String manipulation
Reply: (edit)

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


Report Offensive Follow Up For Removal

Response Number 5
Name: lchi2000g
Date: April 27, 2006 at 17:48:35 Pacific
Subject: String manipulation
Reply: (edit)

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


Report Offensive Follow Up For Removal


Response Number 6
Name: lchi2000g
Date: April 27, 2006 at 18:15:59 Pacific
Subject: String manipulation
Reply: (edit)

Another answer for #1 question:

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


Luke Chi


Report Offensive Follow Up For Removal

Response Number 7
Name: PaulDavidSumner
Date: April 28, 2006 at 04:05:12 Pacific
Subject: String manipulation
Reply: (edit)

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


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: String manipulation

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge