Sponsored Content
Top Forums UNIX for Beginners Questions & Answers Difficulty to create .txt file from loop in bash Post 303044854 by Tim2424 on Thursday 5th of March 2020 10:13:46 AM
Old 03-05-2020
Difficulty to create .txt file from loop in bash

I've this data :

Code:
data1.txt

2020-01-27-06-00;/dev/hd1;100;/
2020-01-27-12-00;/dev/hd1;100;/
2020-01-27-18-00;/dev/hd1;100;/
2020-01-27-06-00;/dev/hd2;200;/usr
2020-01-27-12-00;/dev/hd2;200;/usr
2020-01-27-18-00;/dev/hd2;200;/usr

 data2.txt 

2020-02-27-06-00;/dev/hd1;120;/
2020-02-27-12-00;/dev/hd1;120;/
2020-02-27-18-00;/dev/hd1;120;/
2020-02-27-06-00;/dev/hd2;230;/usr
2020-02-27-12-00;/dev/hd2;230;/usr
2020-02-27-18-00;/dev/hd2;230;/usr

 data3.txt 

2020-03-27-06-00;/dev/hd1;130;/
2020-03-27-12-00;/dev/hd1;130;/
2020-03-27-18-00;/dev/hd1;130;/
2020-03-27-06-00;/dev/hd2;240;/usr
2020-03-27-12-00;/dev/hd2;240;/usr
 2020-03-27-18-00;/dev/hd2;240;/usr

I would like to create a .txt file for each filesystem ( so hd1.txt, hd2.txt, hd3.txt and hd4.txt ) and put in each .txt file the sum of the value from each FS from each dataX.txt. I've some difficulties to explain in english what I want, so here an example of the result wanted

Expected content for the output file `hd1.txt`:
Code:
2020-01;/dev/hd1;300;/
2020-02;/dev/hd1;360;/
2020-03;/dev/hd1;390:/

Expected content for the file `hd2.txt`:


Code:
2020-01;/dev/hd2;600;/usr
2020-02;/dev/hd2;690;/usr
2020-03;/dev/hd2;720;/usr

The implementation I've currently tried:

Code:
for i in $(cat *.txt | awk -F';' '{print $2}' | cut -d '/' -f3| uniq)
do
    cat *.txt | grep -w $i | awk -F';' -v date="$(cat *.txt | awk -F';' '{print $1}' | cut -d'-' -f-2 | uniq )" '{sum+=$3} END {print date";"$2";"sum}' >> $i

done

But it doesn't works...

Can you show me how to do that ?
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

create file .txt

hi.i want to create a bash script called countfiles.sh, that will count the pack of files under the current file that their term satisfy a specific pattern. The script must show the total per file or on the whole. thank tou (4 Replies)
Discussion started by: battaglia
4 Replies

2. Shell Programming and Scripting

how to create file.txt and add current date in file content

Hey guy, how to make bash script to create foo.txt file and add current date into file content and that file always append. example: today the script run and add today date into content foo.txt and tomorrow the script will run and add tomorrow date in content foo.txt without remove today... (3 Replies)
Discussion started by: chenboly
3 Replies

3. Shell Programming and Scripting

Loop difficulty

hi all I am new to unix and want to create a loop to repeat the loop and before that it ask me to do so.I know "while" may help but I put it in my work and getting stuk with it.any help appreciated. (13 Replies)
Discussion started by: samsami1971
13 Replies

4. Shell Programming and Scripting

bash script to create txt files based on timestamp

Hi , please guide me for a bash script that will create a txt files and the name of the txt files will be as of timestamp so that each file name will be different from other and these files will be get created say after every 10 minutes in a folder(/home/p2000/sxs137), please guide me how would... (1 Reply)
Discussion started by: nks342
1 Replies

5. Shell Programming and Scripting

create txt file form data file and add some line on it

Hi Guys, I have file A.txt File A Data AK1521 AK2536 AK3164 I want create text file of all data above and write some data on each file. want Output on below folder /home/kka/out AK1521.txt Hi Welocme (3 Replies)
Discussion started by: asavaliya
3 Replies

6. Shell Programming and Scripting

How to create .txt file by using function?

My Calling script is like below: for file in `echo $LIST_OF_FILES` --listing filenames eg, xyz_meta_20110501_00000789.tar do file_name=`basename $file` <call a function to create .txt file in below format> done Want to generate a .txt file that contains below data in ksh: ... (3 Replies)
Discussion started by: vedanta
3 Replies

7. UNIX for Dummies Questions & Answers

How to create a .csv file from 2 different .txt files?

Hi, I need to create a .csv file from information that i have in two different tab delimited .txt file. I just want to select some of the columns of each .txt file and paste them into a .cvs file. My files look like: File 1 transcript_id Seq. Description Seq. Length ... (2 Replies)
Discussion started by: alisrpp
2 Replies

8. Shell Programming and Scripting

Bash/awk and for loop to create a template

Source File: google.cz http://czechrepublic.google.com/ http://czechrepublic.google.cz http://czechrepublic.google.com/ http://brno.google.cz http://brno.google.com/ Fail Code root@arisvm ~/g] $ cat trya rm -f ss for i in a.txt do #b=`cat $i|awk '{print $1}'` #c=`cat $i|awk '{print... (4 Replies)
Discussion started by: invinzin21
4 Replies

9. Shell Programming and Scripting

awk to create variables to pass into a bash loop to create a download link

I have created one file that contains all the necessary info in it to create a download link. In each of the lines /results/analysis/output/Home/Auto_user_S5-00580-6-Medexome_67_032/plugin_out/FileExporter_out.67... (8 Replies)
Discussion started by: cmccabe
8 Replies

10. UNIX for Beginners Questions & Answers

Copy the content from txt file and create a html file

I have a txt file with a list of error messages in a xml tag format, and each error message is separated with a identifier(endresult).Need to split that and copy and create a new html file.Error message has some special character. how to escape the special character and insert my data into the... (7 Replies)
Discussion started by: DevAakash
7 Replies
DEV(4)							     Kernel Interfaces Manual							    DEV(4)

NAME
dev - device files in /dev DESCRIPTION
Device files are the eyes and ears of the system. Through the device files one has access to the disks, terminals and other parts of the machine. Single bytes or disk blocks may be transferred to or from a device with ordinary read(2) or write(2) calls, byte positions set with lseek(2), or more complicated control functions performed with ioctl(2). Device files as found in /dev have several attributes that must be considered. Here are two examples as ls -l shows them: brw-rw-rw- 1 root operator 2, 1 Jun 10 1995 fd1 crw--w---- 1 kjb tty 4, 0 May 11 09:41 console Most attributes are the same as for a regular file and have the same function. The file type and the major and minor device numbers are special to devices. Character devices are marked with a c as a file type letter. Any I/O on a character device is sent down to the device driver without any interpretation. This means that a process doing the I/O must know the characteristics of the device and deal with them appropriately. Block devices provoke the file system server into buffering the data on those devices. Data read or written by processes is passed through the file system block cache. Unaligned bytes read or written are extracted or reassembled by the file server from or to whole blocks in the cache. The file server transfers data to or from the device driver as blocks to positions at block size boundaries. These blocks are Minix blocks of 1024 bytes, disk devices usually have a 512 byte block size. Only block devices can be mounted as part of the file system tree if they contain a Minix file system. The major device number (2 for fd1 and 4 for console) are used by FS to find the device driver that manages a device. The minor device number (1 for fd1 and 0 for console) is passed to the driver to select a device among a number of related devices that are all managed by that driver. The device drivers are usually kernel tasks under Minix, small processes that are contained within the address space of the kernel. The following tasks and associated devices exist: Memory (major 1) The ram, mem, kmem, and null devices are managed by the memory task. The ram device is a block device for a chunk of memory that is the RAM disk. Any byte read from or written to the ram device is copied from or to that memory chunk. The mem device is a character device for the entire address space of the system, but kmem only for the kernel data area. These two devices allow programs like ps(1) to hunt around the system looking for interesting bits. The null device is a data sink. It happily swallows any bytes written to it, and returns nothing on a read. Floppy disk (major 2) The fd0, fd0a, fd0b, fd0c, and fd0d block devices are the first floppy disk and the four partitions that may exist on a that floppy disk. Likewise are fd1 and fd1[a-d] the device and partitions for the second floppy disk. The floppy disk devices are described in detail in fd(4). Partitioning in general is explained in hd(4). Hard disk (major 3) The first hard disk can be accessed by block device hd0. This device addresses the entire hard disk from the first to the last sector. A hard disk is normally partitioned in up to four primary partitions, hd1, hd2, hd3, and hd4. Each of these devices accesses a range of sec- tors on the hd0 device. It is customary to give each operating system on a disk a primary partition. So the MS-DOS C: "drive" can be on hd1, and Minix can be on hd2. Minix wants to have several partitions on its own, so hd2 can be further subdivided into the subpartitions hd2a, hd2b, hd2c, and hd2d. /dev contains devices for the first and second hard disk (hd0 and hd5) their primary partitions (hd[1-46-9]) and subpartitions thereof (hd[1-46-9][a-d]). More detail can be found in hd(4). Terminals (minor 4) The TTY driver manages the system console device, aptly named console, the serial lines, tty00 and tty01, and the pseudo ttys. Through the console device one can display characters on a screen attached to a monochrome, Hercules, color, or VGA adapter. The ttyc1, ttyc2, etc. devices are the so-called "virtual consoles" that share the one console display. One can select which virtual console is to be visible on the screen and take input from the keyboard. To allow remote login the devices with minor numbers of 128 or higher offer virtual termi- nals. These pseudo ttys come in tty, pty pairs that form a pipe between processes running under the tty, and a controlling process attached to the pty side. See also console(4), and tty(4). Anonymous TTY (major 5) This is just one device named tty that is a synonym for the controlling tty of a process. This device is not managed by any device driver, but is handled by FS itself. A process can get access to the terminal it is running under by using /dev/tty. Line printer (major 6) The lp device sends any bytes written to it to the printer. TCP/IP (major 7) The TCP/IP task is not a kernel task, but a server like MM and FS. It sits between FS and the DP8390 task that manages the ethernet boards. Together they implement the TCP/IP protocol. See also ip(4). CD-ROM (major 8) This is the CD-ROM driver for the Mitsumi proprietary CD-ROM interface. The cd0 device addresses the whole CD, with extra cd[1-4] and cd[1-4][a-d] devices for if the CD also contains partitions with Minix file systems. SCSI disks and tapes (major 10) The sd* devices are disks in the same way as the hd* devices. Except that these disks are SCSI disks attached to an Adaptec 1540 con- troller or compatible. The driver also manages the rst* and nrst* tape devices (rewinding or non-rewinding). See sd(4). Audio (major 13) The audio device can be used to produce or record air vibrations using a Soundblaster 16 type audio card. See audio(4). Mixer (major 14) The mixer device is used to control the audio driver. FILES
/dev/* All Minix devices SEE ALSO
read(2), write(2), lseek(2), ioctl(2), console(4), fd(4), hd(4), ip(4), sd(4), tty(4), MAKEDEV(8). DIAGNOSTICS
There are five prominent errors that processes accessing device files may provoke: ENODEV - No such device There is no driver managing the device class this device belongs to. Either the driver is configured out, or it is not loaded (inet). ENXIO - No such device or address This device is not available. Either the driver does not support it at all, or the hardware isn't available, i.e. accessing the sec- ond disk on a system with only one disk. EACCES - Permission denied This error may cause a lot of head scratching if ls -l shows a device file to be writable. The media you are trying to access is sim- ply physically write protected! EINVAL - Invalid argument Devices may not like reads or writes that are not block multiples, or very big transfers, etc. The device manual page should list the limits. EIO - I/O error This may be a real I/O error, i.e. a read or write on the device failing due to a media error. But it may also be the result of an operation that a device can't do, or an empty tape drive, etc. NOTES
Some devices are not present by default. The MAKEDEV script knows how to make them. MS-DOS equivalents The names of MS-DOS devices map to Minix devices as follows: A: fd0 B: fd1 C: hd1, sd1 (usually the first partition) D: hd6, sd1, sd6 (or an extended partition) CON console COM1 tty00 (UNIX counts from 0) LPT1 lp AUTHOR
Kees J. Bot (kjb@cs.vu.nl) DEV(4)
All times are GMT -4. The time now is 10:18 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy