LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-30-2008, 03:50 AM   #1
Jurrian
Member
 
Registered: Sep 2008
Posts: 58

Rep: Reputation: 15
awk in script


Hi all,

I want to split the result of a command to only use the first word.
Searching on internet resulted in awk, so I use it as following:

Code:
awk '{print $1}' input.txt
this works fine when I use commandline.

However, when I put it inside a small script:

Code:
du $1 > currentdiroutputtemp.txt
FILESIZE="awk '{print $1}' currentdirouttemp.txt"
echo $FILESIZE
$FILESIZE
it fails to succesfully parse the $1, as it will use the $1 from executing the script. Is there anyway I can prevent this?

Thanx in advance,

Jurrian
 
Old 10-30-2008, 04:33 AM   #2
maradnus
Member
 
Registered: Oct 2008
Location: Yellagiri Hills
Distribution: Fedora
Posts: 87

Rep: Reputation: 15
Thumbs up Simple problem

Hi

There are some simple mistakes .......
look at the filename currentdiroutputtemp..
Use backquote instead of " to capture the output of a command

have a good day

Code:
 

du $1 > currentdiroutputtemp.txt
FILESIZE=`awk '{print $1}' currentdiroutputtemp.txt`
echo $FILESIZE
 
Old 10-30-2008, 04:43 AM   #3
Jurrian
Member
 
Registered: Sep 2008
Posts: 58

Original Poster
Rep: Reputation: 15
Hi,

Thanx for answer, but it is not completely working yet. This is my script:

Code:
touch currentdirouttemp.txt
touch finaloutput.txt
echo "du $1 > currentdiroutputtemp.txt"
du $1 > currentdiroutputtemp.txt
FILESIZE=`awk '{print $1}' currentdirouttemp.txt`
echo $FILESIZE
$FILESIZE > finaloutput.txt
rm currentdirouttemp.txt
this is output:
[jurrian@Jurrian test]$ ./size.sh size.sh
du size.sh > currentdiroutputtemp.txt

[jurrian@Jurrian test]$

for some reason the FILESIZE variable isn't parsed correctly, what mistake do I make?
 
Old 10-30-2008, 04:43 AM   #4
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285
I don't get what is this supposed to do:

Code:
echo "du $1 > currentdiroutputtemp.txt"

Last edited by H_TeXMeX_H; 10-30-2008 at 04:46 AM.
 
Old 10-30-2008, 04:46 AM   #5
Jurrian
Member
 
Registered: Sep 2008
Posts: 58

Original Poster
Rep: Reputation: 15
When I change the backquotes with $() I still get the same error that my FILESIZE is empty..
 
Old 10-30-2008, 04:49 AM   #6
maradnus
Member
 
Registered: Oct 2008
Location: Yellagiri Hills
Distribution: Fedora
Posts: 87

Rep: Reputation: 15
The problem is in the filename: currentdirouttemp.txt

FILESIZE=`awk '{print $1}' currentdirouttemp.txt`

where put is missing.

and

echo $FILESIZE
$FILESIZE > finaloutput.txt

instead of that

echo $FILESIZE > finaloutput.txt

good
 
Old 10-30-2008, 04:53 AM   #7
Jurrian
Member
 
Registered: Sep 2008
Posts: 58

Original Poster
Rep: Reputation: 15
thanks for your help. What is exact the difference between the backquotes and $()? Why should I try to avoid this, or is this nonsense?
 
Old 10-30-2008, 04:54 AM   #8
Jurrian
Member
 
Registered: Sep 2008
Posts: 58

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by H_TeXMeX_H View Post
I don't get what is this supposed to do:

Code:
echo "du $1 > currentdiroutputtemp.txt"
Since I am testing the script, I always like to know what it did, so I echo it to the command line, to be sure no type errors or something exist.
 
Old 10-30-2008, 04:58 AM   #9
maradnus
Member
 
Registered: Oct 2008
Location: Yellagiri Hills
Distribution: Fedora
Posts: 87

Rep: Reputation: 15
Same purpose

hi

both $() and `` are used for the same purpose.

thanks
 
Old 10-30-2008, 05:16 AM   #10
Jurrian
Member
 
Registered: Sep 2008
Posts: 58

Original Poster
Rep: Reputation: 15
ok, thank you for your help
 
Old 10-30-2008, 05:19 AM   #11
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
Why not simply pipe the output of du into awk?
Code:
du "${1:-.}" | awk '{print $1}'
 
Old 10-30-2008, 06:06 AM   #12
Jurrian
Member
 
Registered: Sep 2008
Posts: 58

Original Poster
Rep: Reputation: 15
because I didn't know it was possible

could you explain the part "${1:-.}" to me? What if I just want to add a path instead of letting my script be parsed when an argument is passed,
would that become du /path | awk '{print $1}' ?
 
Old 10-30-2008, 06:28 AM   #13
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
The "${1:-.}" part will either use the first argument when calling the script; or if there is no argument, use "." which is the current directory instead. The "." becomes a default argument.
 
Old 10-30-2008, 08:09 PM   #14
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Incidentally, the 'touch' cmds are redundant.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
awk / perl script help cmontr Programming 3 05-22-2008 05:15 PM
awk script help pls cmontr Programming 5 10-24-2007 11:49 PM
what does this awk script do? sharathkv25 Programming 3 03-08-2007 04:10 PM
About awk script sachin_keluskar Linux - Software 2 06-24-2005 04:19 AM
Passing variables from AWK script to my shell script BigLarry Programming 1 06-12-2004 05:32 AM


All times are GMT -5. The time now is 07:10 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration