Question

In: Computer Science

We are interested in the implementation of a Unix/Linux system utility for the concatenation of a list of n text files.

Write in C

We are interested in the implementation of a Unix/Linux system utility for the concatenation of a list of n text files. In order to do that we are going to consider the following syntax below where the concatenation of n text files are written in the output file all.txt or on the console if the output text file is not specified.

      $./mycat file_1.txt file_2.txt   . . .   file_n.txt > all.txt

Solutions

Expert Solution

C code for above problem

#include
#include

int main(int argc,char **argv)
{
   int found=0;
   for(int i=1;i<argc;i++)
   {
       if(strcmp(argv[i],">")==0 && i==argc-2)
       {
           found=1;
           break;
       }
   }
  
   FILE *fout=NULL;
   if(found)
   {
       fout=fopen(argv[argc-1],"w");
       argc-=2;
   }
   for(int i=1;i<argc;i++)
   {
       FILE *fp=fopen(argv[i],"r");
       if(fp)
       {
           char line[100];
           while(fgets(line,sizeof(line),fp))
           {
               if(found)
               {
                   fprintf(fout,"%s",line);
               }
               else
               {
                   printf("%s",line);
               }
           }
       }
       fclose(fp);
   }
  
   if(found)
   {
       fclose(fout);
   }
   return 0;
}

Sample output

if "file_1.txt" has following data

My data of file-1

"file_2.txt" has following data

My data of file-2

(1):

If our command while running is "./a.out file_1.txt file_2.txt", then output looks as follows: (It means printing data of all files on console)

(2):

If our command while running is "./a.out file_1.txt file_2.txt > all.txt", then "all.txt" looks as follows: (It means writing all files into one file)

My data of file-1
My data of file-2

 


Related Solutions

Pertaining to the Linux operating system, which is a variant of Unix, DISCUSS Topics that will...
Pertaining to the Linux operating system, which is a variant of Unix, DISCUSS Topics that will include things such as history, Linux kernel, and design principles.
Linux Directories, File Properties, and the File System in C Understanding Unix/Linux Programming Your version of...
Linux Directories, File Properties, and the File System in C Understanding Unix/Linux Programming Your version of mv command The mv command is more than just a wrapper around the rename system call. Write a version of mv that accepts two argument. The first argument must be the name of a file, and the second argument may be the name of a file or the name of a directory. If the destination is the name of a directory, then mv moves...
Unix/Linux Turn in the following commands and any output from the commands. 1) ll to show the original 3 files
Unix/LinuxTurn in the following commands and any output from the commands.1) ll to show the original 3 files2) run the tar command to stuff three files3) ll to show the 'tar archive'4) mkdir newdir to create a new directory to unstuff the 'tar archive'
System Administration Linux/Unix This lab is for you to research and implement old school DNS for...
System Administration Linux/Unix This lab is for you to research and implement old school DNS for your virtualized environment Currently, if you tried to ping your server01 by IP address (192.168.10.11), you would receive a response. If you tried to ping using its hostname "server01", it would result in "ping: hostname: Temporary failure in name resolution" 1) Research how and where to implement 2) Create the following entries: 192.168.10.1 router 192.168.10.11 SRV1 192.168.10.12 SRV2 192.168.10.13 client 3) Ensure these hostnames...
Unix ramdom question. How do you display a list of files in your current directory, sorted...
Unix ramdom question. How do you display a list of files in your current directory, sorted by file size with the largest files at the top.
1. Discuss the various log files found on a Linux system. Based on their own experiences,...
1. Discuss the various log files found on a Linux system. Based on their own experiences, how often do they feel they would check these files, and how vigilant do they think they would be about the process? Have them outline some of the reasons why an administrator might tend to ignore these files. 2. Discuss there experiences with the creation of user and group accounts in other OSs such as Windows 10 or Windows Server 2019. What do they...
a python function that reads two text files and merges in to one Linked List, be...
a python function that reads two text files and merges in to one Linked List, be able to print each Item in the new single Linked List class Node(object): item = -1 next = None def __init__(self, item, next): self.item = item self.next = next ================================ textfile! 979 2744 5409 1364 4948 4994 5089 703 1994 4637 2228 4004 1088 2812 170 5179 2614 238 4523 4849 3592 3258 1951 3440 3977 1247 4076 1824 4759 4855 5430 347 974...
3.13 LAB: Extracting Passwords (files and lists) The Linux operating system is a very popular server...
3.13 LAB: Extracting Passwords (files and lists) The Linux operating system is a very popular server OS. A network administrator has to protect the login/password files stored on the servers. In Linux there are two important files: /etc/passwd And it contains rows that look like this: root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin user1:x:15:51:User One:/home/user1:nologin user2:x:15:51:User One:/home/user1:nologin user3:x:15:51:User One:/home/user1:nologin This file contains login information. It's a list of the server's accounts that has userID, groupID, home directory, shell and more info....
assume a program in unix that have a multiple system calls how to show the list...
assume a program in unix that have a multiple system calls how to show the list of system calls used in the program?
Using Python:     "We have a list of strings: \n",     "```python\n",     "CharList = ['a',...
Using Python:     "We have a list of strings: \n",     "```python\n",     "CharList = ['a', 'b', 'c', 'd']\n",     "```\n",     "Now, we write a program to delete some elements from the list. <br> \n",     "The indexes of the elements to be deleted are stored in IndexList <br>\n",     "\n",     "```Scenario-1```: IndexList = [3, 0]\n",     "```python\n",     "CharList = ['a', 'b', 'c', 'd']\n",     "IndexList = [3, 0]\n",     "for n in range(0, len(IndexList)):    \n",    ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT