Solved

Concatenating a variable name with text in Unix shell scripting

Posted on 2006-11-03
7
5,109 Views
Last Modified: 2013-12-26
I have a variable named FILENAME1  whose value is yyyymmdd.csv
I want to extract the first 8 characters from the variable and then concatenate _m.csv to it
I'm new to unix shell scripting, but sure this can be done  
0
Comment
Question by:VBStudent
  • 4
  • 2
7 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 17870302
I'm not sure which shell you are using, but
echo $FILENAME1 | perl -pe 'substr($_,8)="_m.csv"'
is one way
0
 
LVL 84

Expert Comment

by:ozo
ID: 17870361
in some shells, such as bash or ksh, you could do
echo ${FILENAME1:0:8}_m.csv
0
 
LVL 16

Expert Comment

by:manav_mathur
ID: 17870428
new_variable=`echo $FILENAME1 | sed 's/\./_m./'`
or
new_variable=`echo ${FILENAME1:0:8}_m.csv`
or
new_variable=`echo $FILENAME1 | perl -pe 'substr($_,8)="_m.csv"'`
 
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:VBStudent
ID: 17883298
echo $FILENAME1 | perl -pe 'substr($_,8)="_m.csv"'
This syntax works well, however, how do I assign the output to the variable name FILENAME1?

I tried FILENAME1 = $FILENAME1|perl -pe 'substr($_,8)="_m.csv"' but it stripped the original filename and left me with _m.csv
0
 
LVL 84

Expert Comment

by:ozo
ID: 17883687
You still didn't say which shell you are using
FILENAME1=`echo $FILENAME1|perl -pe 'substr($_,8)="_m.csv"'`
would work in some shells
0
 

Author Comment

by:VBStudent
ID: 17883936
I'm using ksh shell
0
 
LVL 84

Accepted Solution

by:
ozo earned 250 total points
ID: 17884096
FILENAME1=`echo $FILENAME1|perl -pe 'substr($_,8)="_m.csv"'`
or
FILENAME1=${FILENAME1:0:8}_m.csv
should work in ksh
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video discusses moving either the default database or any database to a new volume.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now