ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Welcome to LinuxQuestions.org, a friendly and active Linux Community.

You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!

Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.

If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.

Having a problem logging in? Please visit this page to clear all LQ-related cookies.

Introduction to Linux - A Hands on Guide

This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.

I´m tryng to replace in specific column and line number within a file where its 3erd column contain the same string in all lines.

Code:

Name 1|Last name 1|Normal player
Name 2|Last name 2|Normal player
Name 3|Last name 3|Normal player
Name 4|Last name 4|Normal player
Name 5|Last name 5|Normal player
Name 6|Last name 6|Normal player
Name 7|Last name 7|Normal player

My goal is to replace only first and last ocurrences of "Normal player", with the following desired output:

Code:

Name 1|Last name 1|Captain
Name 2|Last name 2|Normal player
Name 3|Last name 3|Normal player
Name 4|Last name 4|Normal player
Name 5|Last name 5|Normal player
Name 6|Last name 6|Normal player
Name 7|Last name 7|Coach

I´m not sure how to use the "IF" and "AND" conditions together. I´ve tryed with the code below, but the script replaces the string for every line.

Code:

awk 'BEGIN {OFS=FS="|"; IGNORECASE=1} {if($3 && NR=1) sub(/Normal player/,"Captain",$3); print}' inputfile
Name 1|Last name 1|Captain
Name 2|Last name 2|Captain
Name 3|Last name 3|Captain
Name 4|Last name 4|Captain
Name 5|Last name 5|Captain
Name 6|Last name 6|Captain
Name 7|Last name 7|Captain

how to replace values for specific column in first and last lines within same AWK script, without taking reference data in other columns?

This works only if you know prior to running how many lines there are.
Also, as you do know the format of each line, ie that the third column is the one needing changing and you are changing the whole field,
you can dispense with the sub and just use: <field_number>="<new_text>"

As syg00 has said, you already have the solution, but I thought I would give you an alternative to think about. Let me know if you need
help with what is happening:

syg00, finally with the above script, it´s possible to include the variable that stores the number of the last line "LastRecord=$(awk 'END{print NR}' inputfile)" in something similar to NR==LastRecord instead of NR==7?

grail,

Excellent! really thanks to you too. Your recent code works precise, even I don´t understand some parts of it. For this reason I´ve followed your suggestions and built something similar to your script using what I know so far:

I remove "sub" function assigning directly a new value to $3 and the script is simpler now, but doesn´t work correctly. But doesn´t matter, is only to compare yours and trying to undertand it.

Code:

I´m not sure if it works, because only prints the last line.
awk 'BEGIN {OFS=FS="|"; IGNORECASE=1} NR==1{$3="Captain"} END{$3="Coach";print}' file

Seeing the different parts, how is structured and how works your script comparing with the one I wrote I think that:
1-)";f=0" --> Is like a control variable function and could take values like 0, 1, ...n depending needs.
2-)"f{print x}" --> Evaluating the function to print variable x, but I´m not sure what really is, and what does.
3-)"{x=$0;f=1}" --> Obviously assign all fields to x and f takes value=1 like saying that will execute a new process. I think this part stores all records to avoid show only last line when execute END{$3="coach";print} like happen to me in the previous script.
4-)"NR==1{$3="captain"}" and "END{$3="coach"" --> I understand better this, I think is a directly assignation in a specific column and specific line.

Well, this is how I can interpret your script, please may you help me explaining "why each part is present?", "how it works each part?", "what does each part do?" and "how the script knows which is the last line?".

grail, I´m understand much, much better with your explanation. I´ve learn a lot in this thread. This will help a lot in future scripts.A good example in how to use a boolean controller to print on our needs.

I´ve put your script operation mode and logic that used it within how I understand in a graphic way, an Script Image.

There is no "last record" indicator I'm aware of. END enables you to process after you've read past the last record (i.e. reached eof). Useful for printing totals or footer lines.
Else you need something like grail offered.

Take note of 2) and 3) above - it introduces a nuance that may not be obvious.
And note the comment above re "f{print x}" - it is *not* a function, although it looks (too much) like it if you are used to other languages.