Tedee12345
Tedee12345

Reputation: 1210

AWK, getline - remove the last field

I have a file:

Srednia ocen Wojciech Zieba: 3.83
Srednia ocen Jakub Pelc: 3.76
Srednia ocen Jan Resiak: 3.90

I want to improve the command:

1.

$ awk 'BEGIN{ while( getline $0 < "file") sub($NF,"",$0); print $0}' 
Srednia ocen Jan Resiak: 
userpc@userpc-desktop:~

2.

$ awk 'BEGIN{ while( getline a < "file") sub($NF,"", a); print a}' 
Srednia ocen Jan Resiak: 3.90 
userpc@userpc-desktop:~/Pulpit$

I have to do it by getline. My example is only part of the script. I want to remove last field and print the result:

Srednia ocen Wojciech Zieba: 
Srednia ocen Jakub Pelc: 
Srednia ocen Jan Resiak:

Thank you for your help.

EDIT:

But this way does not print the remaining fields:

$ awk 'BEGIN{ while( getline a < "file") {sub(/[^:]*$/,"",a); print a, $1, $3}}'
Srednia ocen Wojciech Zieba:  
Srednia ocen Jakub Pelc:  
Srednia ocen Jan Resiak:  
bolek@bolek-desktop:~/Pulpit$

This command prints all the fields:

$ awk 'BEGIN{ while( getline $0 < "file") {sub($NF,"",$0); print $0, $1, $3}}'
Srednia ocen Wojciech Zieba:  Srednia Wojciech
Srednia ocen Jakub Pelc:  Srednia Jakub
Srednia ocen Jan Resiak:  Srednia Jan
bolek@bolek-desktop:~/Pulpit$ 

How to improve the first command? I want to first command also shared lines on the field.

EDIT-1:

I want to improve the command:

awk 'BEGIN{ while( getline a < "file") {sub(/[^:]*$/,"",a); print a, $1, $3}}'

I want to print the result:

Srednia ocen Wojciech Zieba:  Srednia Wojciech
Srednia ocen Jakub Pelc:  Srednia Jakub
Srednia ocen Jan Resiak:  Srednia Jan

Upvotes: 1

Views: 6068

Answers (3)

Thor
Thor

Reputation: 47099

As it is you set the a variable to each line in turn, but if you omit a $0 will be set and auto-split will be performed, which would allow you to do this:

awk 'BEGIN{ while( getline < "file") { $NF = ""; print $0, $1, $3 } }'

Output:

Srednia ocen Wojciech Zieba:  Srednia Wojciech
Srednia ocen Jakub Pelc:  Srednia Jakub
Srednia ocen Jan Resiak:  Srednia Jan

Upvotes: 1

Kent
Kent

Reputation: 195059

an awk way:

awk -F':' '$0=$1 ":"' file

test

kent$  echo "Srednia ocen Wojciech Zieba: 3.83
Srednia ocen Jakub Pelc: 3.76
Srednia ocen Jan Resiak: 3.90"|awk -F':' '$0=$1 ":"'
Srednia ocen Wojciech Zieba:
Srednia ocen Jakub Pelc:
Srednia ocen Jan Resiak:

update

i don't know what you really wanna do, but your while{} scope has problem. maybe you want this?

kent$  awk 'BEGIN{ while( getline $0 < "file") {sub($NF,"",$0); print $0}}' 
Srednia ocen Wojciech Zieba: 
Srednia ocen Jakub Pelc: 
Srednia ocen Jan Resiak:

if you want to introduce your own variable, say a, it has no idea about $NF. based on your example, you could do:

kent$  awk 'BEGIN{ while( getline a < "file") {sub(/[^:]*$/,"",a); print a}}' 
Srednia ocen Wojciech Zieba:
Srednia ocen Jakub Pelc:
Srednia ocen Jan Resiak:

Upvotes: 1

user647772
user647772

Reputation:

No need for awk, use cut and sed:

$ cat in.txt 
Srednia ocen Wojciech Zieba: 3.83
Srednia ocen Jakub Pelc: 3.76
Srednia ocen Jan Resiak: 3.90
$ cut -d: -f1 in.txt | sed 's/$/:/'
Srednia ocen Wojciech Zieba:
Srednia ocen Jakub Pelc:
Srednia ocen Jan Resiak:

Upvotes: 1

Related Questions