おれさまラボ

実際に手を動かして理解を深めるブログ。

Bash - ある順番のもの以降の引数を全部取得する

こんな書き方あるんだー!と感動したので、久々のメモ。

${@}はすべての引数を指しますが、${@:2}のように書くと、2つ目の引数以降すべてを指すことができます。

こんな感じで、テストスクリプトを書いてみました。

#/bin/bash

FILE=$1
GREP_WORD=${@:2}

trap 'exit' 2

grep ${GREP_WORD} ${FILE}

マーク・ザッカーバーグハーバード大学卒業式の英文をGrepしてみましょう。

$ head Harvard_Commencement_2017.txt
Harvard Commencement 2017
MARK ZUCKERBERG・2017年5月26日
President Faust, Board of Overseers, faculty, alumni, friends, proud parents, members of the ad board, and graduates of the greatest university in the world,
I'm honored to be with you today because, let's face it, you accomplished something I never could. If I get through this speech, it'll be the first time I actually finish something at Harvard. Class of 2017, congratulations!
I'm an unlikely speaker, not just because I dropped out, but because we're technically in the same generation. We walked this yard less than a decade apart, studied the same ideas and slept through the same Ec10 lectures. We may have taken different paths to get here, especially if you came all the way from the Quad, but today I want to share what I've learned about our generation and the world we're building together.
But first, the last couple of days have brought back a lot of good memories.
How many of you remember exactly what you were doing when you got that email telling you that you got into Harvard? I was playing Civilization and I ran downstairs, got my dad, and for some reason, his reaction was to video me opening the email. That could have been a really sad video. I swear getting into Harvard is still the thing my parents are most proud of me for.
What about your first lecture at Harvard? Mine was Computer Science 121 with the incredible Harry Lewis. I was late so I threw on a t-shirt and didn't realize until afterwards it was inside out and backwards with my tag sticking out the front. I couldn't figure out why no one would talk to me -- except one guy, KX Jin, he just went with it. We ended up doing our problem sets together, and now he runs a big part of Facebook. And that, Class of 2017, is why you should be nice to people.
But my best memory from Harvard was meeting Priscilla. I had just launched this prank website Facemash, and the ad board wanted to "see me". Everyone thought I was going to get kicked out. My parents came to help me pack. My friends threw me a going away party. As luck would have it, Priscilla was at that party with her friend. We met in line for the bathroom in the Pfoho Belltower, and in what must be one of the all time romantic lines, I said: "I'm going to get kicked out in three days, so we need to go on a date quickly."
Actually, any of you graduating can use that line.

検索ワードとして、Faebookを検索してみます。

$ ./test.sh Harvard_Commencement_2017.txt Facebook
What about your first lecture at Harvard? Mine was Computer Science 121 with the incredible Harry Lewis. I was late so I threw on a t-shirt and didn't realize until afterwards it was inside out and backwards with my tag sticking out the front. I couldn't figure out why no one would talk to me -- except one guy, KX Jin, he just went with it. We ended up doing our problem sets together, and now he runs a big part of Facebook. And that, Class of 2017, is why you should be nice to people.
I didn't end up getting kicked out -- I did that to myself. Priscilla and I started dating. And, you know, that movie made it seem like Facemash was so important to creating Facebook. It wasn't. But without Facemash I wouldn't have met Priscilla, and she's the most important person in my life, so you could say it was the most important thing I built in my time here.
I remember the night I launched Facebook from my little dorm in Kirkland House. I went to Noch's with my friend KX. I remember telling him I was excited to connect the Harvard community, but one day someone would connect the whole world.
That was my hardest time leading Facebook. I believed in what we were doing, but I felt alone. And worse, it was my fault. I wondered if I was just wrong, an imposter, a 22 year-old kid who had no idea how the world worked.
If I had to understand everything about connecting people before I began, I never would have started Facebook.
An entrepreneurial culture thrives when it's easy to try lots of new ideas. Facebook wasn't the first thing I built. I also built games, chat systems, study tools and music players. I'm not alone. JK Rowling got rejected 12 times before publishing Harry Potter. Even Beyonce had to make hundreds of songs to get Halo. The greatest successes come from having the freedom to fail.
We all know we don't succeed just by having a good idea or working hard. We succeed by being lucky too. If I had to support my family growing up instead of having time to code, if I didn't know I'd be fine if Facebook didn't work out, I wouldn't be standing here today. If we're honest, we all know how much luck we've had.

grep-oオプションをつけてみましょう。

$ ./test.sh Harvard_Commencement_2017.txt -o Facebook
Facebook
Facebook
Facebook
Facebook
Facebook
Facebook
Facebook

Facebookだけとりだせました。

他にもいろいろと使えそうですね。

参考

AWS CLIを利用してS3上のファイルをgrepする | Developers.IO

シェルスクリプトで、ある順番以降の引数を取得する - Qiita