How do I used sed to remove a whole column of data in unix?

Viewed 74

I need some help with getting rid of all the driver names from the file using a sed command. I've tried multiple different commands but I cant get it to function properly.

Input:

Rank Country         Driver       Races Wins
1 [United_Kingdom] Lewis_Hamilton 264 94
2 [Germany] Sebastian_Vettel      254 53
3 [Spain] Fernando_Alonso         311 32
4 [Finland] Kimi_Raikkonen        326 21
5 [Germany] Nico_Rosberg          200 23

I've tried using cat f1.txt| sed -r 's/\S+//3' but it does not keep the formatting I would like

what i would like it to do

Input:

Rank Country         Driver       Races Wins
1 [United_Kingdom]                 264 94
2 [Germany]                        254 53
3 [Spain]                          311 32
4 [Finland]                        326 21
5 [Germany]                        200 23
4 Answers

The easy way to do it is just replace $3 with spaces equal to the length of the field, e.g.

awk 'FNR > 1{s=sprintf("%*s", length($3) + 1, " "); sub($3,s)}1' drivers

The condition FNR > 1 simply skips the 1st line in the file to preserve all headings. +1 was added to the length of the 3rd field to match your exact output format.

Example Use/Output

$ awk 'FNR > 1{s=sprintf("%*s", length($3) + 1, " "); sub($3,s)}1' drivers
Rank Country         Driver       Races Wins
1 [United_Kingdom]                 264 94
2 [Germany]                        254 53
3 [Spain]                          311 32
4 [Finland]                        326 21
5 [Germany]                        200 23

Note: If your input file actually contains "Input:" at the beginning, then you would use:

awk 'NF == 5 && $1 ~ /^[0-9]/ {s=sprintf("%*s", length($3)+1, " "); sub($3,s)}1' drivers

Here the condition 'NF == 5 && $1 ~ /^[0-9]/ only applies the substitution to records (lines) with five-fields beginning with a digit.

Output

$ awk 'NF == 5 && $1 ~ /^[0-9]/ {s=sprintf("%*s", length($3)+1, " "); sub($3,s)}1' drivers
Input:

Rank Country         Driver       Races Wins
1 [United_Kingdom]                 264 94
2 [Germany]                        254 53
3 [Spain]                          311 32
4 [Finland]                        326 21
5 [Germany]                        200 23

I was unclear if that was actually part of the file.

Could you please try following, this could take care of condition in case name field could have any spaces in it(and considering names wouldn't have digits in them).

awk '
match($0,/\] [^0-9]*/){
  val=substr($0,RSTART,RLENGTH)
  s=sprintf("%*s", length(val) + 1, " ")
  print substr($0,1,RSTART) s substr($0,RSTART+RLENGTH)
  next
}
1
'  Input_file

I would do it using AWK following way, let file.txt content be:

Rank Country         Driver       Races Wins
1 [United_Kingdom] Lewis_Hamilton 264 94
2 [Germany] Sebastian_Vettel      254 53
3 [Spain] Fernando_Alonso         311 32
4 [Finland] Kimi_Raikkonen        326 21
5 [Germany] Nico_Rosberg          200 23

then

awk 'BEGIN{FS=OFS="]"}{gsub(/[[:alpha:]_]/," ",$2)}{print}' file.txt

output

Rank Country         Driver       Races Wins
1 [United_Kingdom]                264 94
2 [Germany]                       254 53
3 [Spain]                         311 32
4 [Finland]                       326 21
5 [Germany]                       200 23

Explanation: I globally replace letters and _ with spaces which are after ], to do so I set field separator to ] and use gsub at 2nd column i.e. everything after ]. I also set output field separator to ] to preserve it.

This might work for you (GNU sed):

sed -E '1b;:a;s/^((\S+\s+){2})[A-Z_]/\1 /i;ta' file

After the first line, replace the third field with spaces.

Alternative:

sed -E '1b;s/\S+/\n&\n/3;h;s/[^\n]/ /g;H;g;s/\n.*\n(.*)\n.*\n(.*)\n.*/\2\1/' file
Related