using the $1 $2 etc for words in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting using the $1 $2 etc for words in awk
# 8  
Old 07-22-2010
Thanks man.

Quote:
Originally Posted by radoulov
This is scottn's code, not mine.
The 1 simply means true, which in the pattern/expression part causes the execution of the default action, which is print the current record.
Thanks man but one query that only print is a default command or any thing else?
# 9  
Old 07-22-2010
Quote:
Originally Posted by scottn
Or, perhaps:
Code:
$ echo 1 2 3 4 5 6 | awk '{if( NF > 2) { $3 = RS $3}}1'
1 2
3 4 5 6

Good stuff, but I actually need this printing stuff in an else if in my script, and I have about 3 else if's, and just substituting the command into it doesn't work! Could you show me how you would do it?

I have something like:

Code:
/usr/bin/nawk '
{if ( NF>2 ){
     if ($3 ~/and/){
         print $0}
    if ($3 ~/do/){
        print $0}}
else if ($3 ~/and/){
        {$4 = RS $4}}1
else if ($3 ~/do/){
        {$4 = RS $4}}1
else if ($3 ~/\./){
        {$3 = RS $3}}1
 
}
 
else{
print $0}
 
}' filename.txt


NOTE: im using Solaris

PLEASE HELP! thanks!
# 10  
Old 07-22-2010
Quote:
Originally Posted by linuxkid
Good stuff, but I actually need this printing stuff in an else if in my script, and I have about 3 else if's, and just substituting the command into it doesn't work! Could you show me how you would do it?

I have something like:

Code:
/usr/bin/nawk '
{if ( NF>2 ){
     if ($3 ~/and/){
         print $0}
    if ($3 ~/do/){
        print $0}}
else if ($3 ~/and/){
        {$4 = RS $4}}1
else if ($3 ~/do/){
        {$4 = RS $4}}1
else if ($3 ~/\./){
        {$3 = RS $3}}1
 
}
 
else{
print $0}
 
}' filename.txt

NOTE: im using Solaris

PLEASE HELP! thanks!
Hi.

Originally I posted:
Code:
$ echo 1 2 3 4 5 6 | awk '{if( NF > 2) { $3 = RS $3; print} }'

but changed it. Maybe that is closer to what you needed?

I have to be honest... I don't fully understand your logic (and overuse of curly brackets!), but this is what I think you were looking for:
Code:
/usr/bin/nawk '{
  if ( NF>2 ) {
    if ( ($3 ~ /and/) || ($3 ~ /do/) )
      $4 = RS $4
    else if ($3 ~ /\./)
      $3 = RS $3
    print
  } else
    print
}' filename.txt


Last edited by Scott; 07-22-2010 at 11:06 AM.. Reason: Slight change to code
# 11  
Old 07-22-2010
Hammer & Screwdriver

Quote:
Originally Posted by scottn
Hi.
I have to be honest... I don't fully understand your logic (and overuse of curly brackets!), but this is what I think you were looking for:
Neither do i! as they say, im "yung and lernin". but thanks anyway, from your solution I managed to work it out all on my own (with a bit of your help though ofcourse!)
# 12  
Old 07-22-2010
Quote:
Originally Posted by linuxkid
Neither do i! as they say, im "yung and lernin". but thanks anyway, from your solution I managed to work it out all on my own (with a bit of your help though ofcourse!)
Smilie

Nice one!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Permutation Words in awk

i have 13 different words. I need to get permutations like all combinations of this words: word1 word2 word3 word4 word5 word6 word7 word8 word9 word10 word11 word12 word13 But the combinations only should be 12 words long. Is there a fast efficient way to do this? Maybe with linux tool... (1 Reply)
Discussion started by: watcherpro
1 Replies

2. Programming

[awk]Chinese words!!

Is there a way to extract chinese words from a text written in an European Language? I want to create a glossary and finding a way would make me save time! Thank you! (3 Replies)
Discussion started by: ettore8888
3 Replies

3. Shell Programming and Scripting

AWK count letters words

Hi All! can anyone help me with this code? I want to count words or letters in every line with if(count>20){else echo $myline} awk '/<script /{p=1} /<\/script>/{p=0; next}!p' index.html | while read myline; do echo $myline done Thank you !!! (3 Replies)
Discussion started by: sanantonio7777
3 Replies

4. UNIX for Advanced & Expert Users

Need help either with awk or sed to get text between words

Hello All, My requirement is to get test between two words START & END, something like html tags Eg. Input file: START Line1 Line2 Line3 CLOSE START Line4 Line5 Line6 END START Line7 START Line8 (7 Replies)
Discussion started by: konerusuneel
7 Replies

5. Shell Programming and Scripting

search for a pattern using awk between two words

Hi, how can we search for a pattren between two words? below are the examples input 1)select from table_name c1,c2,c3,c4,fn(),fn2(),c5;-->false 2)select from table_name c1,c2,c3,c4;--True 3)select from table c1, c2, c3, fn(), c4;-->true 4)select from table_name c1, c2, c3;-->true... (11 Replies)
Discussion started by: manasa_vs
11 Replies

6. Shell Programming and Scripting

search several words with awk command

Hello, I want to test if i find the word CACCIA AND idlck in a file, i have to print a message Ok. For that , i need to user a awk command with a && logical. Can you help me ? :confused: ### CACCIA: DEBUT ### if $(grep -wqi "$2" /etc/passwd); then && rm /etc/security/.idlck ... (3 Replies)
Discussion started by: khalidou13
3 Replies

7. Shell Programming and Scripting

How to get a known word between two known words using awk

hi I have posted it earlier but i was unable to put my exact problem.This time posting in parts. I have a text file which i had transferred to UNIX.It has strings like: alter table table_name add (column_name); as well as modify options. now i need to read the table name between alter... (3 Replies)
Discussion started by: alisha
3 Replies

8. Shell Programming and Scripting

awk after words

hi sorry, newbie for scripting.. the text file: abcdefghijk%%$%^U^%234454234 I got awk script: awk ' ($1 == "abcd") the output only show: abcd I tried using asterix: awk ' ($1 == "abcd*") but same output abcd only... how can I get all of the line ?? (6 Replies)
Discussion started by: flekzout
6 Replies

9. Shell Programming and Scripting

Extract numbers below words with awk

Hi all, Please some help over here. I have a Sales.txt file containing info in blocks for every sold product in the pattern showed below (only for 2 products). NEW BLOCK SALE DATA PRODUCT SERIAL 79833269999 146701011945004 .Some other data .Some... (17 Replies)
Discussion started by: cgkmal
17 Replies

10. UNIX for Dummies Questions & Answers

merging 2 lines with awk and stripping first two words

Hey all i am pretty new to awk... here my problem. My input is something like this: type: NSR client; name: pegasus; save set: /, /var, /part, /part/part2, /testpartition, /foo/bar,... (9 Replies)
Discussion started by: bazzed
9 Replies
Login or Register to Ask a Question