LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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, 02: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, 03: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, 03: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, 03:43 AM   #4
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
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 03:46 AM.
 
Old 10-30-2008, 03: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, 03: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, 03: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, 03: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, 03: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, 04: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, 04:19 AM   #11
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Why not simply pipe the output of du into awk?
Code:
du "${1:-.}" | awk '{print $1}'
 
Old 10-30-2008, 05: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, 05:28 AM   #13
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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, 07:09 PM   #14
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

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


Reply



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 04:15 PM
awk script help pls cmontr Programming 5 10-24-2007 10:49 PM
what does this awk script do? sharathkv25 Programming 3 03-08-2007 03:10 PM
About awk script sachin_keluskar Linux - Software 2 06-24-2005 03:19 AM
Passing variables from AWK script to my shell script BigLarry Programming 1 06-12-2004 04:32 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:23 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
Open Source Consulting | Domain Registration