Solved

Concatenating a variable name with text in Unix shell scripting

Posted on 2006-11-03
7
5,118 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.

821 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