Skip to content

Instantly share code, notes, and snippets.

@hyunjun
Last active October 15, 2020 14:32
Show Gist options
  • Save hyunjun/c8aa8398b60b366177385e8dc36d677d to your computer and use it in GitHub Desktop.
Save hyunjun/c8aa8398b60b366177385e8dc36d677d to your computer and use it in GitHub Desktop.
bash stdin
  • multiple stdin to single output

    $ cat test.dat
    Test
    Multiple
    input
    
    $ cat test.dat | ./multiple_stdin_to_single_output.sh
    Test Multiple input
    
    $ cat multiple_stdin_to_single_output.sh
    #!/bin/sh
    
    myvar=`cat`
    echo $myvar
    
  • multiple stdin to multiple output

    $ cat test.dat | ./multiple_stdin_to_multiple_output.sh
    Test
    Multiple
    input
    
    $ cat multiple_stdin_to_multiple_output.sh
    #!/bin/sh
    
    while read query
    do
      echo $query
    done
    
  • multiple stdin to output splitted by tab

    $ cat test.query
    갤빈클라인      캘빈클라인
    제첩국  재첩국
    
    $ cat test.query | ./test.sh
    갤빈클라인      캘빈클라인
    갤빈클라인
    캘빈클라인
    제첩국  재첩국
    제첩국
    재첩국
    
    $ cat test.sh
    #!/bin/sh
    
    while read query
    do
      echo "$query"
      norm="${query%$'\t'*}"
      oup="${query##*$'\t'}"
      echo $norm
      echo $oup
    done
    
  • how to split a string in shell and get the last field

#!/bin/sh
url='https://www.abc.com/123'
echo $url
protocol_removed=${url##*:}
echo $protocol_removed
slash_removed=${protocol_removed:2:${#protocol_removed}}
echo $slash_removed
slash_changed=${slash_removed/\//-}
echo $slash_changed
# Result
# https://www.abc.com/123
# //www.abc.com/123
# www.abc.com/123
# www.abc.com-123
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment