Home > Enterprise >  How do I prevent the repetition of "Hello" in this script file?
How do I prevent the repetition of "Hello" in this script file?

Time:02-22

Its suppose to display the output username followed by :Hello but it repeats before moving onto the next username displayed.

SCRIPT:

#!/bin/sh
tail -n  17 $1| awk -F' ' '{print substr($1,1,1) substr($2,1,1) substr($3,1,1) substr($5,length($5)-4 1,length($5)) ": Hello"  }'

What I am aiming for:

SAB7448 :Hello

BIC3999 :Hello

What the output is:

SAB7448 :Hello

 :Hello

BIC3999 :Hello

 :Hello

Am I overlooking something? Thank you for your time

(example) Input file:

OFFICIAL CLASS LIST    SPRING 2032

REGISSTRAR'S OFFICE

SYSTEM ADMIN AND SECURITY  CPSC 327 01


16 STUDENTS ENROLLED IN THIS SECTION

First.Name Middle.Init Last.Name  Soc.Sec.No  Campus.Phone
 
SAFFRON    A           BARR       XXX-XX-XXXX 724/555-7448

BRITANY    I           COSTA      XXX-XX-XXXX 724/555-3999

MARGIE     E           CRUZ       XXX-XX-XXXX 724/555-0290

IVY        M           ELLWOOD    XXX-XX-XXXX 724/555-7468

TYLER      V           FINNEY     XXX-XX-XXXX 724/555-3138

CARLEY     D           FISCHER    XXX-XX-XXXX 724/555-4371

TONYA      Y           GALLOWAY   XXX-XX-XXXX 724/555-5920

ANWEN      K           HAMPTON    XXX-XX-XXXX 724/555-1241

FERN       S           KIRBY      XXX-XX-XXXX 724/555-8804

HENLEY     R           LANG       XXX-XX-XXXX 724/555-8935

RILEY      C           MCCOY      XXX-XX-XXXX 724/555-8189

LOUISA     D           RATLIFF    XXX-XX-XXXX 724/555-1207

SULEMAN    L           SHAW       XXX-XX-XXXX 724/555-3201

SHAUNIE    N           SPENCER    XXX-XX-XXXX 724/555-8218

HARLEN     M           SUTTON     XXX-XX-XXXX 724/555-6613

JACOB      G           WHITLEY    XXX-XX-XXXX 724/555-3489

CodePudding user response:

Two points. Your tail command must be reviewed, to ensure it will keep the lines you actually want to process. I assumed that your data file is exactly like that:

data.txt

OFFICIAL CLASS LIST    SPRING 2032

REGISSTRAR'S OFFICE

SYSTEM ADMIN AND SECURITY  CPSC 327 01


16 STUDENTS ENROLLED IN THIS SECTION

First.Name Middle.Init Last.Name  Soc.Sec.No  Campus.Phone
 
SAFFRON    A           BARR       XXX-XX-XXXX 724/555-7448

BRITANY    I           COSTA      XXX-XX-XXXX 724/555-3999

MARGIE     E           CRUZ       XXX-XX-XXXX 724/555-0290

IVY        M           ELLWOOD    XXX-XX-XXXX 724/555-7468

TYLER      V           FINNEY     XXX-XX-XXXX 724/555-3138

CARLEY     D           FISCHER    XXX-XX-XXXX 724/555-4371

TONYA      Y           GALLOWAY   XXX-XX-XXXX 724/555-5920

ANWEN      K           HAMPTON    XXX-XX-XXXX 724/555-1241

FERN       S           KIRBY      XXX-XX-XXXX 724/555-8804

HENLEY     R           LANG       XXX-XX-XXXX 724/555-8935

RILEY      C           MCCOY      XXX-XX-XXXX 724/555-8189

LOUISA     D           RATLIFF    XXX-XX-XXXX 724/555-1207

SULEMAN    L           SHAW       XXX-XX-XXXX 724/555-3201

SHAUNIE    N           SPENCER    XXX-XX-XXXX 724/555-8218

HARLEN     M           SUTTON     XXX-XX-XXXX 724/555-6613

JACOB      G           WHITLEY    XXX-XX-XXXX 724/555-3489

Based on this data, I want to remove the first 11 lines from the processed data. So I use

tail -n  11 data.txt

And then I process the file like you did, but added a filter to ensure empty lines are not processes (and therefore avoid :Hello lines):

tail -n  11 data.txt | awk -F' ' '!/^$/ { print substr($1,1,1) substr($2,1,1) substr($3,1,1) substr($5,length($5)-4 1,length($5)) ": Hello"  }'

What was added is !/^$/ .

  • This matches lines that are not empty.
  • ! do not match the following pattern.
  • // pattern delimiter.
  • ^ line start
  • $ line end
  • ^$ starts and ends, with nothing in between == empty line.

Output

SAB7448: Hello
BIC3999: Hello
MEC0290: Hello
IME7468: Hello
TVF3138: Hello
CDF4371: Hello
TYG5920: Hello
AKH1241: Hello
FSK8804: Hello
HRL8935: Hello
RCM8189: Hello
LDR1207: Hello
SLS3201: Hello
SNS8218: Hello
HMS6613: Hello
JGW3489: Hello
  •  Tags:  
  • bash
  • Related