awk gsub with variables | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

awk gsub with variables

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-11-2013
somu_june somu_june is offline
Registered User
 
Join Date: Nov 2009
Last Activity: 14 May 2014, 8:12 AM EDT
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
awk gsub with variables

Hello,

I'm trying to substitute a string with leading zero for all the records except the trailer record using awk command and with variables. The input file test_med1.txt has data like below


Code:
1234ABC...........................9200............LF
456789.............................0401............LF

And I want to replace 9200 with 09200 and 0401 with 00401


Code:
1234ABC...........................09200............LF
456789.............................00401............LF

I tried below command but not successfull

Command :


Code:
a= cat test_med1.txt | awk '!/TLR/' |
  cut -c 65-84 b= `cat test_med1.txt |
  awk '!/TLR/' |
  cut -c 65-84 |
  awk '{$0=0$0;print}'`; awk -v rvar=${a} -v svar=${b} '{gsub(rvar,svar); print}' test_med1.txt

Error:

Code:
cut: 0653-132 Cannot open b=.
cut: 0653-132 Cannot open 09200.
cut: 0653-132 Cannot open 00401.
cut: 0653-132 Cannot open 07803.
cut: 0653-132 Cannot open 05400.
cut: 0653-132 Cannot open 02500.
cut: 0653-132 Cannot open 09901.
cut: 0653-132 Cannot open 00700.
awk: 0602-533 Cannot find or open file 07803.
The source line number is 1.


Please let me know where the command in failing and if it is complete wrong then let me know how to get above output using awk and gsub command


Thanks,
Somaraju

Last edited by Scott; 03-11-2013 at 11:33 PM.. Reason: You forgot to add code tags...
Sponsored Links
    #2  
Old 03-12-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 31 October 2014, 12:14 PM EDT
Location: Aachen, Germany
Posts: 4,485
Thanks: 74
Thanked 1,099 Times in 1,035 Posts
How do you detect the strings to replace, e.g. by position in line?
How do you identify the trailer record?

And, yes, your command has large opportunities for improvement.
Sponsored Links
    #3  
Old 03-12-2013
Don Cragun's Avatar
Don Cragun Don Cragun is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 31 October 2014, 9:12 PM EDT
Location: San Jose, CA, USA
Posts: 4,939
Thanks: 186
Thanked 1,656 Times in 1,405 Posts
If all you want to do is add a 0 after input character number 64 on lines that don't contain the string TLR , you don't need cat, cut, or gsub(); you just need awk's substr() and print functions.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk gsub ysrini Shell Programming and Scripting 3 11-06-2012 03:46 PM
Awk; gsub in fields 3 and 4 Bubnoff Shell Programming and Scripting 6 09-30-2010 05:15 PM
awk gsub with variables? ergy1983 Shell Programming and Scripting 3 07-01-2010 03:52 AM
awk gsub pxy2d1 Shell Programming and Scripting 4 08-22-2008 08:14 AM
Help with AWK and gsub npolite Shell Programming and Scripting 4 11-15-2007 03:21 PM



All times are GMT -4. The time now is 09:48 PM.