简体   繁体   English

每两行显示一行(osX)zsh

[英]display a line every two line (osX) zsh

I'd to display every two line, a line from a file. 我要显示每两行,一行文件。 I've seen the sed -n 'f~d' awk and per l method. 我已经看过sed -n 'f~d' awkper l方法。 But the sed one doesn't work on osX (As I understood) and the two others are are interpreted languages which i can't use. 但是sed one不适用于osX(据我所知),另外两个是我无法使用的解释语言。

Can you help me ? 你能帮助我吗 ?

Here's an exemple : 这是一个例子:

output before : 输出之前:

-rw-r--r--  1 mfassi-f  2013  22 Jul 17 12:36 test.sh
-rw-r--r--  1 mfassi-f  2013  29 Jul 17 12:30 test1.sh
-rw-r--r--  1 mfassi-f  2013  22 Jul 17 12:36 test2.sh
-rw-r--r--  1 mfassi-f  2013  29 Jul 17 12:30 test3.sh
-rw-r--r--  1 mfassi-f  2013  22 Jul 17 12:36 test4.sh
-rw-r--r--  1 mfassi-f  2013  29 Jul 17 12:30 test5.sh
-rw-r--r--  1 mfassi-f  2013  22 Jul 17 12:36 test6.sh

output after : 输出后:

-rw-r--r--  1 mfassi-f  2013  29 Jul 17 12:30 test1.sh
-rw-r--r--  1 mfassi-f  2013  29 Jul 17 12:30 test3.sh
-rw-r--r--  1 mfassi-f  2013  29 Jul 17 12:30 test3.sh
-rw-r--r--  1 mfassi-f  2013  29 Jul 17 12:30 test5.sh

Here are two answers. 这是两个答案。 One for a file, and one for command-line input. 一个用于文件,一个用于命令行输入。

['cause the question's changed ever so slightly, but these two seemed too similar to put as independent answers]. [因为问题变得如此轻微,但这两个看起来太相似而不能作为独立答案]。

You can use zsh , ls , cut and paste to do this in a for loop. 您可以使用zshlscutpaste在for循环中执行此操作。 It's not the cleanest solution, but it does work (surprisingly). 这不是最干净的解决方案,但确实有效(令人惊讶)。

for file in `ls -1 | paste - - | cut -f 1`
do
   ls -l -d $file
done

We take the output of ls -1 , then extract every second filename. 我们取ls -1的输出,然后提取每秒文件名。 (The way ls chooses to sort the files will have an impact here). (方式ls选择将文件进行排序将在这里产生影响)。 Then, we do ls -l -d on each of these files. 然后,我们对每个文件执行ls -l -d -d is necessary to stop ls from showing us the contents of $file , if $file is a directory. 如果$file是一个目录, -d是必要的,以阻止ls向我们显示$file的内容。 (Not sure if this is OS X specific, or if that's default POSIX ls behaviour). (不确定这是否是OS X特定的,或者是否是默认的POSIX ls行为)。


Second answer: display every second line from a file. 第二个答案:显示文件中的每隔一行。

If you're after a mostly zsh solution, you could do something like the following: 如果你是一个大多之后zsh解决方案,你可以做类似如下:

$ jot 8 0 7 >> sample.txt # Generate some numbers. 
$ count=0                 # Storage variable
$ for i in `cat sample.txt` 
do
if [ $(( $count % 2 )) -eq 0 ] ; then
echo $i
fi
count=`expr $count + 1`
done

This displays every second line. 这显示每隔一行。

Notes: 笔记:
- This leaves a variable count in your session afterwards (it's not clean). - 这会在你的会话中留下一个变量count (它不干净)。
- This fails badly if sample.txt does not contain a single word per line. -这严重失败,如果sample.txt不包含每行一个字。
- I'm almost sure that the modulus comparison I do isn't the most efficient: I grabbed it from here . - 我几乎可以肯定我所做的模数比较不是最有效的:我从这里抓住它。
- I say it's mostly zsh because it does rely on cat , but I'm not sure how to avoid that. - 我说它主要是 zsh,因为它确实依赖于cat ,但我不确定如何避免这种情况。

The OS X version of sed is frustrating. OS X版本的sed令人沮丧。 Using sed -n '0~2p' <filename> doesn't work because, in the BSD sed, -n does something different: 使用sed -n '0~2p' <filename>不起作用,因为在BSD sed中, -n做了不同的事情:

-n -n
By default, each line of input is echoed to the standard output after all of the commands have been applied to it. 默认情况下,在将所有命令应用到标准输出后,每行输入都会回显到标准输出。 The -n option suppresses this behavior. -n选项可以抑制此行为。

I'd highly recommend installing GNU sed, which can be done using Homebrew : 我强烈建议安装GNU sed,这可以使用Homebrew来完成:

brew install gnu-sed

And then you can use: 然后你可以使用:

gsed -n '0~2p' filename # Display the 2nd, 4th etc
gsed -n '1~2p' filename # Display the 1st, 3rd etc. 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM