Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Concatenating a variable name with text in Unix shell scripting

Posted on 2006-11-03
7
Medium Priority
?
5,135 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 1000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

722 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