The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Script for reading filelist and preparing param file. thebeginer UNIX for Dummies Questions & Answers 2 10-05-2007 05:19 PM
chmod - pass a filelist as a parameter? alangibson UNIX for Dummies Questions & Answers 2 04-11-2007 09:44 AM
how to get nth file filelist balireddy_77 Shell Programming and Scripting 1 11-20-2006 02:17 AM
Help with for loop within two scripts chiru_h Shell Programming and Scripting 3 09-08-2006 11:38 AM
How to combine "find" command in for each loop (tcsh) umen Shell Programming and Scripting 3 08-22-2005 05:07 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-17-2007
LAKSHMI NARAYAN LAKSHMI NARAYAN is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 38
i want to combine two awk scripts which is having same loop and filelist

hi,
to all
give me some idea below scripts

file=`cat filelist`
for filename in $file
do
awk '{
if ($0 ~ /system/ && flag == 0) {
flag=1;
print $0;
}
else if ($0 ~ /system/ && flag == 1) {
printf("%s SYSLAY %s %s %s\n",$1,$3, $4, $5);
}
else
print $0;
}' $filename > temp.txt
mv temp.txt $filename
done

for this script1 : i have to add a new line which contain syslay for the filelist containing system. and that system occurs two times so after the second occurence i have to add new line . i have done like this if there is any other solution of this please send the solution.



files=`cat filelist`
for filename in $files
do
awk '{
if ( $0 ~ /narayana / && flag == 0 ) {
flag=1
print $0;
}
else if ( $0 ~ /narayana/ && flag == 1 ) {
printf("%s %s /narayana/mde\n",$1,$2);
}
else
print $0;
}' $filename > temp.txt
mv temp.txt $filename.tmp
done


script2: i have to add '/mde' for narayana as narayana/mde for the files in the filelist . And for this also for the second occurence of the line containing narayana only should be changed .

i have to merge these two scripts as one so kindly help in this matter urgently


example;

this is not a typical example
narayana
solution is not verified
this section is to be changed
narayana
sos ana s plaea

the above is example of the file
and it should be modiefied as



this is not a typical example
narayana/mde
solution is not verified
this section is to be changed
narayana/mde
sos ana s plaea



with regards
narayana
  #2 (permalink)  
Old 06-17-2007
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,428
You can do something like that :
Code:
file=`cat filelist`
for filename in $file
do
   awk '
      /system/ {
         if (flag_system) {
            printf("%s SYSLAY %s %s %s\n",$1,$3, $4, $5);
            next;
         }
         flag_system = 1;
      }
      /narayana/ {
         if (flag_narayana) {
            printf("%s %s /narayana/mde\n",$1,$2);
            next;
         }
         flag_narayana = 1;
      }
      { print $0 }
   ' $filename > temp.txt
   mv temp.txt $filename
done
  #3 (permalink)  
Old 06-17-2007
LAKSHMI NARAYAN LAKSHMI NARAYAN is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 38
thanks for solving the problem

i am very much thankful to you
i have run that script and that was working.
and i have lot of doubts regarding using flags
if time availabe can u explain in detail of the script


thanks

regards
  #4 (permalink)  
Old 06-17-2007
LAKSHMI NARAYAN LAKSHMI NARAYAN is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 38
hey thanks one more doubt

hi

how to modify for the third or fourth occurence of the pattern
  #5 (permalink)  
Old 06-17-2007
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,428
The following version of the script modifies lines :
  • First and following occurences of 'system.
  • Third and following occurences of 'narayana'
Code:
file=`cat filelist`
for filename in $file
do
   awk '
      BEGIN {
         system_occ   = 1;
         narayana_occ = 3;
      }
      /system/ {
         ++flag_system;
         if (flag_system >= system_occ) {
            printf("%s SYSLAY %s %s %s\n",$1,$3, $4, $5);
            next;
         }
      }
      /narayana/ {
         ++flag_narayana;
         if (flag_narayana >= narayana_occ) {
            printf("%s %s /narayana/mde\n",$1,$2);
            next;
         }
      }
      1
   ' $filename > temp.txt
   mv temp.txt $filename
done
  #6 (permalink)  
Old 06-19-2007
LAKSHMI NARAYAN LAKSHMI NARAYAN is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 38
again i have to add another script for this

for the above script which u have given the solution for the solution i have to add another script also

for the below file i have remove a comment after the line narayana


this is not a typical example
#narayana
#solution is not verified
#this section is to be changed
#narayanae
sos ana s plaea

this is not a typical example
narayana
solution is not verified
this section is to be changed
narayana
sos ana s plaea

please kindly give the solution

with regards
narayana
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:16 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0