How do I remove leading spaces in UNIX when count of space character is not fixed? Example below-

Script showStreamsGLIS$reg.$env.ksh gives me output as below-

Job  Stime Etime Status ExitCode
      GLIS-AS-S-EFL-LOCK-B   -----                -----                OI 103313880/0
    GLIS-ALL-Q-EOD-FX-UPDT-1730-B -----                -----                TE 0/0
    GLIS-TK-S-BWSOD-B        -----                -----                OI 191344064/0
      GLIS-TK-S-BWSODWAIT-B  -----                -----                OI 191344064/0
GLIS-ALL-S-BWEC-B            10/06/2016  10:48:12 -----                RU 196982837/1
  GLIS-ALL-S-BWEC-START-B    10/06/2016  10:48:13 -----                RU 196982837/1
    GLIS-ALL-S-BWEC-GATHER   10/06/2016  10:48:16 -----                RU 196982837/1
    GLIS-ALL-S-BWEC-BWPP-BAL-J -----                -----                OI 134547446/0
    GLIS-ALL-S-BWEC-BWBM-BALS-J -----                -----                OI 134547446/0
GLIS-ALL-S-EOD-FX-UPDT-1730-B -----                -----                OI 0/0
     GLIS-ALL-R-EOD-FX-UPDT-1730-B -----                -----                ST 0/0
    GLIS-ALL-Q-EOD-FX-UPDT-1730-B -----                -----                TE 0/0

My requirement is to get rows containing status as 'OI/OH/RU' (highlighted few possible status values) and remove all trailing spaces. And I am trying to integrate this code in a perl script.

Idea is to do it without saving the output in a file.

Hello Tanu,

Could you please try following and let me know if this helps you.

awk '{sub(/^ +/,X,$0);print}'   Input_file

Thanks,
R. Singh

I am using below and it is giving me error- egrep: write error: Broken pipe

my @pendJobs = `showStreamsGLIS$reg.$env.ksh  | egrep ' OH | OI | RU ' | awk '{sub(/^ +/,X,$0);print}'`;

P.S: I am getting the output from a script and I don't want to use a file to temporary store the data and perform these activities. Trying to fetch the rows in a single command.

Hello Tanu,

Without sample Input_file or without letting us know what you are trying to achieve, we all could guess only. So as a guess could you please try following and let us know how it goes then(seems you are using perl and didn't test it).

 my @pendJobs = `showStreamsGLIS$reg.$env.ksh  | awk '/OH|RU|OI/{sub(/^ +/,X,$0);print}'`;
 

Thanks,
R. Singh

1 Like

Agree. Apologies for not providing proper requirement. Updated my original question.

This latest solution provided by you is not working as well. Error is "grep: write error: Broken pipe"

my piece of code is as below (nothing else in script except below calculation)-

my @pendJobs = `showStreamsGLIS$reg.$env.ksh  | awk '/OH|RU|OI/{sub(/^ +/,X,$0);print}'`;
chomp(@pendJobs);
print Dumper(\@pendJobs);
exit 1;

Hello Tanu,

So a important step of trouble shooting is either in any tool/code/operation is never neglect your error messages. So here we could see you script and then awk command is there, so it means there could be a chance your script itself is having issues, so following are the points:

  1. Please try to run your script independently, is it running or not?
  2. Post your script(if possible) or let us know what it does with complete details.
  3. Check if you have used egrep command in script and try to figure out if that works or not?

Please follow above steps and additional too if you get any and let us know how it goes then.

Thanks,
R. Singh

1 Like

Thanks Ravindra.

I had egrep in my code (but strange that line was commented).
I did remove it and added escape character (\$0) and it worked.

my @pendJobs = `/ms/dev/saperp/sapbatchtools/$env/install/common/bin/autosys/showStreamsGLIS$reg.$env.ksh  | awk '/OH|RU|OI/{sub(/^ +/,X,\$0);print}'`;

Could you please explain the below highlighted part of your command-

awk '/OH|RU|OI/{sub(/^ +/,X,$0);print}';

Hello Tanu,

Could you please go through following and let me know if this helps you.

awk '/OH|RU|OI/    ####So here we are searching for lines which have strings "OH" or "RU" or "OI", if yes then do following operations.
{sub(/^ +/,X,$0);  ####Here I am using awk's built-in substitute functionality by using sub, whose syntax is--> sub(/pattern or value which needs to be substituted/,new_value,line/variable), so here 
                       I am substituting each line which is starting with space by doing (^ ) with X(a variable) whose value is NULL in current line/record by mentioning $0.
print}             ####printing line here, so either previous line's leading spaces were removed or not print will happen(as per your shown code only prepared it) in both the cases.
';
 

Thanks,
R. Singh

2 Likes

great Thanks. I'll read more about awk built-in functions.