1

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
  • I assume you run this script like: /path/to/script < report.txt ? – Jeff Schaller Aug 22 '18 at 14:11
  • Please add some sample from report.txt – pLumo Aug 22 '18 at 14:12
  • 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 Aug 22 '18 at 17:44
  • Not knowing which 2nd line you meant, I updated my answer to show a sed-only option (plus the existing |sort). – Jeff Schaller Aug 22 '18 at 17:59
  • My answer does exactly what you want since the beginning, but you don't seem to have seen it... – pLumo Aug 23 '18 at 6:11
3

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.

  • I just updated my thread with more info. Can you kindly check it again? Thanks... – Apollonius Aug 22 '18 at 17:13
1

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
  • 1
    The cat utility will read from standard input by default if no filename is given to it. – Kusalananda Aug 22 '18 at 14:45
  • cool ! edited.. – pLumo Aug 22 '18 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.