I save the result in a variable


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers I save the result in a variable
# 1  
Old 09-08-2014
I save the result in a variable

I have friends that this command worked perfectly, but I would like to save the result in a variable, which I have not achieved
Code:
var=prueba.txt

echo $var | cut -d "." -f 1

prueba


I need to do this but does not work me
Code:
salida=echo $var | cut -d "." -f 1


echo "result:$salida"

prueba


Last edited by vbe; 09-08-2014 at 10:13 AM..
# 2  
Old 09-08-2014
try
Code:
salida=`echo $var | cut -d "." -f 1`

This User Gave Thanks to Makarand Dodmis For This Post:
# 3  
Old 09-08-2014
thank you very much worked perfectly
# 4  
Old 09-08-2014
Hello tricampeon81,

you can use the following command also for same.

Code:
 salida=`echo "prueba.txt" | awk '{gsub(/\..*/,X,$0);print "result= " $0}'`

Output will be as follows.

Code:
echo $salida
result= prueba


Thanks,
R. Singh

Last edited by RavinderSingh13; 09-08-2014 at 10:29 AM.. Reason: Added variable approch
# 5  
Old 09-08-2014
Quote:
Originally Posted by Makarand Dodmis
try
Code:
salida=`echo $var | cut -d "." -f 1`

Quote:
Originally Posted by RavinderSingh13
Code:
 salida=`echo "prueba.txt" | awk '{gsub(/\..*/,X,$0);print "result= " $0}'`

Yikes!

Please notice, that every new process you start is taxing on the system. If you do this on a considerable amount of strings (like i.e. inside a loop) it will slow down your script considerably. Use "variable expansion" (or "parameter expansion", as it is also called) for this:

Code:
var="file.extension"
echo "${var%.*}"

If more than one delimiter characters can be in teh string you want to change notice, notice the following (longest/shortest match):

Code:
var="file.ext1.ex2.ext3"
echo "${var%.*}"
echo "${var%%.*}"

I hope this helps.

bakunin

Last edited by bakunin; 09-08-2014 at 03:48 PM..
This User Gave Thanks to bakunin For This Post:
# 6  
Old 09-08-2014
Bug

Quote:
Originally Posted by bakunin
Code:
var="file.extension"
echo "$var{%.*}"

Code:
var="file.ext1.ex2.ext3"
echo "$var{%.*}"
echo "$var{%%.*}"

I guess you mean echo "${var%.*}" and echo "${var%%.*}" Smilie
This User Gave Thanks to junior-helper For This Post:
# 7  
Old 09-08-2014
Quote:
Originally Posted by junior-helper
I guess you mean echo "${var%.*}" and echo "${var%%.*}" Smilie
Yes, you are right! How on earth i could mix that up so consistently.... *shakes head about himself*

bakunin
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

How to save a data of a file into a variable?

My requirement is to read a column data from a file & save it in a variable for each row & process it. I am using the below code- Leadlines="$TGTFILE/Error.txt">>$log_file while read line do id = ` echo $line | cut -d "," -f1 ` email = ` echo $line | cut -d "," -f2 ` ----------- done My... (2 Replies)
Discussion started by: saga20
2 Replies

2. Shell Programming and Scripting

Save result to a text file

Currently I have a perl code to combine two different files. #! /usr/bin/perl -w use strict; open FP1,"A.txt"; open FP2,"B.txt"; my ($l1,$l2); while(1) { $l1=<FP1>; chomp $l1; $l2=<FP2>; chomp $l2; last unless(defined $l1 or defined $l2); print "$l1 $l2\n"; } close FP2;... (4 Replies)
Discussion started by: Tzeronone
4 Replies

3. UNIX for Dummies Questions & Answers

Solved: how to save an output to a variable

i want to save the output of /scripts/whoowns domain.com to a username like $user = /scripts/whoowns domain.com but I'm not sure how to do that This is inside a bash script how can I get the output of /scripts/whoowns then save that to a variable? thanks! ---------- Post updated at... (0 Replies)
Discussion started by: vanessafan99
0 Replies

4. Shell Programming and Scripting

awk and save result on a different file

Hi, If I type: ls -l *txt | awk '{print $8}' I get the file listing if I am in the directory. If I try to do the same from a job flow, doing also other things, I can't do ls -l directory/*txt | awk '{print $8}' > directory/result.txt or echo ls -l directory/*txt | awk '{print... (8 Replies)
Discussion started by: essemario
8 Replies

5. Shell Programming and Scripting

how to save an output of a command in a variable

Hi, in shell script, i have the command swstart -p which returns an output. i want to store the output of this command into a variable. how i can do that excerpt from the script #!/usr/bin/ksh # # # # Program: swstart -p # # Description: Starts the sentinels on Slave server ... (4 Replies)
Discussion started by: lookinginfo
4 Replies

6. Shell Programming and Scripting

Save output in a variable

Hi, I have a 3rd party tool on Solaris 8. I am running it thorugh my script but I am not able to capture its output into a variable. Like, I tried, but did not work. output=`/usr/bin/myTool` echo $output Also, I tried saving in a file but when I run, output is always shown on the... (19 Replies)
Discussion started by: angshuman_ag
19 Replies

7. Shell Programming and Scripting

Way to save output result of a program into another new file...

Does anybody know any alternative way to save output result of a program into another new file? I got try the command below: program_used input_file > new_output_file program_used input_file >> new_output_file Unfortunately, both the ">" and ">>" is not work at this case to save the output... (6 Replies)
Discussion started by: patrick87
6 Replies

8. Shell Programming and Scripting

"Time" command and save result in a file.txt

Hi, I'am using "time" to check execution time of some script. Is there any possibility to save time command result into a file ? (2 Replies)
Discussion started by: Physix
2 Replies

9. Shell Programming and Scripting

save the HTML result

Hi all, I am displaying my result in HTML format using tables. I want to save the results in file in the same format along with the table only. How do i do that in perl? I have attached the table structure . I want to save like that itself . with regards (2 Replies)
Discussion started by: vanitham
2 Replies

10. UNIX for Dummies Questions & Answers

Save contents of a Variable

I want to save the contents of a variable to a file. How can that be achieved? I have tried with: echo $varname > textfile.txt but for some reason it does not print anything. (1 Reply)
Discussion started by: carl_vieyra
1 Replies
Login or Register to Ask a Question