| |

VerySource

 Forgot password?
 Register
Search
View: 2344|Reply: 10

With a shell script?

[Copy link]

5

Threads

14

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-1-28 22:00:02
| Show all posts |Read mode
How to write a shell script to display the odd number of lines (1, 3, 5 .... lines) for a given file?
Reply

Use magic Report

0

Threads

25

Posts

19.00

Credits

Newbie

Rank: 1

Credits
19.00

 China

Post time: 2020-3-3 14:15:01
| Show all posts
sed -n "p; n" / etc / profile
Reply

Use magic Report

0

Threads

25

Posts

19.00

Credits

Newbie

Rank: 1

Credits
19.00

 China

Post time: 2020-3-3 17:30:02
| Show all posts
awk '{if (NR% 2 == 1) print $ 0}' / etc / profile
Reply

Use magic Report

0

Threads

10

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 Invalid IP Address

Post time: 2020-3-4 21:15:01
| Show all posts
If it is a text file, you can process it directly
linenum = 1
lines = `cat filename`
for line in $ {lines}; do
   if [$ [$ {linenum}% 2] -ne 0]; then
           echo $ {line}
   fi
   linenum = $ [$ {linenum} +1]
done
Reply

Use magic Report

0

Threads

10

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 Invalid IP Address

Post time: 2020-3-5 05:15:01
| Show all posts
Of course, the previous methods are all correct.
Reply

Use magic Report

5

Threads

14

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

 Author| Post time: 2020-3-11 05:00:02
| Show all posts
Excuse me, how does the value of the variable line change?
Reply

Use magic Report

5

Threads

14

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

 Author| Post time: 2020-3-11 13:00:02
| Show all posts
#! / bin / sh
echo "input filename"
read fname
linenum = 1
lines = 'cat filename'
for linenum in $ {lines}; do
   if [$ [$ {linenum}% 2] -ne 0]; then
           echo $ {linenum}
   fi
   linenum = $ [$ {linenum} +1]
done


Is there something wrong with this?
Why can't I debug it?
Reply

Use magic Report

5

Threads

14

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

 Author| Post time: 2020-3-11 15:00:01
| Show all posts
#! / bin / sh
echo "input filename"
read fname
linenum = 1
lines = 'cat fname'
for linenum in $ {lines}; do
     if [$ [$ {linenum}% 2] -ne 0]; then
                     echo $ {linenum}
     fi
     linenum = $ [$ {linenum} +1]
done
 
 
Is there something wrong with this?
Why can't I debug it?
Reply

Use magic Report

0

Threads

10

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 Invalid IP Address

Post time: 2020-3-22 00:30:01
| Show all posts
Written like this $ fname
And your cat is also wrong. It is not a single quote, but a back quote. It is generally the one to the left of 1 on the keyboard, which means executing a shell command. It can also be replaced with $ ()
linenum = 1
read filename
lines = `cat $ {filename}`
for line in $ {lines}; do
if [$ [$ {linenum}% 2] -ne 0]; then
echo $ {line}
fi
linenum = $ [$ {linenum} +1]
done
Reply

Use magic Report

0

Threads

10

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 Invalid IP Address

Post time: 2020-3-22 15:15:02
| Show all posts
However, the above code is actually problematic, that is, there can be no more tokens such as spaces in a line, otherwise an error will occur. The better method is the1231456method.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list