Using cut and sed command at the same time in a script

8,770

Solution 1

When you chained on the sed command with && you told the shell to run that command separately. That left the cat ... cut ... sort output alone and then also printed the 2nd line, without any cutting.

If you want to print only the second (sorted) line of input, change it to:

cut -d ' ' --fields=2,3 | sort | sed -n 2p

There's no need to repeat /dev/stdin, as both cut and sed will read stdin when you don't provide an input file.


To print the 2nd and 3rd space-delimited fields from a file, as well as print the entire 2nd line, you could do it all within sed:

sed -e 2p -e 's/^[^ ]* //' < report.txt | sort

This tells sed two execute two programs:

  • print line 2, and
  • search and replace: from the beginning of the line, zero or more non-space characters, followed by a space ... with: (nothing)

When run on your updated sample input, I get:

Books 5
Colorpen 2
Eraser 1
Khata 3
Pen 12
Sakib Khata 3
Sharpner 1

... since Sakib Khata 3 is the 2nd line of the file.

Solution 2

Read the contents of /dev/stdin to a variable:

content=$(cat)
echo "$content" | cut -d ' ' --fields=2,3 | sort
echo "$content" | sed -n '2p'

cat defaults to reads from stdin.

Example:

(
  content=$(cat);
  echo "$content" | cut -d ' ' --fields=2,3 | sort;
  echo "$content" | sed -n '2p'
) < <(echo -e "a b c\nd e f\ng h i\n")


b c
e f
h i
d e f

Update:

Example from your edited question

echo 'Jakaria Books 5
Sakib Khata 3
Afzal Pen 12
Sharif Colorpen 2
Sakib Eraser 1
Sharif Sharpner 1' | (
    content=$(cat);
    echo "$content" | cut -d ' ' --fields=2,3 | sort;
    echo "$content" | sed -n '2p'
)

Books 5
Colorpen 2
Eraser 1
Khata 3
Pen 12
Sharpner 1
Sakib Khata 3
Share:
8,770

Related videos on Youtube

selectiveduplicate
Author by

selectiveduplicate

Updated on September 18, 2022

Comments

  • selectiveduplicate
    selectiveduplicate over 1 year

    I have the following code in a Bash script:

    #!/bin/bash
    
    # gives summary of my buyings in the report.txt file
    
    echo Here is the summary of your purchases:
    echo =========================================
    echo
    
    # /dev/stdin is the standard input, i.e. the file
    cat /dev/stdin | cut -d ' ' --fields=2,3 | sort && sed -n '2 p' /dev/stdin 
    

    So what I want is to print the 2nd and 3rd fields with space as delimiter and also print the second line of report.txt using sed. So how can I do that?

    UPDATE:

    The contents of my report.txt are:

    Jakaria Books 5
    Sakib Khata 3
    Afzal Pen 12
    Sharif Colorpen 2
    Sakib Eraser 1
    Sharif Sharpner 1
    

    I edited my script as suggested and now it's the following:

    #!/bin/bash
    
    # gives summary of my buyings in the report.txt file
    
    echo Here is the summary of your purchases:
    echo =========================================
    
    # /dev/stdin is the standard input, i.e. the file
    cat /dev/stdin | cut -d ' ' --fields=2,3 | sort | sed -n '2 p'
    

    I run my script as cat report.txt | ./summary where summary is my script file name. Now the output is Books 5.

    Apart from printing the 2nd and 3rd fields I also want to print the 2nd line of the report.txt, i.e. the output should be the following:

    Books 5
    Colorpen 2
    Eraser 1
    Khata 3
    Pen 12
    Sharpner 1
    Sharif Sharpner 1
    
    • Jeff Schaller
      Jeff Schaller over 5 years
      I assume you run this script like: /path/to/script < report.txt ?
    • pLumo
      pLumo over 5 years
      Please add some sample from report.txt
    • Jeff Schaller
      Jeff Schaller over 5 years
      You say you want to print the 2nd line of report.txt, but the sample report.txt's 2nd line is "Sakib Khata 3", which doesn't show up in your sample output.
    • Jeff Schaller
      Jeff Schaller over 5 years
      Not knowing which 2nd line you meant, I updated my answer to show a sed-only option (plus the existing |sort).
    • pLumo
      pLumo over 5 years
      My answer does exactly what you want since the beginning, but you don't seem to have seen it...
  • Kusalananda
    Kusalananda over 5 years
    The cat utility will read from standard input by default if no filename is given to it.
  • pLumo
    pLumo over 5 years
    cool ! edited..
  • selectiveduplicate
    selectiveduplicate over 5 years
    I just updated my thread with more info. Can you kindly check it again? Thanks...