I need to remove the last 3 characters in alternating lines. 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:
~$ 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. This looks like X~Y, which will match every Y'th line starting with line X.
sed '2~2 s/...$//g'
Divide the number by 1000 and store the answer into an integer.
As such:
int a = 32768/1000;
The value stored in a will be 32
For the Alternate lines:
Use a loop that increments by 2 (assuming you store those numbers in an array)
for(a=0;a<6;a=a+2)
{
place code described above here;
}
Here's an awk program that does it:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.