LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-16-2011, 11:28 AM   #1
vinay007
LQ Newbie
 
Registered: Aug 2011
Posts: 7

Rep: Reputation: Disabled
Unhappy problem while comparing awk field variable with input variable entered using keyboard


echo "\nplease enter the day\c"

read day
(I am entering 8)

echo "\nplease enter the Month\c"

read month
(I am entering Aug)


ls -lrt *dagknbb*US |awk '$7==$day'|awk '$7=="$month"

nothing is happening it cant able to select particular
day and month file

Is this comparison Incorrect

awk '$7==$day'

I am not getting any errors but the code is not working

please help...................
 
Old 08-16-2011, 12:01 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Please use [code][/code] tags around your code, to preserve formatting and to improve readability.


Shell variables are NOT awk variables! They may look the same at first glance, but awk uses a separate variable system with a slightly different syntax.

You have to import the bash variable into an awk variable before you can use it. (It's also possible by using careful quoting of the awk expression to allow shell expansion, but it's usually better to import it.

Code:
awk -v awkday="$day" -v awkmonth="$month" '( $7==awkday || $7==awkmonth ) {print}'
Notice first that the values of awk variables are read with the name only. Adding "$" to it references the field of the input text, if the variable contains a number.

Also, you can usually do everything you want in a single awk instance. It's a full scripting language of its own, after all: http://www.grymoire.com/Unix/Awk.html

(BTW, why are you testing the same field for both day and month?)

Finally, however, parsing ls isn't recommended. Perhaps if you explain what your real goal is, we could help you develop a better way to do what you want.

Last edited by David the H.; 08-16-2011 at 12:02 PM.
 
Old 08-19-2011, 02:26 AM   #3
vinay007
LQ Newbie
 
Registered: Aug 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
Thanks david for your suggestion but my script is on solaris and i am getting

awk:syntax error near line 1
awk:bailing out error near line 1

can you please suggest the same above code in SOLARIS
 
Old 08-19-2011, 05:40 AM   #4
vinay007
LQ Newbie
 
Registered: Aug 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
hi david , my goal is based on the key board input i have to get the list of files of specific pattern and date and copy the same to say /files/jjj/ab directory

file pattern
date

should be input from the key board
 
Old 08-19-2011, 08:31 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
It always good to point out when you're using Unix-based applications. Since this is a Linux forum we tend to assume gnu utilities by default. I suppose you aren't using bash either, then?

Could you please post some specific examples of the directory structures, names, and dates that you want to locate?

It seems to me that you'd be better off with a simple find command instead. Just plug the variable into the find regex to get the matches you want. Something like this, perhaps?

Code:
read day

find . -name ".*$day.*" -exec mv "{}" \;
 
Old 08-21-2011, 04:22 AM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,498

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
Quote:
Originally Posted by vinay007 View Post
my script is on solaris and i am getting

awk:syntax error near line 1
awk:bailing out error near line 1

can you please suggest the same above code in SOLARIS
Just a comment about awk and Solaris.
On Solaris 10 and older, /usr/bin/awk is a legacy binary that doesn't comply with standard awk. It is there only to maintain compatibility with old scripts that might still stay around.
For new scripts, you should always use /usr/bin/nawk (for new awk) instead. nawk is faster and complies with POSIX awk. For pure standard compliance, you might also use /usr/xpg4/bin/awk. If you are using Gnu specific extensions, you should instead run something like /usr/sfw/bin/gawk, /usr/local/bin/gawk, /opt/csw/bin/gawk, /usr/gnu/awk, or somewhere else depending on the Solaris release and optional packages possibly installed.
 
Old 08-21-2011, 04:31 AM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Newer version of Solaris have at least bash 3.2+. So why not use the shell?

Code:
read day
read month
for file in *${day}${month}*dagknbb*US 
do
   mv "$file" ....
done
 
Old 08-21-2011, 10:08 AM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,498

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
Quote:
Originally Posted by ghostdog74 View Post
Newer version of Solaris have at least bash 3.2+. So why not use the shell?

Code:
read day
read month
for file in *${day}${month}*dagknbb*US 
do
   mv "$file" ....
done
There is nothing requiring bash in your sample code. On Solaris, the old Bourne shell, the Posix compliant one and ksh will all also run it
 
Old 08-21-2011, 10:14 AM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by jlliagre View Post
There is nothing requiring bash in your sample code. On Solaris, the old Bourne shell, the Posix compliant one and ksh will all also run it
yes, that's true, for my sample that is.
 
Old 08-21-2011, 11:38 PM   #10
vinay007
LQ Newbie
 
Registered: Aug 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
i want to search pattern along with that the file name pattern i am searching should belong to specific date i.e for example Aug 15 it should copy only august 15th files not other month 15 th files
 
Old 08-22-2011, 01:27 AM   #11
vinay007
LQ Newbie
 
Registered: Aug 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
this is what i am doing
echo "\nplease enter the Pattern\c"
read FilePattern
(I will enter *hjhjhjh*)

echo "\nplease enter REgion\c"
read region
( i will enter KR
)

echo "\nplease enter the day\c"
read day
(I am entering 8)

echo "\nplease enter the Month\c"
read month
(I am entering Aug)

then i have to find this $FilePattern$region which will be in the file name(under directory say /jil/jj)

i.e.
*hjhjhjh*KR and copy this to another directory /jkj/dir
 
Old 08-22-2011, 01:28 AM   #12
vinay007
LQ Newbie
 
Registered: Aug 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
by the way i want only specfic date files i.e if in enter $day as 10 and $month as Aug
it should copy only Aug 10 th files

---------- Post added 08-22-11 at 01:28 AM ----------

please help me
 
Old 08-23-2011, 12:44 AM   #13
vinay007
LQ Newbie
 
Registered: Aug 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
I would like to thank David for help........
 
  


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
[SOLVED] awk: how can I assign value to a shell variable inside awk? quanba Programming 6 03-23-2010 02:18 AM
Problem with bash script - variable name within variable name steven.c.banks Linux - Newbie 3 03-10-2009 03:08 AM
AWK a variable Ouptut to a new variable and using the new variable with the old one alertroshannow Linux - Newbie 4 02-16-2009 12:08 AM
Bash Awk Variable problem _hadi_ Programming 5 12-13-2006 12:25 AM
bash - comparing a variable to several values davee Programming 3 05-05-2003 07:26 AM


All times are GMT -5. The time now is 08:32 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration