Solved

Concatenating a variable name with text in Unix shell scripting

Posted on 2006-11-03
7
5,110 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Question regarding Copy/Paste 16 91
ShiftLeft challenge 21 76
abstract class with all non abstract mentods 6 76
MaxSpan challenge 9 82
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…
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.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

895 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

13 Experts available now in Live!

Get 1:1 Help Now