You can try this,
printline() { for f in create*; do sed -n "$1{p;q}" $f; done ;}
题
I'd like to write an alias that would print particular line from all files in a folder, i.e.:
$ print_line 20
- would print 20th line from all files in a folder.
I spent hour with various constructions in .bashrc (some examples below), with lot of different escaping, but so far nothing worked. Thanks for any help.
alias a="for f in create*; do sed -n '\$1{p;q}' $f; done"
alias b="for f in create*; do awk 'NR==\$1{print $0}' $f; done"
function l { r=$(echo \'"$1"{p;q}\'); sed -n "$r" *; }
解决方案
You can try this,
printline() { for f in create*; do sed -n "$1{p;q}" $f; done ;}
其他提示
bash aliases, unlike those in csh, do not allow parameters; see Make a Bash alias that takes a parameter? and Passing argument to alias in bash, and How to pass command line arguments to a shell alias? and How do I include parameters in a bash alias?
So you need a function. If you have the GNU sed with the -s
switch, the following will work:
printline() { sed -ns "$1 p" create*;}
Otherwise, slightly modifying sat's nice answer, use a for-loop:
printline() { for f in create*; do sed -n "$1 p" "$f"; done;}
Note the space after the {
is necessary. The use of double-quotes in "$f"
guards against filenames with bad characters, such as spaces or stars.