Storing command output in a variable and using cut/awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Storing command output in a variable and using cut/awk
# 1  
Old 08-21-2014
Storing command output in a variable and using cut/awk

Hi,

My aim is to get the md5 hash of a file and store it in a variable.


var1="md5sum file1"
$var1


The above outputs fine but also contains the filename, so somthing like this 243ASsf25 file1

i just need to get the first part and put it into a variable.


var1="md5sum file1"
$var1 | awk '{print $1}'


This only prints the first part which is what i want but i dont know how to put it into a variable to use later on
# 2  
Old 08-21-2014
Code:
newvar={$var1 | awk '{print $1}'}

or
Code:
newvar=`$var1 | awk '{print $1}'`

# 3  
Old 08-21-2014
Quote:
Originally Posted by Franklin52
Code:
newvar={$var1 | awk '{print $1}'}

or
Code:
newvar=`$var1 | awk '{print $1}'`

hi,

the first one doesn't work , shows a syntax error on the last bracket.



I am doing this.

Code:
var1="md5sum file1"

newvar={$var1 | awk '{print $1}'}

$newvar


Last edited by rbatte1; 08-21-2014 at 10:12 AM..
# 4  
Old 08-21-2014
I'm sorry, the first command should be:
Code:
newvar=$($var1 | awk '{print $1}')

This User Gave Thanks to Franklin52 For This Post:
# 5  
Old 08-21-2014
JustALol,
I think you are trying to over work this and have confused it trying to make it neat. Would this do?:-
Code:
md5sum file1 | read newvar filename
print $newvar

What is the intention with this? If you are looking to store the value long term and then check for unauthorised changes, then md5sum allows you to read a file containing the hashes and compare them:-
Code:
echo "Original file" > my_test_file
for file in *
do
   md5sum $file
done > /tmp/my_hashes

echo "Changed file" > my_test_file
md5sum -c /tmp/my_hashes

Does that work with what you are really intending to do?



Robin

Last edited by rbatte1; 08-21-2014 at 10:18 AM.. Reason: Addressed to JustALol, not Franklin52
# 6  
Old 08-23-2014
Hi,

sorry for my late reply, it ended up working fine

Code:
newvar=$($var1 | awk '{print $1}')


Thankyou for your replies

Last edited by JustALol; 08-23-2014 at 03:36 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use cut output as variable piped awk command

Hi, I would like use the output of my cut command as a variable in my following awk command. Here's what I've written. cut -f1 info.txt | awk -v i=xargs -F'' '{if($6 == $i) print $20}' summary.txt Where obviously the 'xargs' doesn't do what I want. How can I pass my cut result to my awk... (3 Replies)
Discussion started by: heyooo
3 Replies

2. Shell Programming and Scripting

Storing multiple sql queries output into variable by running sql command only once

Hi All, I want to run multiple sql queries and store the data in variable but i want to use sql command only once. Is there a way without running sql command twice and storing.Please advise. Eg : Select 'Query 1 output' from dual; Select 'Query 2 output' from dual; I want to... (3 Replies)
Discussion started by: Rokkesh
3 Replies

3. UNIX for Advanced & Expert Users

Passing variable as input & storing output in other variable

I have a below syntax its working fine... var12=$(ps -ef | grep apache | awk '{print $2,$4}') Im getting expected output as below: printf "%b\n" "${VAR12}" dell 123 dell 456 dell 457 Now I wrote a while loop.. the output of VAR12 should be passed as input parameters to while loop and results... (5 Replies)
Discussion started by: sam@sam
5 Replies

4. Shell Programming and Scripting

Storing awk command in a variable

I'm working on a script in which gives certain details in its output depending on user-specified options. So, what I'd like to do is something like: if then awkcmd='some_awk_command' else awkcmd='some_other_awk_command' fi Then, later in the script, we'd do something like: ... (5 Replies)
Discussion started by: treesloth
5 Replies

5. Shell Programming and Scripting

Storing output into a variable

My script below seems to be choking because I need the the output of the find command to be stored as a variable that can then be called by used lower in the script. #!/bin/bash cd "/resumes_to_be_completed" var1=find . -mmin -1 -type f \( -name "*.doc" -o -name "*.docx" \)... (1 Reply)
Discussion started by: binary-ninja
1 Replies

6. Shell Programming and Scripting

Storing output of "time" command to a variable

Hi all, I am new to Linux/shell scripting having moderate knowledge. In my script, I need to get execution time of a command (say 'ls') in mili seconds level. For this i tried using "time" command to retrieve the total execution time in milli seconds. But, the problem is that, how to save... (9 Replies)
Discussion started by: happening_linux
9 Replies

7. Shell Programming and Scripting

storing output from echo & cut into variable

Hi All, Hope someone can advise here as I have been struggling to find a syntax that works here. I have tried a stack of combination I have seed in the forums but I think because I have needed to use "" and `` in the statments another method is found. I am reading in lines with the following... (1 Reply)
Discussion started by: nkwilliams
1 Replies

8. UNIX Desktop Questions & Answers

problem while storing the output of awk to variable

Hi, i have some files in one directory(say some sample dir) whose names will be like the following. some_file1.txt some_file2.txt. i need to get the last modified file size based on file name pattern like some_ here i am able to get the value of the last modified file size using the... (5 Replies)
Discussion started by: eswarreddya
5 Replies

9. UNIX for Dummies Questions & Answers

Storing the output into a variable

Hi unix gurus, I am trying to store the result of a command into a variable. But it is not getting stored. x='hello' y=echo $x | wc -c but it is giving the output as 0(zero) Pls help me its very urgent (7 Replies)
Discussion started by: ravi raj kumar
7 Replies

10. Shell Programming and Scripting

storing output of awk in variable

HI I am trying to store the output of this awk command awk -F, {(if NR==2) print $1} test.sr in a variable when I am trying v= awk -F, {(if NR==2) print $1} test.sr $v = awk -F, {(if NR==2) print $1} test.sr but its not working out . Any suggestions Thanks Arif (3 Replies)
Discussion started by: mab_arif16
3 Replies
Login or Register to Ask a Question