1

I've a file with following content:

A 28713.64                  27736.1000
B 9835.32
C 38548.96

Now, i need to check if the last row in the first column is 'C', then the value of first row in third column should be printed in the third column against 'C'.

Expected Output:

A 28713.64                  27736.1000
B 9835.32
C 38548.96                  27736.1000

I tried below, but it's not working:

awk '{if ($1 == "C") ; print $1,$2,$3}' file_name

Any help is most welcome!!!

4
  • 1
    ...then i want to print the same value which is there in third column. from which row? always the first row? or the last not-zero/empty value? Commented Feb 22, 2019 at 10:57
  • @Kent: yes, always the first row of third column. Commented Feb 22, 2019 at 11:03
  • Your requirements aren't clear - do you need to modify any/every row that starts with C as $1 or do you need to modify the 3rd row IF that specific row starts with C or do you need to modify the 3rd row regardless of it's $1 value or something else? If the 3rd/C-row already has a value in the 3rd field should that be changed or left alone? Commented Feb 22, 2019 at 14:15
  • @EdMorton: I need to modify the row which has string 'C' in it , and my file would always contains only one 'C' in the first column. Commented Feb 22, 2019 at 14:28

3 Answers 3

2

This works for the given example:

awk 'NR==1{v=$3}$1=="C"{$0=$0 FS v}7' file|column -t

If you want to append the 3rd column value from A row to C row, change NR==1 into $1=="A"

The column -t part is just for making output pretty. :-)

Sign up to request clarification or add additional context in comments.

6 Comments

Can you please explain the above awk command. Would be helpful!!! Thanks.
Erm... 7? :-D
@A.K if the current row is the first, save $3 in var v. if $1=="C", add the v to the end of the line, then print the current row.
@ghoti, condition should be TRUE(non-zero number or other words if something is NOT zero in short condition here), either it is 7 or 1 or 100 :) usually IMHO people put 1 so that it will be easily understandable that we are putting 1 to make condition TRUE :)
@RavinderSingh13 you are right, personally I prefer right index finger better than left little finger. :D
|
2

EDIT: As per OP's comment OP is looking for very first line and looking to match C string at very last line of Input_file, if this is the case then one should try following.

awk '
FNR==1{
  value=$NF
  print
  next
}
prev{
  print prev
}
{
  prev=$0
  prev_first=$1
}
END{
  if(prev_first=="C"){
      print prev,value
  }
  else{
      print
  }
}'  file | column -t


Assuming that your actual Input_file is same as shown samples and you want to pick value from 1st column whose value is A.

awk '$1=="A" && FNR==1{value=$NF} $1=="C"{print $0,value;next} 1' Input_file| column -t

Output will be as follows.

A  28713.64  27736.1000
B  9835.32
C  38548.96  27736.1000

3 Comments

Thanks for the answer. It's working. But here, i'm not considering the value of first row in 1st column i.e 'A'. I just want to check if the last row in the first column is 'C', then the value of first row in third column should be printed in the third column against 'C'.
@A.K, please check my EDIT solution, it only checks last line's C if C is not there then it will simply print the line, check and let me know then?
Perfect!! Thanks:-)
2

POSIX dictates that "assigning to a nonexistent field (for example, $(NF+2)=5) shall increase the value of NF; create any intervening fields with the uninitialized value; and cause the value of $0 to be recomputed, with the fields being separated by the value of OFS."

So...

awk 'NR==1{x=$3} $1=="C"{$3=x} 1' input.txt

Note that the output is not formatted well, but that's likely the case with most of the solutions here. You could pipe the output through column, as Ravinder suggested. Or you could control things precisely by printing your data with printf.

awk 'NR==1{x=$3} $1=="C"{$3=x} {printf "%-2s%-26s%s\n",$1,$2,$3}' input.txt

If your lines can be expressed in a printf format, you'll be able to avoid the unpredictability of column -t and save the overhead of a pipe.

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.