Replace multiple positions in records which match crireria

I have a test file a.txt

001 123 456 789
002 This is just a
001 test data
003 file.

I want to clear columns 5 and 6 if the first 3 characters are 001 using awk.

I tried following but does not work. Any suggestions?

awk 'BEGIN{OFS=FS=""} {if (substr($0,1,3)=="123") $5=" "; $6=" ";  print $0}' a.txt

You say 001 but in your code you have 123?

It is type. It should be 001 in code

Hello Soham,

Your Input_file doesn't have 5th and 6th column itself, so if your actual Input_file has it then following may help you in same.

awk '(substr($0,1,3)=="001"){$5=$6=""}1'   Input_file

If your Input_file doesn't have 5th and 6th fields and you meant to remove the last two columns 3rd and 4th then following may help you in same.

 awk '(substr($0,1,3)=="001"){$3=$4=""}1'   Input_file

Thanks,
R. Singh

I meant position 5 and 6.

I tried your command. Unfortunately it gives error

awk: syntax error near line 1
awk: bailing out near line 1

if on Solaris, use nawk instead of awk.

Yes, I tried nawk. Now it does not give error but not desired result. Columns do not become blank.I even tried to change columns 3 and 4 to 5 and 6.

something along these lines?

nawk '$1=="001" {$2="00" substr($2,3)}1' myFile

---------- Post updated at 02:32 PM ---------- Previous update was at 02:31 PM ----------

I think you're using terms columns and positions too freely - they have different meaning...

Try this (untested, try with nawk and /usr/xpg4/bin/awk)

awk 'BEGIN{OFS=FS=""} (substr($0,1,3)=="001") {$5=$6=" "}  {print}' a.txt

That's what. It runs but it does not change the output (i.e. 5th and 6th columns are not becoming blank

Server:cat ~/a.txt
001 123 456 789    x
002 This is just a x
001 test data      x
003 file.          x
         11111111112
12345678901234567890
Server:nawk 'BEGIN{OFS=FS=""} (substr($0,1,3)=="001")
> {$5=$6=" "}  {print}' ~/a.txt
001 123 456 789    x
001 123 456 789    x
002 This is just a x
001 test data      x
001 test data      x
003 file.          x
         11111111112
12345678901234567890
Server:

could you please show the input and the desired output files.
your objective is a bit confusing.......

In the earlier mail I described that only.

Input file is

001 123 456 789    x
002 This is just a x
001 test data      x
003 file.          x
         11111111112
12345678901234567890

The command I ran is

nawk 'BEGIN{OFS=FS=""} (substr($0,1,3)=="001") {$5=$6=" "}  {print}' ~/a.txt

The output I got is

001 123 456 789    x
001 123 456 789    x
002 This is just a x
001 test data      x
001 test data      x
003 file.          x
         11111111112
12345678901234567890

I want to make 5th and 6th position blank for records which start with 001

once again, could you show a DESIRED output given your sample input.

Guessing here:

nawk '$1=="001" {$2="  " substr($2,3)}1' myFile

Great. It worked. But a small issue again. If the input line has one or more blanks in between they are converted to single blank and if at the end of teh line they are truncated.

Naturally

BEGIN{OFS=FS=""}

will not work.

different tack:

nawk '/^001 / {$0="001   " substr($0, index($0," ")+3)}1' myFile

or try this - Solaris' awk/nawk are finicky...

nawk 'BEGIN {FS=SUBSEP}/^001 / {$0="001   " substr($0, index($0," ")+3)}1' myFile
1 Like

Setting FS and OFS to an empty string to treat character positions as fields is a GNU extension that is not supported by nawk , /usr/xpg4/bin/awk , nor required by the POSIX standards. Just using standard interfaces in awk to change character positions 5 and 6 to <space>s if character positions 1, 2, and 3 are the string 001 can be done simply on a Solaris system with:

nawk '/^001/ {$0 = substr($0, 1, 4) "  " substr($0, 7)} 1' input_file

or:

/usr/xpg4/bin/awk '/^001/ {$0 = substr($0, 1, 4) "  " substr($0, 7)} 1' input_file
2 Likes

It works !!!

Thanks

Try also

awk 'sub ("^001.","&##") {sub (/##../, "  ")} 1'  file

You may want to choose a better placeholder pattern / value.

1 Like

Great. I never thought it is so simple !!!

Substitutions at character positions are often easier with sed

sed '/^001/ s/\(.\{4\}\)../\1  /' file

The \1 puts back what matched within the \( \) and that is the first 4 characters.