1

I have a csv file with multiple headers and their corresponding records as below-

NA,A,B,C
$H,1,2,3
NA,D,E,F
$R,4,5,6
NA,G,H,I
$R,7,8,9
NA,J,K,L
$R,10,11,12

Please note the rows starting with NA are headers. need to replace 'NA' with null. So my expected output will be-

,A,B,C
$H,1,2,3
,D,E,F
$R,4,5,6
,G,H,I
$R,7,8,9
,J,K,L
$R,10,11,12

I have tried the below code , but did not work-

awk -F '|' -v OFS='|' '$1 == "NA" { $1 = "" }1' test.csv >test_n.csv
2
  • it's always the first line, you don't need to check the entire file, right? Commented Mar 31, 2022 at 20:16
  • Please look up FS and OFS in the awk man page and ask yourself why you're setting them to |. If sed 's/NA//' isn't all you need then edit your question to show NAs is some other contexts where you don't want them removed. Commented Apr 1, 2022 at 1:43

2 Answers 2

2

Using a fairly simple sed:

sed 's/^NA,/,/' file

,A,B,C
$H,1,2,3
,D,E,F
$R,4,5,6
,G,H,I
$R,7,8,9
,J,K,L
$R,10,11,12
Sign up to request clarification or add additional context in comments.

Comments

1

Changing the FS in your code would have worked

$ awk 'BEGIN{FS=OFS=","}$1=="NA"{$1=""}1' input_file
,A,B,C
$H,1,2,3
,D,E,F
$R,4,5,6
,G,H,I
$R,7,8,9
,J,K,L
$R,10,11,12

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.