i want to combine two awk scripts which is having same loop and filelist


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting i want to combine two awk scripts which is having same loop and filelist
# 1  
Old 06-17-2007
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  
Old 06-17-2007
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  
Old 06-17-2007
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  
Old 06-17-2007
hey thanks one more doubt

hi

how to modify for the third or fourth occurence of the pattern
# 5  
Old 06-17-2007
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  
Old 06-19-2007
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
# 7  
Old 06-19-2007
The following version of the script removes all consecutive comments after the lines containing 'narayana'
Code:
file=`cat filelist`
for filename in $file
do
   awk '
      BEGIN {
         system_occ   = 1;
         narayana_occ = 3;
      }
      uncomment  {
         if (sub(/^#/, "")) {
            print;
            next;
         }
         else
            uncomment = 0;
      }
      /system/ {
         ++flag_system;
         if (flag_system >= system_occ) {
            printf("%s SYSLAY %s %s %s\n",$1,$3, $4, $5);
            next;
         }
      }
      /narayana/ {
         ++flag_narayana;
         uncomment = 1;
         if (flag_narayana >= narayana_occ) {
            printf("%s %s /narayana/mde\n",$1,$2);
            next;
         }
      }
      1
   ' $filename > temp.txt
   mv temp.txt $filename
done

If you want to removes comments only after occurence 'narayana_occ' :
Code:
      /narayana/ {
         ++flag_narayana;
         if (flag_narayana >= narayana_occ) {
            uncomment = 1;
            printf("%s %s /narayana/mde\n",$1,$2);
            next;
         }
      }

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Combine awk scripts

Hi, Below command is working as expected, but would like to know how to club the two AWK scripts in the command into one echo -e "MMS000101S0203430A|20180412E|\nMMB0001INVESTMENT||107-86193-01-03|\nMMB0001FUND||107-86193-04-01|\nMMC9991 " | awk -F'|' -v OFS=, '/^MMC9991/{print r"|"s,t; next}... (3 Replies)
Discussion started by: JSKOBS
3 Replies

2. Shell Programming and Scripting

Combine awk statement into one

awk '{a=$0} END {while (NR) print a}' mail.log | awk '/Feb 2/ {print ;exit;}' first, can this code be combined into one? what i want this code to do is output every single line it finds up until the last occurrence of "Feb 2". right now, this code simply outputs one line, and aborts. ... (12 Replies)
Discussion started by: SkySmart
12 Replies

3. UNIX for Dummies Questions & Answers

Combine two awk statements into one

Hi, I have the following two awk statements which I'd like to consolidate into one by piping the output from the first into the second awk statement (rather than having to write kat.txt out to a file and then reading back in). awk 'BEGIN {FS=OFS=" "} {printf("%s ", $2);for (x=7; x<=10;... (3 Replies)
Discussion started by: kasan0
3 Replies

4. Shell Programming and Scripting

awk filelist containing strange characters

I've written a script: find -depth | awk ‘ { if ( substr($1,length($0)-2,3) == “/1.” ) { print $1 } { system(“awk -f test1.awk “ $1 ) } } ‘ The idea is that it trundles through a large directory structure looking for files which are named '1.' and then... (3 Replies)
Discussion started by: nashcom
3 Replies

5. Shell Programming and Scripting

combine awk and tr -d

Hi Everyone, awk 'BEGIN{print strftime("%c",1272814948)}' | tr -d '\n' how to change tr -d '\n' to be part of the awk? means awk this pchoh time, and awk also remove '\n', instead of using "|" to combine "tr" command. Thanks (2 Replies)
Discussion started by: jimmy_y
2 Replies

6. Shell Programming and Scripting

how to combine two scripts into one

I need to combine two scripts, into one script. One is to delete something, the to recreate. Here is the scripts: BIN=/usr/lbin LINE='device for 0101a01: lpd://172.25.41.111:515' while read LINE do prt=`echo $LINE | awk '{print $3 }'| cut -c 1-7` echo $prt #drv=`echo $LINE | awk -F":"... (4 Replies)
Discussion started by: ggoliath
4 Replies

7. Shell Programming and Scripting

help with a script that will combine two separate scripts for solaris and aix

Hello experts, I have separate scripts (KSH) each for Solaris and AIX to install core applications (e.g. BigBrother). I also have a script called Installer that gives a menu list to select a particular application to install from a bunch of applications. Now I am trying to combine separate... (7 Replies)
Discussion started by: solaix14
7 Replies

8. UNIX for Dummies Questions & Answers

combine two awk codes

Hello How do I combine the following 2 codes in one, the purpose is to not generate the outfile1 awk '{print $9 "\t" $10 "\t" $11}' infile > outfile1 awk '{ gsub(/.fa/,""); print }' outfile1 > outfile2 Thanks Joseph (2 Replies)
Discussion started by: jdhahbi
2 Replies

9. Shell Programming and Scripting

How to combine data files using for loop

Hi, I have 5 files basically;namely file1.txt situated each at folder A to E respectively. I would like to extract out third column from each of these file1.txt from folder A to folder E. Also, I wanted to extract the first and second column which are common. In other words, e.g ... (6 Replies)
Discussion started by: ahjiefreak
6 Replies

10. Shell Programming and Scripting

Help with for loop within two scripts

Hello All, I have a problem in executing the following - I have a monitor.sh that kicks off and then the config.sh which sends message to the monitor.sh. So If I recieve a Failed message, the monitor.sh will kill the config.sh process and outputs a message. monitor.sh: message= FAILED... (3 Replies)
Discussion started by: chiru_h
3 Replies
Login or Register to Ask a Question