Altering a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Altering a variable
# 1  
Old 02-03-2014
Altering a variable

Can I take an argument input, lets say it's, hg0000_xy1_v2, in the script it becomes f ... then hack off the end of the filename to change the variable to hg0000 only.

I tried using sed but can't figure it out.

Code:
f="$f" | sed 's/_fg[0-9]_v[0-9]//'

I could change the variable label if necessary to anything other than f if that's easier. I just need to be able to work with only the first 6 characters of the filename.

Any ideas?

Thanks

Last edited by Scrutinizer; 02-04-2014 at 01:11 AM..
# 2  
Old 02-03-2014
Did not test, sometime like

Code:
f=hg0000_xy1_v2
f={f%%_*}     
echo ${f}

or
Code:
f=hg0000_xy1_v2
f=`echo ${f} | cut -d _ -f1`

# 3  
Old 02-03-2014
Quote:
Originally Posted by scribling
I just need to be able to work with only the first 6 characters of the filename.

Any ideas?

Thanks
In bash/ksh you can get 1st 6 chars of f with ${f:0:6}

Code:
$ f=hg0000_xy1_v2
$ echo ${f:0:6}
hg0000

# 4  
Old 02-03-2014
${f:0:6} worked perfectly.

Thank you.
# 5  
Old 02-04-2014
More general (as blackrageous suggested dollarlessly):
Code:
echo "${f%%_*}"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Efficiently altering and merging files in perl

I have two files fileA HEADER LINE A CommentLine A Content A .... .... .... TAILER AfileB HEADER LINE B CommentLine B Content B .... .... .... TAILER BI want to merge these two files as HEADER LINE A CommentLine A Content A (4 Replies)
Discussion started by: sam05121988
4 Replies

2. Programming

Altering a jar file

I have a script I am trying to test and run but it runs against a jar file. I wrote an external property file so it would redirect with my script, but it keeps going in search of the previous property file. Is there any way to externally over write the jar file and if not how do you go about... (7 Replies)
Discussion started by: risarose87
7 Replies

3. Shell Programming and Scripting

[Solved] How to increment and add variable length numbers to a variable in a loop?

Hi All, I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I... (5 Replies)
Discussion started by: danish0909
5 Replies

4. Red Hat

How to pass value of pwd as variable in SED to replace variable in a script file

Hi all, Hereby wish to have your advise for below: Main concept is I intend to get current directory of my script file. This script file will be copied to /etc/init.d. A string in this copy will be replaced with current directory value. Below is original script file: ... (6 Replies)
Discussion started by: cielle
6 Replies

5. Shell Programming and Scripting

Scripting question: Altering 2 field.

Hi Experts, I want to alter two filed of my data file: The _new should come to 2nd column, and _new to be removed from 4rth column, please advise, datafile.txt aa /dev/vgAA/lvol1 bb /dev/vgAA_new/lvol1 aa /dev/vgAA1/lvol2 bb /dev/vgAA1_new/lvol2 aa /dev/vgAC/lvol1 bb... (5 Replies)
Discussion started by: rveri
5 Replies

6. Shell Programming and Scripting

How to define a variable with variable definition is stored in a variable?

Hi all, I have a variable say var1 (output from somewhere, which I can't change)which store something like this: echo $var1 name=fred age=25 address="123 abc" password=pass1234 how can I make the variable $name, $age, $address and $password contain the info? I mean do this in a... (1 Reply)
Discussion started by: freddy1228
1 Replies

7. SuSE

Tape Loader Device /dev/sgN is altering after each reboot

Dear all, we are running SLES 11 where an iscsi tape library is attached. To load the tape in a slot we are using for instance the command #mtx -f /dev/sg1 load 2 which is doing very well. But after a server's reboot the device name of the loader is now /dev/sg4 or /dev/sg5 I mean it is... (1 Reply)
Discussion started by: xunil321
1 Replies

8. Shell Programming and Scripting

Insert a line including Variable & Carriage Return / sed command as Variable

I want to instert Category:XXXXX into the 2. line something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong: sed "2i\\${n}Category:$cat\n" Sample: Titel Blahh Blahh abllk sdhsd sjdhf Blahh Blah Blahh Blahh Should look like... (2 Replies)
Discussion started by: lowmaster
2 Replies

9. Shell Programming and Scripting

altering numbers in files

I want to change a number in a file into number -1.. for instance file_input is fdisdlf_s35 fdjsk_s27 fsdf_s42 jkljllljkkl_s57 ... etc now i want the output to be fdisdlf_s34 fdjsk_s26 fdsf_s41 jkljllljkkl_s56 ... etc I was think of using "sed -e 's/2/1/g' -e 's/3/2/g' -e... (4 Replies)
Discussion started by: bigboizvince
4 Replies

10. Shell Programming and Scripting

Issue altering end data

I have an inventory program that I would like to have the ability to go and change or alter the field data based on the item number as a key. I have the menu option set but at the end of the script process it just appends the changed data to the database rather than what I would like; which is to... (5 Replies)
Discussion started by: stlitguru
5 Replies
Login or Register to Ask a Question