Question

I am a beginner in shell. Please help me with this question. question: In this assignment,...

I am a beginner in shell. Please help me with this question.

question:

In this assignment, you will research into a few standard Unix utilities and leverage them in a shell script to perform a simple but useful task that's right up the alley of Unix shell scripting.

As usual, you should aim for reasonably efficient algorithms and reasonably organized, comprehensible code.

Consolidating log files

My Internet chat software recording my conversations into daily log files, each named by date, e.g., “2020-06-14.log”. Naturally, as months go by, this becomes unwieldily too many files.

If I can merge daily log files into monthly log files, that will be great. But of course, I want to preserve the dates in another way. Help me by writing a shell script for this, call it “stampmerge.sh”. Here is the precise specification:

For each filename of the form “yyyy-mm-dd.log” in the current directory, in alphabetical order (if you use a shell pattern, it's already ordered for you):

Append the content to “yyyy-mm.log”, but with this addition: Each line is prefixed with “yyyy-mm-dd” followed by a space. Example: If 2020-06-14.log has these lines:

Hello everyone in CSCB09
Bye!

then append these lines to 2020-06.log:

2020-06-14 Hello everyone in CSCB09
2020-06-14 Bye!

Moreover! Putting a space between the date and the original line is only the default. If I provide a command line argument, use that instead; you may assume that it's just one character. Example: if I run “sh stampmerge.sh :”, then the lines to be appended are:

2020-06-14:Hello everyone in CSCB09
2020-06-14:Bye!

Sample input (including a pre-existing monthly file that you must append to, not overwrite) and sample expected output 2020-06.log.expected (using the default space) are provided.

I will test your script with sh; bash extra features will not be supported. I may test with no permission to create new files—you don't need it.

Useful utilities

cut can help extract “2020-06” and “2020-06-14” from “2020-06-14.log”.

yes, head, paste can help with prefixing a string to every line. But you also need to know how many lines are in the daily file, so use wc.

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

Hi,

Please find the code below.

echo $PWD
FILES=/home/*.log
for f in $FILES
do
echo "Processing $f file..."
# take action on each file. $f store current file name
cat $f
  
abc=$(echo $f | cut -d "." -f1 |rev|cut -d"-" -f2-|rev|cut -d'/' -f3-)
echo # from this i have got just year and month part of the log file name.
echo $abc
  
# this wil attach each line of the file with year and month.
if [ $# -eq 0 ]
then
echo "No arguments supplied"
while IFS= read -r line; do
echo "${abc} $line"
done < "$f" > $abc.log
else
echo "arguments supplied"
while IFS= read -r line; do
echo "${abc}${1}$line"
done < "$f" > $abc.log
fi

done
  
  

code snapshot:

4 11 13 main.bash 2020-06-19.log 2020-06.log! 1 echo $PWD 2 FILES=/home/*.log 3 for f in $FILES do 5 echo Processing $f file

output :

without command line argument:

main.bash 2020-06-19.log 2020-06.log 2020-06 Hello everyone in CSCBO9 2 2020-06 Bye! 3 1

output attached.with command line argument: ":"

main.bash 2020-06-19.log 2020-06.log! 1 2020-06:Hello everyone in CSCB09 2 2020-06:Bye! 3

Thanks,

kindly upvote

Add a comment
Know the answer?
Add Answer to:
I am a beginner in shell. Please help me with this question. question: In this assignment,...
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
  • UNIX help!!!! I need what the text would look like in the vi editor. also note...

    UNIX help!!!! I need what the text would look like in the vi editor. also note this question has been posted before and the answers were all wrong!! Write a shell script (bash or sh) that prints out the following lines/information: Only your name may be hard coded, the rest must be obtained programmatically within the shell script. Name: "Your name" Hostname: "The computer's hostname" Host IP: "The computer's IP number" Default Gateway: "The computer's default gateway" OS Version: "Whatever...

  • Revision Question 3 on Linux. Please explain the shell script commands in the context of post...

    Revision Question 3 on Linux. Please explain the shell script commands in the context of post execution. You have just logged in and have a directory called "novel" in your home directory containing the following files: chapter1.docx chapter2.docx chapter3.docx draft.pdf save where "save" is itself a directory and contains the files Attributes.txt draft.pdf.1 draft.pdf.2 draft.pdf.3 indexA.txt indexB.txt list1.txt list2.txt a) Describe the results you would expect when executing the following shell commands: i) ls novel/save/*A* ii) ls novel/*r[23]* iii) rmdir...

  • Unix questions, i need help on Hint: Commands to study to answer this question: predefined shell...

    Unix questions, i need help on Hint: Commands to study to answer this question: predefined shell variables, and .profile script file, echo SHELL, HOME, PATH, MAIL and TERM are predefined shell variables. You can use the value of a shell variable in a shell command putting $ in front of it. For example, to display the value of the HOME directory of the user, specify $HOME in the echo command like echo $HOME. Do not give just the value of...

  • Hello all, I have a c++/unix (bash) question. I am struggling on starting this assignment. If...

    Hello all, I have a c++/unix (bash) question. I am struggling on starting this assignment. If you could start the assignment and tell me how to do the rest it would be greatly appreciated! (Quick thumbs up answer response if thorough and correct) Maintain automobile records in a database Write a shell script to create, view and modify a simple database that contains automobile records. The shell script has to be done in Bourne shell syntax (bash as a matter...

  • I am in need of some help with UNIX/Linux, particularly sed and awk: Please help me...

    I am in need of some help with UNIX/Linux, particularly sed and awk: Please help me Display from /etc/passwd a list of users and their shells for those using the Korn shell or Bash shell. Order the output by the absolute pathname of the shell used. Thank you.

  • Can you help me? I am taking an INTRODUCTORY unix/linux shell programming course and need to...

    Can you help me? I am taking an INTRODUCTORY unix/linux shell programming course and need to know what the following command will do. Need a SIMPLE answer please. onsystem john1 &

  • Hello, I am attempting to write a basic shell script using bash. It will need to...

    Hello, I am attempting to write a basic shell script using bash. It will need to print the user, host, time, and date. It will need to take user input for a txt file to be searched and create a new file to save the results. It must print the number of lines and characters in the text. The user will need to enter three words to be searched, and the number of occurrences of those words in the text...

  • USING Unix/Linux shell !!!: 1) Write a script (project2.sh) that will take information from a file,...

    USING Unix/Linux shell !!!: 1) Write a script (project2.sh) that will take information from a file, and print (user’s choice of printers) identical form letters to each recipient. Either E197, E-199, E-194 printers or to project2.output. Prompt the user for their choice. 2) The script and output file are due: per Moodle timeline. Please upload your fully functioning script (project2.sh) your data file (project2.input) and your output file (project2.output) into Moodle. 3) Requirements of the script. a. You can use...

  • Hello, I need help creating a bash script for the following question, please also show screen...

    Hello, I need help creating a bash script for the following question, please also show screen shot of running the program, thank you for your help in advance Create a script to create files such as jpeg, pdf, png, gif, etc.

  • 1. Write 4 simple shell scripts. Each question will ask you to write one or more...

    1. Write 4 simple shell scripts. Each question will ask you to write one or more command lines to solve a problem. Put the command(s) from each problem into a file named problem1.sh, problem2.sh Note that if you want your script to be interpreted by bash, each file should start with the line: #!/bin/bash and will have one (or more, as needed) commands following it. To execute each script in bash, so you can determine if it is working or...

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