- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: need some script help please
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2004 07:17 AM
05-17-2004 07:17 AM
need some script help please
See below:
I am trying to print two values to one line in a file. After the values are read into the file, I will cat the file and read the new values.
% cat citylist.txt
Columbus, OH 1999
Travis, TX 1998
Methuen, MA 2001
Salem, NH 2004
CITY=`cat citylist | awk '{print $1}'
CDATE=`cat citylist.txt | awk '{print $3}'
TWO=2
export CITY CDATE TWO
# I want to subtract field 2 ($2) by 2 years and then place the new value in a new file.
((NEWDATE=$CDATE-$TWO))
printf "%s %s \n" $CITY $NEWDATE > /tmp/newdate
cat /tmp/newdate
Columbus, OH 1997
Travis, TX 1996
Methuen, MA 1999
Salem, NH 2002
But this doesn't work. Where am I going wrong?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2004 07:25 AM
05-17-2004 07:25 AM
Re: need some script help please
your script seems fine to me, but when you print the NEWDATE as %s, you might want to treat it as a %d.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2004 07:29 AM
05-17-2004 07:29 AM
Re: need some script help please
If I understand your problem correctly you
are missing the "," separator. The citylist.txt has 2 fields not 3.
Thus when you do a
cat citylist.txt | awk '{ print $3 }'
it will throw a blank space.
You need to modify your code to take into
account the field separator ",".
Replace $3 with $2 in your code and check.
-Siddhartha
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2004 07:38 AM
05-17-2004 07:38 AM
Re: need some script help please
Your command to get the city and state into the CITY variable actually will just grab the first word of the city. Using $2 will pick up the state, but only if your city names are all one word. Plus, you might want to put it into a loop so that you can work on the values of each line as you read them.
You could do something like this:
while read line
do
CITY=`echo $line | awk '{print substr($0, 1, length($0)-4}'`
CDATE=`echo $line | awk '{print $NF}'
((NEWDATE=$CDATE-$TWO))
printf ...(etc.)
done
Save that as a shell script and run it with the citylist.txt as input.
It's not pretty but it might do what you are looking for.
JP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2004 07:48 AM
05-17-2004 07:48 AM
Re: need some script help please
cat citylist.txt | awk '
/NF > 2/ {$3=$3-2;print $0;
}' > /tmp/newdate
you might want to add some error checking to see if $3 is actually a number.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2004 07:50 AM
05-17-2004 07:50 AM
Re: need some script help please
#!/bin/sh
#set -x
# name=testscript
#
while read line
do
CITY=`echo $line | awk '{print substr($0, 1, length($0)-4}'`
CDATE=`echo $line | awk '{print $NF}'`
TWO=2
export CITY CDATE TWO
((NEWDATE=$CDATE-$TWO))
printf '%s %s \n' $CITY $NEWDATE > newdate.txt
done
When I kick if off, it just sits at the prompt. Any other ideas?
Curt - I'll look into your suggestion now.
Points to come ...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2004 08:02 AM
05-17-2004 08:02 AM
Re: need some script help please
Tom - what is %d as opposed to %s?
Curt - your idea seems to do something and then outputs an empty newdate file.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2004 08:04 AM
05-17-2004 08:04 AM
Re: need some script help please
TWO=2
while read CITY CDATE
do
((NEWDATE=$CDATE-$TWO))
printf '%s %d \n' $CITY $NEWDATE
done < citylist.txt > /tmp/newdate.txt
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2004 08:06 AM
05-17-2004 08:06 AM
Re: need some script help please
cat citylist.txt | awk '
/NF > 1/ {$2=$2-2;print $0;
}' > /tmp/newdate
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2004 08:08 AM
05-17-2004 08:08 AM
Re: need some script help please
The %d stays for 'd'ecimal value. the %s as you know, stays for 's'tring value.
str="abc"
printf "%s", str
foo=123456
printf "%d", foo
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2004 08:08 AM
05-17-2004 08:08 AM
Re: need some script help please
If you named your script 'testscript', try it like this:
./testscript
JP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2004 08:17 AM
05-17-2004 08:17 AM
Re: need some script help please
CDATE=`cat citylist | awk '{print $3}'`
CITY=`cat citylist | awk '{print $1}'`
TWO=2
while read CITY CDATE
do
((NEWDATE=$CDATE-$TWO))
printf '%s %d \n' $CITY $NEWDATE
done < citylist > newdate.txt
which give me this:
%cat newdate.txt
Columbus,OH 0
Travis,TX 0
Methuen,MA 0
Salem,NH 0
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2004 08:21 AM
05-17-2004 08:21 AM
Re: need some script help please
((NEWDATE=$CDATE-$TWO))
do
NEWDATE=$(($CDATE - $TWO))
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2004 08:22 AM
05-17-2004 08:22 AM
Re: need some script help please
And the awk statement to read $2 instead of $3. But still same problem.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2004 08:27 AM
05-17-2004 08:27 AM
Re: need some script help please
CDATE=`cat citylist | awk '{print $2}'`
CITY=`cat citylist | awk '{print $1}'`
TWO=2
while read CITY CDATE
do
NEWDATE=$(($CDATE - $TWO))
printf '%s %d \n' $CITY $NEWDATE
done < citylist > newdate.txt
now I get:
./testscript2: syntax error at line 10: `NEWDATE=$' unexpected
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2004 08:28 AM
05-17-2004 08:28 AM
Re: need some script help please
cat citylist.txt | awk '{print $2;}'
you should be getting the year
they try:
cat citylist.txt | awk '{a=$2-2;print $a;}'
then:
cat citylist.txt | awk '{$2=$2-2;print $2;}'
then
cat citylist.txt | awk '{$2=$2-2;print $0;}'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2004 08:32 AM
05-17-2004 08:32 AM
Re: need some script help please
you can comment these lines out as they are unnecessary
CDATE=`cat citylist | awk '{print $2}'`
CITY=`cat citylist | awk '{print $1}'`
to
#CDATE=`cat citylist | awk '{print $2}'`
#CITY=`cat citylist | awk '{print $1}'`
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2004 08:32 AM
05-17-2004 08:32 AM
Re: need some script help please
awk '{$NF=$NF-2; print $0;}' citylist.txt
If that looks good, you can redirect the output to the /tmp/newdate file like this:
awk '{$NF=$NF-2; print $0;}' citylist.txt >/tmp/newdate
JP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2004 08:33 AM
05-17-2004 08:33 AM
Re: need some script help please
$cat citylist
Columbus,OH 1999
Travis,TX 1998
Methuen,MA 2001
Salem,NH 2004
$
$cat newdate.txt
cat: Cannot open newdate.txt: No such file or directory
$
$cat itrc
#!/bin/sh
CDATE=`cat citylist | awk '{print $2}'`
CITY=`cat citylist | awk '{print $1}'`
TWO=2
while read CITY CDATE
do
((NEWDATE=$CDATE-$TWO))
printf '%s %d \n' $CITY $NEWDATE
done < citylist > newdate.txt
$
$
$sh itrc
$
$cat newdate.txt
Columbus,OH 1997
Travis,TX 1996
Methuen,MA 1999
Salem,NH 2002
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2004 08:42 AM
05-17-2004 08:42 AM
Re: need some script help please
New Orleans, LA 1999
The NF variable in awk returns the number of fields read on a line, and the $NF variable is the value of the last variable on the line. If you use $NF in Curt's awk example, you can be sure that you will always get the year, no matter what the city name is.
Specifying the input file on the command line with awk saves using cat and piping it to awk.
JP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2004 08:47 AM
05-17-2004 08:47 AM
Re: need some script help please
perl -pe 's/ (\d+)/$1-2/e'
HTH
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2004 08:53 AM
05-17-2004 08:53 AM
Re: need some script help please
# perl -pe 's/ (\d+)/$1-2/e' < citylist >/tmp/newdate
# more /tmp/newdate
Columbus,OH1997
Travis,TX1996
Methuen,MA1999
Salem,NH2002
How do I get a space between the city and year using the perl approach?
To the others - I got your suggestions to work but I was trying to use korn shell and it was giving me difficulties with that.
I'll post my results soon.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2004 09:02 AM
05-17-2004 09:02 AM
Re: need some script help please
perl -pe 's/ (\d+)/$1-2/e'
aaaaaaaaaaaaaaaaaa^
perl -pe 's/ (\d+)/ $1-2/e'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2004 09:08 AM
05-17-2004 09:08 AM
Re: need some script help please
How do I get City,State from one file and the year from another file onto the same line, separated by a space in a third file (citylist)?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2004 09:13 AM
05-17-2004 09:13 AM
Re: need some script help please
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=592643
paste file1 file2 > file3