Senthil
Senthil

Reputation: 83

nawk whitespace trim with multiple condition

temp_file.txt

|112233456543214 |
|154233456873221|
|154233456868320|

When i am using the below nawk command i saw the data is skipping if you have the empty space so i have to trim the white space .I have to include the white space trim in the below command(Know to use nwak command alone)that is the challenge.The question i have is to how include the white space trim in the below command

nawk -F '|' 'BEGIN{FS=OFS="|"}(length($2) >=13){
  $2=substr($2,1,6)"xxxxxx"substr($2,length($2)-3)}1' temp_file.txt>tmp.txt && mv tmp.txt temp_file.txt

Upvotes: 0

Views: 233

Answers (2)

glenn jackman
glenn jackman

Reputation: 247042

Because the first char is a pipe, your $1 will always be an empty string. You want to examine $2 for your data.

awk -F\| -v OFS=\| '{gsub(/(^[[:blank:]]+)|([[:blank:]]+$)/, "", $2)} 1' <<END
|112233456543214 |
|  154233456873221  |
| 154233456868320|
END
|112233456543214|
|154233456873221|
|154233456868320|

If you're using -F, you don't also need to define FS in a BEGIN block.


If you want it spelled out:

$ nawk -F '|' '
    BEGIN {OFS = FS}
    function obfuscate(string) {
        gsub(/(^[[:blank:]]+)|([[:blank:]]+$)/, "", string)
        return substr(string,1,6) "xxxxxx" substr(string,length(string)-3)
    }
    length($2) >= 13 {$2 = obfuscate($2) }
    1
' <<END
|112233456543214 |
|  154233456873221  |
|123456789012|
|1234567890123|
|12345678901234|
| 154233456868320|
END
|112233xxxxxx3214|
|154233xxxxxx3221|
|123456789012|
|123456xxxxxx0123|
|123456xxxxxx1234|
|154233xxxxxx8320|

Upvotes: 3

Dennis Williamson
Dennis Williamson

Reputation: 360445

nawk supports regular expressions in the definition of FS. You can use this to trim the leading and trailing spaces or tabs.

nawk -F '[[:blank:]]*[|][[:blank:]]*' 'BEGIN{OFS = "|"} (length($2) >= 13) {
    $2 = substr($2, 1, 6) "xxxxxx" substr($2, length($2) - 3)}1'

The pipe is surrounded by square brackets to prevent it from acting as the regex alternation operator. The regex says to use the combination of zero or more blanks (space or tab) followed by a pipe character followed by zero or more blanks as the field separator.

Upvotes: 0

Related Questions