0

I want to copy one csv header to another in row wise with some modifications

Input csv

name,"Mobile Number","mobile1,mobile2",email2,Address,email21
test, 123456789,+123456767676,[email protected],testaddr,[email protected]
test1,7867778,8799787899898,b@test,com, test2addr,[email protected]

In new csv this should be like this and file should also be created. And for sting column I will pass the column name so only that column will be converted to string

name.auto()
Mobile Number.auto()
mobile1,mobile2.string()
email2.auto()
Address.auto()
email21.auto()

As you see above all these header with type modification should be inserted in different rows

I have tried with below command but this is only for copy first row

sed '1!d' input.csv > output.csv
7
  • What did you try so far? Commented Dec 6, 2020 at 12:56
  • Welcome to Stack Overflow. SO is a question and answer page for professional and enthusiast programmers. Please add your own code to your question. You are expected to show at least the amount of research you have put into solving this question yourself. Commented Dec 6, 2020 at 13:01
  • @Tushar I have updated the question with my effort. Commented Dec 6, 2020 at 13:10
  • @user13000875 - I am not able to understand the output requirement. Do you want to split the header row into multiple lines? Commented Dec 6, 2020 at 13:14
  • What is the criterion for using .string()? Commented Dec 6, 2020 at 13:15

2 Answers 2

2

You may try this alternative gnu awk command as well:

awk -v FPAT='"[^"]+"|[^,]+' 'NR == 1 {
   for (i=1; i<=NF; ++i)
      print gensub(/"/, "", "g", $i) "." ($i ~ /,/ ? "string" : "auto") "()"
   exit
}' file
name.auto()
Mobile Number.auto()
mobile1,mobile2.string()
email2.auto()
Address.auto()
email21.auto()

Or using sed:

sed -i -e '1i 1234567890.string(),My address is test.auto(),[email protected](),120000003.auto(),abc-003.auto(),3.com.auto()' -e '1d' test.csv
Sign up to request clarification or add additional context in comments.

4 Comments

You solution prints perfect but when I save this output to a file then mobile1 is saving in one column and mobile2.string() is on another column
Do you have some DOS line endings in your input file? Can you show output of head file | cat -A command?
name,"Mobile Number","mobile1,mobile2",email2,Address,email21$. After running head file | cat -A command
ok then run awk -v FPAT='"[^"]+"|[^,]+' 'NR==1 {for (i=1; i<=NF; ++i) print gensub(/"/, "", "g", $i) "." ($i ~ /,/ ? "string()" : "auto()"); exit}' file > out; cat -A out
1

EDIT: As per OP's comment to print only first line(header) please try following.

awk -v FPAT='[^,]*|"[^"]+"' '
FNR==1{
  for(i=1;i<=NF;i++){
    if($i~/^".*,.*"$/){
      gsub(/"/,"",$i)
      print $i".string()"
    }
    else{
      print $i".auto()"
    }
  }
  exit
}
' Input_file > output_file


Could you please try following, written and tested with GUN awk with shown samples.

awk -v FPAT='[^,]*|"[^"]+"' '
FNR==1{
  for(i=1;i<=NF;i++){
    if($i~/^".*,.*"$/){
      gsub(/"/,"",$i)
      print $i".string()"
    }
    else{
      print $i".auto()"
    }
  }
  next
}
1
' Input_file

Explanation: Adding detailed explanation for above.

awk -v FPAT='[^,]*|"[^"]+"' '  ##Starting awk program and setting FPAT to [^,]*|"[^"]+".
FNR==1{                        ##Checking condition if this is first line then do following.
  for(i=1;i<=NF;i++){          ##Running for loop from i=1 to till NF value.
    if($i~/^".*,.*"$/){        ##Checking condition if current field starts from " and ends with " and having comma in between its value then do following.
      gsub(/"/,"",$i)          ##Substitute all occurrences of " with NULL in current field.
      print $i".string()"      ##Printing current field and .string() here.
    }
    else{                      ##else do following.
      print $i".auto()"        ##Printing current field dot auto() string here.
    }
  }
  next                         ##next will skip all further statements from here.
}
1                              ##1 will print current line.
' Input_file                   ##Mentioning Input_file name here.

6 Comments

I only want to this for header.
@user13000875, ok sure, edited the answer now, it will edit the very first line and print the rest of the lines as it is, let me know.
Rest of the lines I don't wan to print into new file . only headers need to print
@user13000875, Please try my EDIT solution once and let me know then?
@your solution works when there is no space and comma in header column . but when space or comma in headers column then all the data is printing in single line
|

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.