简体   繁体   English

删除交替行的最后3个字符

[英]Remove last 3 characters on alternating lines

I need to remove the last 3 characters in alternating lines. 我需要删除交替显示的最后3个字符。 I have 我有

 36960
32768
40800
16384
22656
4096

I need to have output like 我需要像这样的输出

 36960
32
40800
16
22656
4

Could someone please help? 有人可以帮忙吗?

An awk solution: awk解决方案:

~$ cat file
 36960
32768
40800
16384
22656
4096
~$ awk '{if (NR%2){print $0}else{print substr($0, 0, length($0)-3)}}' file
 36960
32
40800
16
22656
4

Preceding a "sed" command with a "step" amount will do what you want. 在“ sed”命令前加上“ step”数量可以完成您想要的操作。 This looks like X~Y, which will match every Y'th line starting with line X. 看起来像X〜Y,它将匹配从X行开始的每个Y'行。

sed '2~2 s/...$//g'

Divide the number by 1000 and store the answer into an integer. 将数字除以1000,然后将答案存储为整数。

As such: 因此:

int a = 32768/1000; 整数a = 32768/1000;

The value stored in a will be 32 存储在中的值将为32

For the Alternate lines: 对于备用行:

Use a loop that increments by 2 (assuming you store those numbers in an array) 使用递增2的循环(假设您将这些数字存储在数组中)

for(a=0;a<6;a=a+2)
{ 
    place code described above here; 
}

Here's an awk program that does it: 这是一个执行该操作的awk程序:

if (NR % 2 == 0) {
  print gensub(/(.*).../, "\\1", "g"); 
}
else
{
 print $0
}

If I run it like this I get the shown output: 如果我像这样运行它,将得到显示的输出:

:> awk '{ if (NR % 2 == 0) { print gensub(/(.*).../, "\\1", "g")} else { print $0 }}' << EOL
>  36960
> 32768
> 40800
> 16384
> 22656
> 4096
> EOL
 36960
32
40800
16
22656
4

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

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