Friday 15 August 2014

Bash script replace two fields in a text file using variables -


It should be a simple fix, but I can not wrap my head at this time.

There is a comma-delimited file named I my_course that contains a list of some information about them.

I need to get user input about the last two areas and change them accordingly.

Example:

  CSC3210   

, Computer Org & amp; Programming, 3.0, N / A CSC2010, Introduction to Computer Science, 3.0, N / A CSC1010, Computer & amp; Application 3,0, N / A

I get user input for 3 things: course number , position (0 or 1) , and Grade (A, B, C, N / A)

So far, I have tried to match the line which has a course number and the last two fields changed Used to be. I do not know how to modify the previous two areas by using SAL, so I am using this terrible disturbance and SAD:

  temporary = $ (awk -v status = $ status -v grade = $ grade 'BEGIN {FS = ","}; $ (NF) = "" $ (NF-1) = "" /' $ cNum '/ {printf $ 0 " "Status", "grade;} 'my_course) sed -i" s / csc $ cnum. * / $ Temp / g "my_course   

The issue is that I'm moving in here The numbers in the course title can be from 1 to 4 in the field so that Not only the first n fields can not simply print. I have tried to remove the last two areas and add new values ​​for the status and grade but it is not working for me.

Note: I have already checked to ensure that the user validates valid data

Use a simple encoded script:

  BEGIN {FS = "," OFS = FS} $ 0 ~ course {$ (NF-1) = position $ NF = Grade} {print}   

And on the CMD-line, set three parameters, different parameters like curriculum, position and grade.

In action:

  $ cat input CSC3210, computer ORG & amp; Programming, 3.0, N / A CSC2010, Introduction to Computer Science, 3.0, N / A CSC1010, Computer & amp; APPLICATIONS, 3.0, N / A $ AJK-vcourse = "CSC3210" -vstatus = "1" -Agread = "A" -f grade.com Input CSC 3210, Computer Org & amp; Programming, 3.1, a CSC-2010, Introduction to Computer Science, 3.0, N / A CSC 1010, Computer & amp; APPLICATIONS, 3.0, N / A $ AJK-vcourse = "CSC1010" -vstatus = "1" -Agread = "B" -F grade Input Input CSC 3210, Computer ORG & amp; Programming, 3.0, N / A CSC2010, Introduction to Computer Science, 3.0, N / A CSC1010, Computer & amp; APPLICATIONS, 3.1, b    

No comments:

Post a Comment