Question

a) Write a one-line sed command that will replace all multiple spaces in a file called...

a) Write a one-line sed command that will replace all multiple spaces in a file called names.txt (this is an arbitrary file that you may create yourself) by only one space.

b) Consider the following file called system.txt:

1  2  3  4

5  6  7  8

9  He 11 Hg

Ts H  15 Be

17 Mc Li 20

Write a one-line sed command that replaces the second number in every line with the string “system”, without the quotes. For example, after running the appropriate sed command, the following would be output to the terminal:

1  system  3  4

5  system  7  8

9  He system Hg

Ts H  15 Be

17 Mc Li system

c) Consider the following file called swap.txt:

349 230 481

800 456 129

043 942 584

901 627 874

Each three-digit number is separated by a space. Write a one-line sed command that performs in-place substitution to swap the first digit with the third digit of the first number in every line. For example, after running the appropriate sed command, the swap.txt file would contain:

943 230 481

008 456 129

340 942 584

109 627 874

For bonus, can you swap the first digit with the third digit of the second number in every line? Do not worry about performing in-place substitution for this part. For example, after running the appropriate sed command, the following would be output to the terminal:

349 032 481

800 654 129

043 249 584

901 726 874

0 0
Add a comment Improve this question Transcribed image text
Answer #1

First question is :-

-----------------------------------

1.Write a one-line sed command that replace all multiple spaces in “name.txt” with only one space.

------------------------------------

Answer:-

------------------------------------

Suppose “names.txt” has the following content

-----------

Hi How Are You

------------

IF you observe the above content has multiple spaces.

-----------------------------------

Command--------------sed 's/ */\ /g' names

-----------------------------------

Output:-

Hi How Are You

=======================

Output images as follows:-

Applications Places System 8 嘞 圆Sun Sep 24, 12:01 PM a seenu names (-/Desktop)-gedit File Edit View Search Tools Documents He

Applications Places System 8-1) Sun Sep 24, 12:00 PM aseenu d) seenu@ubuntu:-/Desktop ni File Edit View Search Terminal Help

2. Second question is :-

-----------------------------------

1.Write a one-line sed command that replace second number in every line with the string “system”

------------------------------------

Answer:-

------------------------------------

Suppose “system.txt” has the following content

-----------

1 2 3 4

5 6 7 8

9 He 11 Hg

Ts H 15 Be

17 Mc Li 20

----------------------

Command--------------- sed -E 's/[0-9]+/system/2' system

----------------------

Output:-

1 system 3 4

5 system 7 8

9 He system Hg

Ts H 15 Be

17 Mc Li system

------------------------

Ouput Images as follows:-

Applications Places System 8 빼) 圆Sun Sep 24, 1:02 PM aseenu。 esystem (-/Desktop) -gedit File Edit View Search Tools Documents

Applications Places System ti Sun Sep 24, 1:03 PM seenu seenu@ubuntu: /Desktop hi File Edit View Search Terminal Help seenu@u

For the better understand of 1st question i replace "double spaces" with "?" see below image

Applications Places System ti Sun Sep 24, 1:36 PM seenu seenu@ubuntu: /Desktop File Edit View Search Terminal Help seenu@ubun

For the better understand of 2nd question i replace "second number in every line" with "hello" see below image

Applications Places System t↓ , sun sep 24, 1:38 PM aseenu d seenu@ubuntu: /Desktop File Edit View Search Terminal Help seenu




#please post a separate question for the remaining 2 parts

Add a comment
Know the answer?
Add Answer to:
a) Write a one-line sed command that will replace all multiple spaces in a file called...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT