简体   繁体   中英

How to insert '|' between two words?

I have to find average of marks in the following way:

The input file format is

Student name#class#marks1#marks2

The output file format is

Student name|class|marks 1|marks 2|average marks

I am using Ubuntu and I am very new in shell scripting. I know the normal way of coding the above in python but not in this above specific format. Please help.

Also, please tell if there is any way of writing the above program without using echo command.

您的规范是咳嗽相当缺乏,但...

awk 'BEGIN{RS="#";ORS="|"}{print $1,$2,$3,$4,($3+$4)/2 }' file
sed 's/#/|/g'

will substitute every # for a | .

Example:

cat <<EOF >file.txt
Student name#class#marks1#marks2
EOF

sed -i 's/#/|/g' file.txt

cat file.txt

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.

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