Tarek Sayed
Tarek Sayed

Reputation: 11

Can any one help me to understand this?

I'm new to shell scripting and I Can't understand those lines:

wc -l $x|sed 's/\s\+/|/g'

rc=`echo "$BTEQ_OUT"|grep "RC (return code)"| sed 's/ //g' | cut -d '=' -f2|tr -d "\r\n "`;

Upvotes: 0

Views: 63

Answers (2)

Jordan Simba
Jordan Simba

Reputation: 1206

wc -l $x|sed 's/\s\+/|/g'

wc is a tools used for counting, with the -l flag, this will count the lines in a file or a string.

$x is the variable holding probably a file name to be passed into wc

| called 'pipe' passes the output of the command before as the input into the command after

sed is another scripting tool used to edit text in files.

's/\s\+/|/g' is regex which globally (g) substitutes any number of white space chars with pipe symbols '|'

This program does the following

Count how many lines are in $x and whatever you output replace empty characters with pipe symbols.

The fact that they expect multiple outputs from wc -l hints that $x might store more than one file ...

I'd suggest looking into what some of the other commands are and what they do, and how they interact. List below

echo 
tr
cut
pipe

Upvotes: 1

glenn jackman
glenn jackman

Reputation: 246744

When you see a long pipeline, one useful technique for understanding it is to execute it piece by piece:

  1. first, what's in $x?

    echo $x
    

    is that the name of a file?

    ls -l $x
    
  2. what does wc do?

    wc -l $x
    
  3. ok, what does the sed part do? (note, \s requires GNU sed)

    wc -l $x | sed 's/\s\+/|/g'
    

Similarly:

echo "$BTEQ_OUT"
echo "$BTEQ_OUT"|grep "RC (return code)"
echo "$BTEQ_OUT"|grep "RC (return code)"| sed 's/ //g'
echo "$BTEQ_OUT"|grep "RC (return code)"| sed 's/ //g' | cut -d '=' -f2
echo "$BTEQ_OUT"|grep "RC (return code)"| sed 's/ //g' | cut -d '=' -f2|tr -d "\r\n ";

Upvotes: 1

Related Questions