Question

Write a linux command that will add a "#" character to the start of every line...

Write a linux command that will add a "#" character to the start of every line that does not already start with a "#" character.


Write a linux command that will append all lines of the /etc/passwd file that contain the word "STUDENT" to the end of the file.


Write a linux command that will move the last word of each line to the start of the line.


Write a linux command that will add the line number to the beginning of each line. Note that vim will display line numbers - what you must do is insert the line number at the start of each line.


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

1. Write a linux command that will add '#' charracter to the start of every line that does not already start with '.#' charracter

Ans: You can do this with sed command shown below
      
     e.g sed 's/^./#/' inputfile.txt

     Explaination: sed   s/find string /replace with string/   


     s -- to search and replace
     ^ -- search at begining
     . -- It will avoid adding '#' to those line already starting with '#'
     # -- It is the charracter to be added. You can change it to any other


2.Write a linux command that will append all lines of the /etc/passwd file that contain the word "STUDENT" to the end of the file.

Sol: cat /etc/passwd | grep -i "STUDENT" >> /etc/passwd

    Command break and explanation:

    cat /etc/passwd :

    It will display the content of the file to screen.

    grep -i "STUDENT" :

    It will find the lines containing "STUDENT". -i is to ignore case.
    You should use it if lowercase "student" and uppercase "STUDENT" are same for u. Other wise simply use
    grep "STUDENT".

    >> /etc/passwd :
  
    >> filename . It is to append the output to a file at the end.
  
      

3.

Write a linux command that will move the last word of each line to the start of the line.

Ans: cat input.txt | awk '{lastword=$NF;NF=NF-1;print lastword,$0}'

     Logic and explanation:
  
     find the last word : In awk $NF stores the last word automatically.
     Store the last word in a variable : lastword = $NF
     Reassign the last word to the second last word. : $NF = NF -1
     print the lastword and remaining : print lastword, $0).

     e.g
  
input.txt:
---------
Hi I m neeraj
My last name is mishra
I m born in year 1992

Output:

/home/neeraj> cat input.txt | awk '{lastword=$NF;NF=NF-1;print lastword,$0}'
neeraj Hi I m
mishra My last name is
1992 I m born in year

Sorry for not answering the last one. I tried my best to answer. I hope you understood.

Add a comment
Know the answer?
Add Answer to:
Write a linux command that will add a "#" character to the start of every line...
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
  • Login to Suse Linux as root. Complete the task working as the root user. 1. What...

    Login to Suse Linux as root. Complete the task working as the root user. 1. What is your current directory. What did you enter at the command prompt to determine your current working directory? 2. Change directories if needed to find your foods (or food) file. Use vi to make the file contain 10 food items. Save your file and quit. Use the cat command, and option, and the argument food to display the food file with numbered lines. Use...

  • JAVA DATA STRUCTURES Write a line-based text editor. The command syntax is similar to the Unix...

    JAVA DATA STRUCTURES Write a line-based text editor. The command syntax is similar to the Unix line editor ed. The internal copy of the file is maintained as a linked list of lines. To be able to go up and down in the file, you have to maintain a doubly linked list. Most commands are represented by a one-character string. Some are two characters and require an argument (or two). Support the commands shown below: Command Function Go to the...

  • In a linux command line Give AWK commands for accomplishing each of the following: Print the...

    In a linux command line Give AWK commands for accomplishing each of the following: Print the 2nd last field (the field directly before the last field) of each line from a file named 'last.txt' Assume you have a file called 'names' that contains a list of people, one person per line. Also, assume that on each line the 3rd field on that line contains the age of the person. Some of the people do not have an age listed, and...

  • in vim editor: 1. Write a command that will change all "/" characters to "+" in...

    in vim editor: 1. Write a command that will change all "/" characters to "+" in the file. 2.Write a command that will add one blank line after every existing line in the file.

  • Using Ubuntu OS... help is greatly needed! Thank you. Create an alias called "home" that does...

    Using Ubuntu OS... help is greatly needed! Thank you. Create an alias called "home" that does two things... returns you to your home directory, then does a long listing of all files found there Create a file called "Output.txt" that contains the output of the "ls /etc" command Using "cat", create a file called "poem.txt" where you enter at least one line of text ("Mary had a little lamb" will do...) Using "cat", pipe the contents of poem.txt into the...

  • Write Linux command for each question. Please use one single commend to do following process. Q1:...

    Write Linux command for each question. Please use one single commend to do following process. Q1: Display all lines of list1.txt and list2.txt containing the letter ‘p', and sort the result. Q2: Append “file1.txt” and “file2.txt”, and redirect to “file3.txt”. Q3: Sort “file3.txt”, and redirect the result to file “file4.txt” Q4: Use shorthand way to change the permission of “file3.txt” to “user - write read execute, group - read execute, other - execute”. Q5: Extract first column from “file3.txt”, and...

  • Using linux Write one-line New Tab ands that determine and print the following desired output: 1....

    Using linux Write one-line New Tab ands that determine and print the following desired output: 1. The total word count in files file1.txt, file2.txt, and file3.txt combined (assume the three files exist in the current directory) 2. How many user accounts are not allowed to log in to the system. You need to calculate the number of users in file /etc/passwd that include /nologin on their line. 3. The name of the 6 largest files in/usr/bin , while suppressing any...

  • linux help Create a file called trees, containing the following individual lines: Oak tree Pine Tree...

    linux help Create a file called trees, containing the following individual lines: Oak tree Pine Tree Spruce tree Cottonwood Maple tree Pine Tree Copy in the contents of the trees to more_trees file. Next, add the following trees at the end of the list. Redwood Willow tree Use a command to compare the trees and more_trees files and show the differences in terms of individual lines that differ. Determine the number of bytes in both the trees and more trees...

  • This is done in Linux terminal. Modify the .bashrc file using command line which would add...

    This is done in Linux terminal. Modify the .bashrc file using command line which would add the following functionality to your shell upon opening a new terminal: 1. Alias called “fc” (short for “folder contents) invoking the command “ls -lah”B. Welcome message “This is demo number 7” C. Variable “semester” being equal to “Winter 2020” and print the value on screen 2. Create any directory of your choice and add it to PATH variable. 3. Show an example of local...

  • Using Kali Linux, the Windows Linux Sub-System, or another Debian based Linux distribution, perform the following...

    Using Kali Linux, the Windows Linux Sub-System, or another Debian based Linux distribution, perform the following tasks based on the Linux Fundamentals lecture. For this lab, take screenshots for all major steps completed to illustrate that each task was successfully completed the same method as would be used for other labs). Tasks: 1. Create a new user named Billy Bob using the command linter face 2. Add Billy Bob to the sudoers group 3. Update and upgrade your Linux distribution...

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