|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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.txtError: 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
|
|||
|
|||
|
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
|
|||
|
|||
|
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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|