Undesired removal of white space with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Undesired removal of white space with awk
# 1  
Old 06-20-2012
Undesired removal of white space with awk

Hi,

I'm fairly new to scripting and I have a problem that I am having difficulty solving.

What I'd like to do is run an awk script to adjust the string in the first field depending on the string in another field. This is best explained with an example:

Here is my script:

Code:
cat find_replace.awk
BEGIN{}
{
    if ($1 == "01" && $6 == "55"){
    sub("01","07",$1);{print $0}
    }
    else if ($1 == "02" && $6 == "55"){
    sub("02","17",$1);{print $0}
    }
    else if ($1 == "31" && $6 == "55"){
    sub("31","67",$1);{print $0}
    }
    else if ($1 == "36" && $6 == "55"){
    sub("36","67",$1);{print $0}
    }
    else {print $0}
}

For each line, the above script reads the string in the first and sixth fields. Should the first field contain 01, 02, 31 or 36 and the sixth field contain 55, then the number in the first field is substituted and the line printed. Otherwise, the line is printed unaltered.


Here is my input file:

Code:
 cat test_input
01 ABCDEFGH AB  AB 1234       04   1  12
01 AAAAAAAA AA  AA 1111       55   1  11
02 AAAAAAAA AA  AA 1111       55   1  11
31 AAAAAAAA AA  AA 1111       55   1  11
36 AAAAAAAA AA  AA 1111       55   1  11
36 AAAAAAAA AA  AA 1111       77   1  11
94 AAAAAAAA AA  AA 1111       63   1  11

and here is the actual output:

Code:
 gawk -f find_replace.awk test_input
01 ABCDEFGH AB  AB 1234       04   1  12
07 AAAAAAAA AA AA 1111 55 1 11
17 AAAAAAAA AA AA 1111 55 1 11
67 AAAAAAAA AA AA 1111 55 1 11
67 AAAAAAAA AA AA 1111 55 1 11
36 AAAAAAAA AA  AA 1111       77   1  11
94 AAAAAAAA AA  AA 1111       63   1  11

For reasons that I do not understand, the sub commands have stripped the white space between the fields and replaced it with a single space, despite using a print $0 statement. Is there another command that I can use instead of sub that will preserve the white space?

The output that I am looking for is:

Code:
 
01 ABCDEFGH AB  AB 1234       04   1  12
07 AAAAAAAA AA  AA 1111       55   1  11
17 AAAAAAAA AA  AA 1111       55   1  11
67 AAAAAAAA AA  AA 1111       55   1  11
67 AAAAAAAA AA  AA 1111       55   1  11
36 AAAAAAAA AA  AA 1111       77   1  11
94 AAAAAAAA AA  AA 1111       63   1  11

where the correct number of spaces are preserved between the fields.

I'm sorry if this problem has been addressed before - I did a brief search, but I couldn't find a solution. Please feel free to direct me to a solution if this has been addressed before. Also, if possible, I'd prefer to keep the script in a file that can be called using awk -f scriptname.
# 2  
Old 06-20-2012
You can do something like this:
Code:
BEGIN{}
{
    if ($1 == "01" && $6 == "55"){
    s = $0
    sub("^01","07",s);{print s}
    }
    else if ($1 == "02" && $6 == "55"){
    s = $0
    sub("^02","17",s);{print s}
    }
    else if ($1 == "31" && $6 == "55"){
    s = $0
    sub("^31","67",s);{print s}
    }
    else if ($1 == "36" && $6 == "55"){
    s = $0
    sub("^36","67",s);{print s}
    }
    else {print $0}
}

This User Gave Thanks to Franklin52 For This Post:
# 3  
Old 06-20-2012
Or if you like long one-liners ;-), this works:

Code:
awk '$1 ~ /[01|02|31|36]/ && $6 ~ /55/{sub("01","07");sub("02","17");sub("31","67");sub("36","67");print}' file

07 AAAAAAAA AA  AA 1111       55   1  11
17 AAAAAAAA AA  AA 1111       55   1  11
67 AAAAAAAA AA  AA 1111       55   1  11
67 AAAAAAAA AA  AA 1111       55   1  11

This User Gave Thanks to in2nix4life For This Post:
# 4  
Old 06-20-2012
Thank you so much Franklin52 and in2nix4life!I can't believe that the solution was so straight forward.

I prefer to have a script file so that I can send it to others to use, although the one liner is also useful. I'm guessing that I could make the one liner even longer by getting it to print the lines that do not have replaced strings like the script in my opening post. Smilie

Once again, thanks for your responses.
# 5  
Old 06-20-2012
Code:
awk '$6==55{sub("^01 ","07 "); sub("^02 ","17 "); sub(/^3[16] /,"67 ")}1' infile




--
Quote:
Originally Posted by in2nix4life
Or if you like long one-liners ;-), this works:
Code:
awk '$1 ~ /[01|02|31|36]/ && [..]

That is equivalent to
Code:
awk '$1 ~ /[01236|]/ && [..]

This may be what you were after:
Code:
awk '$1 ~ /^(01|02|31|36)$/


Last edited by Scrutinizer; 06-20-2012 at 04:55 PM..
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removal of space

Hi , Can any one help me out how to remove space from below line select file_name from dba_data_files where tablespace_name='SYSTEM ----space---- '; i want as : select file_name from dba_data_files where tablespace_name='SYSTEM'; my code i use file=/u01/script/latest/tbs.temp while... (14 Replies)
Discussion started by: Praful Pednekar
14 Replies

2. Shell Programming and Scripting

Add white space

hi guys how can i add spacein file name with sed if strings have no space around dash input 19-20 ( 18-19 ) ABC-EFG output after add white space 19 - 20 (18 - 19 ) ABC - EFG thx in advance (2 Replies)
Discussion started by: mhs
2 Replies

3. Shell Programming and Scripting

Removing white space in awk

Hi How to remove white space from this input:|blue | 1| |green| 4| |black| 2| I like to search for green and get 4not 4 How to modify this to work correct:awk -F"|" '/green/ {print $3} (7 Replies)
Discussion started by: Jotne
7 Replies

4. Shell Programming and Scripting

awk - trim white space from a field / variable

Hi, Consider the data (FS = |): 1| England |end 2| New Zealand |end 3|Australia|end 4| Some Made Up Country |end 5| West Indies|end I want the output to be (i.e. without the leading and trailing white space from $2) England New Zealand Australia Some Made Up Country West... (4 Replies)
Discussion started by: Storms
4 Replies

5. Shell Programming and Scripting

AWK - Ignoring White Space with FS

I have an AWK script that uses multiple delimiters in the FS variable. FS="+" My awk script takes a file name such as this: 12345_smith_bubba_12345_20120215_4_0.pdf and parses it out based on the under score. Each parsed field then has some code for data validation etc. This script has... (12 Replies)
Discussion started by: reno4me
12 Replies

6. UNIX for Dummies Questions & Answers

filename with white space

our user creates a text file with a white space on the filename. this same file is transfered to unix via automation tool. i have a korn shell script that reads these files on a input directory and connects to oracle database to run the oracle procedures which will load the data from each of the... (2 Replies)
Discussion started by: wtolentino
2 Replies

7. UNIX for Dummies Questions & Answers

removal of space from the end

HI, I need the help from the experts like I have created one file with text like: Code: a b c de f g hi j k l So my question is that i have to write the script in which like in the first sentence it will take only one space after d and remove all the extra space in the end.I dont... (0 Replies)
Discussion started by: bhanudhingra
0 Replies

8. Shell Programming and Scripting

awk: Eliminating white space while setting variable

Hi, I have a large flat file from host without delimiter. I'm transforming this file to a csv file using statements like # Row 03: Customer / field position 3059 +20 WOFABNAM=substr( $0, 3059, 20 ); and deleting the trailing whitespaces before and after with that sub( /^ +/, "",... (4 Replies)
Discussion started by: Celald
4 Replies

9. Shell Programming and Scripting

sed + white space

Hi, What sed command (if sed is the right command) can remove ALL white space from my file. I have a csv, except I want to remove all white space between commas and characters. My idea (without testing) sed 's/ //g' Is there a better way? (18 Replies)
Discussion started by: mcclunyboy
18 Replies

10. UNIX for Dummies Questions & Answers

SED with White Space

Dear Members, Suppose i have a variable test which stores a string as below: test='John drives+++++++++a+++++car' now i want to use sed on the above variable and replace + with a white space, so that i get echo $test should give me 'john drives a car' Between... (1 Reply)
Discussion started by: sandeep_1105
1 Replies
Login or Register to Ask a Question