Overwriting the leading spaces in NAWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Overwriting the leading spaces in NAWK
# 1  
Old 04-07-2009
Overwriting the leading spaces in NAWK

I have a file
Code:
1  1  40421 120   18421       112h 019   6  10335 03    
1  1  40421 120   18422       124h 055   6  10335 03

I have wriiten a nawk statement
Code:
nawk '$5 ~ "18421" {sub($7,"0190"print}' old.txt

I am getting the output as
Code:
1  1  40421 120   18421       112h 0190   6  10335 03   
1  1  40421 120   18422       124h 055   6  10335 03

but i need the following output
Code:
1  1  40421 120   18421       112h 0190 6  10335 03   
1  1  40421 120   18422       124h 055  6  10335 03

The new charcter in $7 filed must replace the leading space Can anyone please help me on this

Last edited by prav076; 04-08-2009 at 02:09 AM..
# 2  
Old 04-07-2009
There is no difference between the output you are getting and the output you are expecting. Typo on your part?
# 3  
Old 04-07-2009
And please make your post more readable, use code tags for your files and code.
Select the text you want between the tags and click on the # above the edit box.

Regards
# 4  
Old 04-08-2009
I have used the code tags
# 5  
Old 04-08-2009
try this
Code:
awk '$5 ~ "18421" {sub($7" ","0190")}{print}' filename

# 6  
Old 04-08-2009
Thanks Vidyadhar for your help ,
the code is working fineSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Trying to remove leading spaces

OS : RHEL 6.7 Shell : bash I am trying to remove the leading the spaces in the below file $ cat pattern2.txt hello1 hello2 hello3 hello4 Expected output is shown below. $ cat pattern2.txt hello1 hello2 hello3 hello4 (2 Replies)
Discussion started by: John K
2 Replies

2. Shell Programming and Scripting

Counting leading spaces to a character

data.txt { "auth_type": "role", "default_attributes": { "sudoers": { i need to know how manyspaces are before an actual character in each line of a file. for example. in the above data.txt, There are 0 spaces leading up to { There are 4 spaces leading up to the... (7 Replies)
Discussion started by: SkySmart
7 Replies

3. Shell Programming and Scripting

Leading white spaces

Hi, I am having problem in deleting the leading spaces:- cat x.csv baseball,NULL,8798765,Most played baseball,NULL,8928192,Most played baseball,NULL,5678945,Most played cricket,NOTNULL,125782,Usually played cricket,NOTNULL,678921,Usually played $ nawk 'BEGIN{FS=","}!a... (2 Replies)
Discussion started by: scripter12
2 Replies

4. Shell Programming and Scripting

Removing leading spaces from the variable value.

Hi All, I am trying to replace the value of a xml tag with a new one. But, the existing value in the xml contain leading spaces and I tried to remove that with different sed commands but all in vain. For replacing the value I wrote the command in BOLD letters below: bash-3.00$... (3 Replies)
Discussion started by: khedu
3 Replies

5. Shell Programming and Scripting

Not able to remove leading spaces

Hi Experts, In a file tht i copied from the web , i am not able to remove the leading white spaces. I tried the below , none of them working . I opened the file through vi to check for the special characters if any , but no such characters found. Your advice will be greatly appreciated. sed... (5 Replies)
Discussion started by: panyam
5 Replies

6. Shell Programming and Scripting

Leading spaces of variable removed

#!/bin/bash TESTVAR=" 5spaces" echo $TESTVAR hostame:~# ./test.sh 5spaces The leading spaces from my variable are removed when the content is echo'd. I am trying to make some tabular data. `echo -e` also fails. Any suggestions? (2 Replies)
Discussion started by: grumm3t
2 Replies

7. Shell Programming and Scripting

remove leading spaces from a line

Hi friends I need some help, I have a file which looks as follows TEMP 014637065 014637065 517502 517502 RTE 517502 517502 RTE AWATER_TEST 12325 23563 588323 2323 5656 32385 23235635 ANOTHER_TEST 12 5433 FTHH 5653 833 TEST 123 123 3235 5353 353 53 35 353 535 3 YTERS GJK JKLS ... (6 Replies)
Discussion started by: lijojoseph
6 Replies

8. UNIX for Dummies Questions & Answers

Stripping leading spaces on right justified name

I have a name field in a file which is right justified (yep - its true). I need to strip the leading spaces from the field and write the name out left justified. Again, I think I need to use a sed or awk command but so far, my results are at best disappointing. Thank you in advance from a UNIX... (2 Replies)
Discussion started by: Marcia P
2 Replies

9. Shell Programming and Scripting

Strip leading and trailing spaces only in a shell variable with embedded spaces

I am trying to strip all leading and trailing spaces of a shell variable using either awk or sed or any other utility, however unscuccessful and need your help. echo $SH_VAR | command_line Syntax. The SH_VAR contains embedded spaces which needs to be preserved. I need only for the leading and... (6 Replies)
Discussion started by: jerardfjay
6 Replies

10. Shell Programming and Scripting

Leading and Trailing Spaces

Hi, how to i remove leading and trailing spaces from a line? the spaces can be behind or in front of any field or line example of a line in the input data: Amy Reds , 100 , /bin/sh how to i get it to be: Amy Read,100,/bin/sh i saw something on this on the Man pages for AWK... (7 Replies)
Discussion started by: sleepster
7 Replies
Login or Register to Ask a Question