awk

redhat linux 8
how can I get the following code to print a file without printing the same line twice;;;
when there are 12 lines (ex)
ex;
awk 'NR ==8{"date"|getline d; printf "%s\t%s%s\n", "(profile file)", $0, "("d")"}{print}' file1

i am looking at this from a dataprocessing angle and the answer for some unknown reason has totally escaped me! what I would like to say is that I know about redirection.. so if it is a mater of redirecting out input thats not a problem.. but if there is a way to make it work in this aprticular way I would like to know.. thanks!
anyone?!:smiley:

Sort the file then pipe it to uniq. You can then parse it with awk

check if your sort command offers the -u switch. this incorporates uniq into the sort command.

thanks dudes! i just have one more question ! using awk to process a paragraph in file... am I going to get this result and have to use sort or is there an actual way around that!:rolleyes:

i dont know awk very well so i cant directly answer your question.

i suppose i am not really understanding your inital question.

i read "how can I get the following code to print a file without printing the same line twice;;;"

w/ only looking at that i assume the order of the items in your file is not important.

if you have to preserve the order of the file we can rule out sort.

i would think the next best thing would be to load your file into an array and test each element got uniqness agest the rest of the array.

if the order is not important then awk out what you need and pipe it to sort -u and wammo you have only uniq lines going to stdout.

I think that I finally see what your problem is. It's not that you input file has duplicate lines that you want to suppress. Your awk script is outputing line 8 twice. At the end of your awk script, change {print} to NR!=8{print}

thanks ! it worked! this is incredible.. i didn't know that it was possible to piece together a command that way.. this is definitely a break through for me!:smiley: :slight_smile:

:frowning: red hat linux 8.0
I tried this simple script no lucK
echo "read number:\c:"
read number
echo "read name:\c"
read name
NR == $num{"date"|getline d; printf "%s\t%s%s\n", "profile file", $0, d}NR!=$num{print}
.......
run_file!
awk -f won't work even when I change the script..
and awk 'NR == $num.............. wont work either and the error message says taht fild Zero is name of script file.. please help[!!!!:confused:

Pass variable to awk with -v option, e.g...

awk -v num=$number 'NR==num{print "blah"}' file1

:rolleyes: not quite the results that I am looking for!

Just do it when you want to use these external variables in BEGIN procedure too.

Remember, variable's reference can't be prefixed with $ unless they are position variables like $0~$NF.
Ygor is right. However, you didn't modify your variable reference, so you failed.
Just skip the $, use "num".

By the way, when you want to print patterned line without twice display, just improve your structure like me.

[root@home root]# cat myawk8
#!/bin/gawk
{
        if(NR==num){
                "date"|getline d;
                printf "%s\t%s%s\n","profile file",$0,d
                next
        }
        print
}
[root@home root]# cat mydata
hi, i am home_king from linuxsir.org.
2003-12-5 Bash it.
I am shell's administrator.
[root@home root]# num=2
[root@home root]# gawk -f myawk8 num=$num mydata
hi, i am home_king from linuxsir.org.
profile file    2003-12-5 Bash it.��  4�� 16 01:44:50 CST 2004
I am shell's administrator.