Operating System - HP-UX
1835251 Members
2289 Online
110078 Solutions
New Discussion

need some script help please

 
Kristopher March
Regular Advisor

need some script help please

How can I place the value of two variables into one file on the same line?

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?
"This ain't no burger flippin job!"
27 REPLIES 27
D Block 2
Respected Contributor

Re: need some script help please

I could be wrong, but your first line in your INPUT file appears to be a blank-line.

your script seems fine to me, but when you print the NEWDATE as %s, you might want to treat it as a %d.

Golf is a Good Walk Spoiled, Mark Twain.
Siddhartha M
Frequent Advisor

Re: need some script help please

Hi Kristopher,

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
John Poff
Honored Contributor

Re: need some script help please

Hi,

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
curt larson_1
Honored Contributor

Re: need some script help please

why don't you just do your calculation in awk. it will be easier and faster

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.
Kristopher March
Regular Advisor

Re: need some script help please

John - this is for you. Here's what I've got:

#!/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 ...
"This ain't no burger flippin job!"
Kristopher March
Regular Advisor

Re: need some script help please

Siddhartha - I can awk $3 of citylist.txt and get the year back. So I think that part is working.

Tom - what is %d as opposed to %s?

Curt - your idea seems to do something and then outputs an empty newdate file.
"This ain't no burger flippin job!"
curt larson_1
Honored Contributor

Re: need some script help please

if your going to do it in the shell, this would be better

TWO=2
while read CITY CDATE
do
((NEWDATE=$CDATE-$TWO))
printf '%s %d \n' $CITY $NEWDATE
done < citylist.txt > /tmp/newdate.txt

curt larson_1
Honored Contributor

Re: need some script help please

my awk script might be off by 1 field, try

cat citylist.txt | awk '
/NF > 1/ {$2=$2-2;print $0;
}' > /tmp/newdate
D Block 2
Respected Contributor

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

Golf is a Good Walk Spoiled, Mark Twain.
John Poff
Honored Contributor

Re: need some script help please

Kristopher,

If you named your script 'testscript', try it like this:

./testscript
JP
Kristopher March
Regular Advisor

Re: need some script help please

Curt - have a look:

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

"This ain't no burger flippin job!"
curt larson_1
Honored Contributor

Re: need some script help please

instead of
((NEWDATE=$CDATE-$TWO))

do

NEWDATE=$(($CDATE - $TWO))
Kristopher March
Regular Advisor

Re: need some script help please

I changed citlist to look only have two fields. plymouth,ma 2004

And the awk statement to read $2 instead of $3. But still same problem.
"This ain't no burger flippin job!"
Kristopher March
Regular Advisor

Re: need some script help please

updated script:

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


"This ain't no burger flippin job!"
curt larson_1
Honored Contributor

Re: need some script help please

try this:

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;}'
curt larson_1
Honored Contributor

Re: need some script help please

you have a typo somewhere

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}'`
John Poff
Honored Contributor

Re: need some script help please

Curt has you on the right track. Try this:

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
Siddhartha M
Frequent Advisor

Re: need some script help please

Check this:(seems to produce the desired output)

$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
John Poff
Honored Contributor

Re: need some script help please

The trick with using $2 and $3 is ok if you only have city names that are one word. But what happens if your file includes?:

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
Rodney Hills
Honored Contributor

Re: need some script help please

Here's a real simple way using perl-

perl -pe 's/ (\d+)/$1-2/e' /tmp/newdate

HTH

-- Rod Hills
There be dragons...
Kristopher March
Regular Advisor

Re: need some script help please

The perl line does work:

# 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.
"This ain't no burger flippin job!"
curt larson_1
Honored Contributor

Re: need some script help please

put a space after / and before the $1

perl -pe 's/ (\d+)/$1-2/e'
aaaaaaaaaaaaaaaaaa^

perl -pe 's/ (\d+)/ $1-2/e'
Kristopher March
Regular Advisor

Re: need some script help please

Great! Here's my next question:

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)?

"This ain't no burger flippin job!"
curt larson_1
Honored Contributor

Re: need some script help please

read yesterday's questions:

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=592643

paste file1 file2 > file3