Need comand or script for append text after searching for the desired string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need comand or script for append text after searching for the desired string
# 29  
Old 08-12-2014
Do we have any other command for this requirement...?i tried by including this printf statement but of no use....SmilieSmilie
# 30  
Old 08-12-2014
How about doing some handicrafts yourself?

I came up with this, making use of the printf executable itself, it's messy but does the job - adapt further if need be:
Code:
awk -F, 'NF==2  {next}
                {ITM[++c]=$1
                 AMT[c]=$2+0
                 CNT[c]=$3+0
                 TOTA+=$2   
                 TOTC+=$3}
         END    {PO="read a b x; /usr/bin/printf \"%s %s %\047.6g\n\" $a $b $x"  
                 for (i=1;i<=c;i++) printf "amount %s:%s\n", ITM[i], AMT[i]
                 for (i=1;i<=c;i++) print " count " ITM[i] ":" CNT[i]
                 print "total amount: " TOTA | PO; close (PO)
                 print "total count: " TOTC | PO; close (PO)}
        ' file
amount  0125:1000
amount  0124:1000
 count  0125:20
 count  0124:3
total amount: 2.000
total count: 23

# 31  
Old 08-12-2014
A second thread discussing this same topic was recently started by the originator of this thread. That thread is now closed. I am not merging the threads because I believe it would confuse both discussions even though there is some overlap.
# 32  
Old 08-12-2014
hi Rudic,
i have implemeted the same through below code and its working
Code:
awk -F ,-v sq="'" '
         NF<3 {next}
  {ITM[NR]=$1
   AMT[NR]=$2
   CNT[NR]=$3
   TOTA+=$2
   TOTC+=$3}
    END   {FOR  (i=2;i<=NR:i++)
   {
           if [[ AMT[i] -gt 1 ]];
           then
           printf( "   amount"ITM[i] " %" sq".2f\n",AMT[i]"\n")
    else
           printf( "   amount"ITM[i]      " %" sq".2f\n",AMT[i]"\n")
     fi
          }
   printf("      TOTAL AMOUNT %" sq".2f\n", TOTA)
         }

in the aboev code snippet i add logic only for amount not for count ....

while iam trying to excute without if condtion its working ... getting the ouput as expected with comma seperated...
now i am trying to display two type of printf statements based on if logic but iam, getting syntax error is der any problem in the above logic..? or syntax...?(note : printf statements are same but differ in spaces appended) can u please help me..?

thanks in advance

regards,
hemanth sai
# 33  
Old 08-12-2014
Quote:
Originally Posted by hemanthsaikumar
hi Rudic,
i have implemeted the same through below code and its working
Code:
awk -F ,-v sq="'" '
         NF<3 {next}
  {ITM[NR]=$1
   AMT[NR]=$2
   CNT[NR]=$3
   TOTA+=$2
   TOTC+=$3}
    END   {FOR  (i=2;i<=NR:i++)
   {
           if [[ AMT[i] -gt 1 ]];
           then
           printf( "   amount"ITM[i] " %" sq".2f\n",AMT[i]"\n")
    else
           printf( "   amount"ITM[i]      " %" sq".2f\n",AMT[i]"\n")
     fi
          }
   printf("      TOTAL AMOUNT %" sq".2f\n", TOTA)
         }

in the aboev code snippet i add logic only for amount not for count ....

while iam trying to excute without if condtion its working ... getting the ouput as expected with comma seperated...
now i am trying to display two type of printf statements based on if logic but iam, getting syntax error is der any problem in the above logic..? or syntax...?(note : printf statements are same but differ in spaces appended) can u please help me..?

thanks in advance

regards,
hemanth sai
OK. So it looks like my suggestion in the other thread did work.

Your indentation seems to be trying to hide the structure of your code, but I'm not going to try to fix that for you again. Try changing:
Code:
           printf( "   amount"ITM[i] " %" sq".2f\n",AMT[i]"\n")
    else
           printf( "   amount"ITM[i]      " %" sq".2f\n",AMT[i]"\n")

to:
Code:
           printf( "   amount %s  %" sq ".2f\n", ITM[i], AMT[i])
    else
           printf( "   amount %s       %" sq ".2f\n", ITM[i], AMT[i])

# 34  
Old 08-12-2014
hi ,

i am getting a syntax error in if logic is it right ...?(no problem with printf statements)

thanks in advance
# 35  
Old 08-12-2014
You are using shell syntax for your if statement - change to awk syntax.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Needed shell script to append desired text to each line in a file

Hi, I had generated a report in my tool as followsoutput.txt 43.35 9 i needed the script to generate a new file like below i want to append the text to each of these lines of my filenewoutputfile.txt should be Total Amount : 43.35 Record Count:9 Regards, Vasa Saikumar. ... (2 Replies)
Discussion started by: hemanthsaikumar
2 Replies

2. Shell Programming and Scripting

sed - Find a String and append a text end of the Line

Hi, I have a File, which have multiple rows. Like below 123456 Test1 FNAME JRW#$% PB MO Approver XXXXXX. YYYY 123457 Test2 FNAME JRW#$% PB MO Super XXXXXX. YYYY 123458 Test3 FNAME JRW#$% PB MO Approver XXXXXX. YYYY I want to search a line which contains PB MO Approver and append... (2 Replies)
Discussion started by: java2006
2 Replies

3. Shell Programming and Scripting

Help Needed! - Cut characters after a text string and append to end of filename

Hi all.. I have several unique files that contain one thing in common, and that is acct#. For all files in the directory, I want to append the 10 characters following the word "ACCOUNT:" to the end of the filename. for example: I have file 111_123 that contains ACCOUNT:ABC1234567 The file... (5 Replies)
Discussion started by: cinderella1
5 Replies

4. Shell Programming and Scripting

Trying to search for a string and append text only once

Hi I am trying to search for a particular occurrence of a string in a file, and if found, append another string to the end of that line. Here is my file contents: column1 userlist default nowrite=3 output=4 column2 access default nowrite=3 Here is the code: A="user=1... (1 Reply)
Discussion started by: bludhemn
1 Replies

5. Shell Programming and Scripting

Searching for a particular string and modifying text within block of data

Hi Forum. Is there a quick way to do the following search/replace within a block of data? I tried to google the solution but didn't really know what to look for. I have the following text file (I want to search for a particular string "s_m_f_acct_txn_daily_a1" and replace the... (5 Replies)
Discussion started by: pchang
5 Replies

6. Shell Programming and Scripting

searching a text string for n'th :

hello, i'm a novice on bsh scripting so thanks for any help here basically i have a shell var $x that looks like this > echo $x nabc1234:!:73394:17155:Gary Mason:/home/garym:/bin/ksh and i'm trying to keep the first 8 characters and the text from the 4th : to the 5th : i've been trying... (9 Replies)
Discussion started by: sasglm
9 Replies

7. Shell Programming and Scripting

String searching and output to a file in a formatted text

Hi, I'm very new to UNIX scripting and find quite difficult to understand simple UNIX syntax. Really appreciat if somebody could help me to give simple codes for my below problems:- 1) I need to search for a string "TTOH 8031950001" in a files which filename will be "*host*'. For example, the... (3 Replies)
Discussion started by: cuji
3 Replies

8. Shell Programming and Scripting

Search a string and append text after the string

Hi, I have a file like this... <o t="Batch" id="8410" p="/" g="32"> <a n="name"> <v s="DBBA1MM"/> </a> <a n="owner"> <v r="/Administrator"/> </a> <a n="rights"> <v s="95"/> </a> <a n="debugLevel"> <v s="3"/> </a> <a n="avsStoreLoc"> <v... (8 Replies)
Discussion started by: kesu2k
8 Replies

9. Shell Programming and Scripting

PERL: Searching for a string in a text file problem

Looking for a bit of help. I need to search for a string of words, but unfortunately these words are located on separate lines. for example the text output is: United Chanmpions Ronaldo Liverpool Losers Torres and my script code is print("DEBUG - checking file message"); while... (15 Replies)
Discussion started by: meevagh
15 Replies

10. UNIX for Dummies Questions & Answers

Hi! Searching for a text string in UNIX

Hi! I'm new here and glad to meet everyone! I've been wrestling with a problem lately however! How do I recursively (recursive means to keep going through the subdirectories until no more are there) search a bunch of textfiles in a long directory structure for a specific string.. but only... (1 Reply)
Discussion started by: skwadim
1 Replies
Login or Register to Ask a Question