Operating System - HP-UX
1828676 Members
2223 Online
109984 Solutions
New Discussion

Re: problem with awk script..

 
SOLVED
Go to solution
amonamon
Regular Advisor

problem with awk script..

Hello I am trying to firure what is wrong with this script..but it does not work..here is what I tryed:
nawk -F"|" '
{ mo=substr($1,16,2)
day=substr($1,18,2)}
{print $15"|2007|"mo"|"day;} ' $1 > final

so basicly from my input file $1 with substring function I want to assign mo and day variable and have final file..
maybe it is not right pleace to assign mo and day in this part?? I am using KSH..

thanks..
14 REPLIES 14
James R. Ferguson
Acclaimed Contributor
Solution

Re: problem with awk script..

Hi:

What doesn't work? Using your script and this input:

# echo "...............12142007"|./myscript

...yields:

|2007|12|14

...where './myscript' is as you wrote (using 'awk').

Regards!

...JRF...
Peter Godron
Honored Contributor

Re: problem with awk script..

Hi,
how about:
nawk -F"|" '{ mo=substr($0,16,2); day=substr($0,18,2)} {print $15"|2007|"mo"|"day;} ' $1 > final

May be helpful if you provided input and expected output.
Peter Nikitka
Honored Contributor

Re: problem with awk script..

Hi,

if Peter's tip is ($0 is the complete line of input) not what you mean: Do you want to extract the day+month information from the filename you supply?
Then use, use

nawk -F"|" 'FNR==1 {mo=substr(FILENAME,16,2)
day=substr(FILENAME,18,2)}
{print $15"|2007|"mo"|"day;}' in1 in2 .. >final

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
amonamon
Regular Advisor

Re: problem with awk script..

Peter Nikitka that is what I wanted...Thanks a lot everyone..
Sandman!
Honored Contributor

Re: problem with awk script..

Can you provide sample input and how the output should look like?
amonamon
Regular Advisor

Re: problem with awk script..

again me...:(

now I have file1:

232323|2007|12|09
343434|2007|12|09
565656|2007|12|09
898989|2007|12|09
090909|2007|12|09
..
..

and file 2:

232323
343434
343433
343432
343431
..
..

My result that does not work OK with command grep -v -f file1 file2

and output should be:

232323|2007|12|09
343434|2007|12|09

thanks friends..
James R. Ferguson
Acclaimed Contributor

Re: problem with awk script..

Hi (again):

# grep -f file2 file1

...you have the pattern file and the input file reversed and don't want the '-v'.

Regards!

...JRF...
amonamon
Regular Advisor

Re: problem with awk script..

hmmm i tryed both


grep: illegal option -- f
Usage: grep -hblcnsviw pattern file . . .

I got this error..
Sandman!
Honored Contributor

Re: problem with awk script..

How about using the join(1) command:

# join -1 1 -2 1 -t'|' -o 1.1 1.2 1.3 1.4 file1 file2
spex
Honored Contributor

Re: problem with awk script..

Hi,

Based on the grep error message you provided, it doesn't appear you're using HP-UX (SunOS, perhaps?).

If that's the case, try:

$ /usr/xpg4/bin/grep -f file2 file1

or:

$ grep `cat file2` file1

PCS
amonamon
Regular Advisor

Re: problem with awk script..

sandman it works..but I do not understand a code..I never worked with join..I am trying to gigure it in man join..but it goes slow..if U could explain little..

grep 'cat file1' file2 does not work..

thanks friends..

cheers..
Sandman!
Honored Contributor

Re: problem with awk script..

The join(1) example was based on the input you had provided, i.e.

file1:

232323|2007|12|09
343434|2007|12|09
565656|2007|12|09
898989|2007|12|09
090909|2007|12|09
..
..

file 2:

232323
343434
343433
343432
343431
..
..

# join -1 1 -2 1 -t'|' -o 1.1 1.2 1.3 1.4 file1 file2

The join(1) example above "glues" the two files (file1 and file2) together on the fields that are common to both...which is field 1 as shown by command line options -> "-1 1 -2 1".

The field separator is the pipe character -t'|' (needs to be escaped or quoted as it has special meaning to the shell, and the output should consist of all fields from the first file -> "-o 1.1 1.2 1.3 1.4".

imho...not sure why the grep command by JRF is not working for you. It works perfectly on my HP-UX box. Are you using HP-UX or some other variant?

~hope it helps
Peter Nikitka
Honored Contributor

Re: problem with awk script..

Hi,

of course JRF's solution works for Solaris as well - but you have to use the POSIX compliant binary:

/usr/xpg4/bin/grep -f file2 file1

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
amonamon
Regular Advisor

Re: problem with awk script..

thanks..a lot

yes /usr/xpg4/bin/grep -f file2 file1
works fine on my solaris..

thanks guys,

cheers,