awk - syntax error
I'm learning awk and wrote a simple test program. The desired operation is to read a file of words and output only those which...
- are five characters long - have the same letter in position 2 and 5. A sample input file... Quote:
Quote:
Code:
cat < $InFile \ Daniel B. Martin |
Very close! How about this:
Code:
cat < $InFile | \ |
Hi.
For learning purposes here is another solution Code:
$ awk -F '' 'NF==5 && $2==$5' infile.txt NF -- number of fields; in this case -- length of words. $2, $5 -- second and 5th character in the string. EDIT: 1) Note that if there are no action "{...}" after a pattern (or logical expression) then, by default, awk assumes '{print $0;}' 2) from info gawk: Quote:
|
Quote:
I made the next step by parameterizing this line. Code:
# Parameterize the word length and "must match" character positions. Daniel B. Martin |
Quote:
I made the next step by parameterizing this line. Code:
# Parameterize the word length and "must match" character positions. Daniel B. Martin |
Hi.
Quote:
Code:
$ awk -v WL=5 -v p1=2 -v p2=5 '{ if (length==WL && substr($0,p1,1)==substr($0,p2,1)) print}' infile.txt I'd like to dissuade you from writing awk programs in C (as well as C++ programs in C etc) :) IMHO, each language has it own preferable thought patterns, and you should learn them, not only the syntax. In case of awk, typically, you should think of input data as a sequence of records, each one consists of fields. Using this simple paradigm you can move mountains! |
AWK- Syntax errror
Hi,
I have just started with unix and today started learning awk, this is my first post on this forum and do not know where to start a new thread so posting on this thrad. Problem Details: I tried to run the follwing command ls -l | grep -v total | awk '{ print size is $5 bytes for $2 }' and it showed me this error: syntax error The source line is 1. The error context is { print size is $5 bytes >>> for <<< $2 } awk: The statement cannot be correctly parsed. The source line is 1. while if I running this command : ls -l | grep -v total | awk '{ print size is $5 }' , it runs successfully with output as: 164 17146-rw-r--r-- 1 goyalank users 164 Mar 7 09:20 email -rw-r--r-- 1 goyalank users 17146 Mar 7 13:49 task To mention the detail the output of ls -l is : -rw-r--r-- 1 goyalank users 164 Mar 7 09:20 email -rw-r--r-- 1 goyalank users 17146 Mar 7 13:49 task Any help is appreciable. |
Hi.
You should quote literal strings, like this: Code:
ls -l | grep -v total | awk '{ print "size is "$5" bytes for "$2 }' |
awk syntax
Thanks a ton! That worked......
Can you also tell the step to create a new thead for a particular problem like the one I faced below....thanks again in advance. |
Quote:
|
syntax error for awk
Quote:
although I was fine the first beginning 10 weeks now its getting really hard i have to write two scripts one that tells me when my ip Changes and one that pulls out words out of a dictionary file from my professor and on that one i have to take out greater than or equal to 3 characters and less than or equal to 6 and put them in a empty file i decided to use awk for the dictionary one and i cant figure out the right syntax ive been having trouble ive spent hours upon hours trying to figure these two out been two weeks now anyways it would be great if someone can help me please heres what i started out with awk 'length 6=< && >=3 {printf "%d. %s\n"}' test > test1 cat test1 thanks guys |
Quote:
Code:
Honda Code:
awk '{if (3<length($0) && length($0)<7) print}' $InFile >$OutFile Code:
Honda |
do not use:
cat < file | awk 'something' > newfile but awk 'something' file > newfile another comment: the usual syntax of awk is: awk ' condition { action } ', so: awk '3<length($0) && length($0)<7' $Infile > $OutFile will probably work (the default action is print, you can omit that. |
I like this for readability (previously posted) ...
Code:
awk '{if (3<length($0) && length($0)<7) print}' $InFile >$OutFile Code:
awk '{if (length($0)~/[3456]/) print}' $InFile >$OutFile |
I agree with you, but the usual syntax of awk is what I wrote you:
awk ' condition { action } ' and using the usual syntax will not influence readability. Your original post: awk '{length($0)=5 && substr($0,2,1)==substr($0,5,1)}{print}' should work too, you only need to remove { } . |
sorry i havent got back to you guys in a while but, thank you very much for the replies they were very helpful and i actutaly got the dictionary file one to work, and i totally thank you guys and all of your help thanks again and have a good one
|
I botched the test and mistakenly thought my grep solution was good. It isn't. Solution withdrawn. Lesson learned.
Daniel B. Martin |
Quote:
Code:
$ grep ".{4,7}" input |
All times are GMT -5. The time now is 10:21 AM. |