To get the file extension of the files present in each row using shell script

Raam

How to get the file extension of the below shown data. Apparently, I have millions of rows in the csv file.

col1                             ,col2     ,col3                        ,col4     , col5, col6, col7
aaaaa/                           ,0        ,2018-03-16T09:31:42.000Z,   xx-daily.......
aaaaa/201802/                    ,0        ,2019-01-17T06:16:34.000Z,   xx-daily
aaaaa/201802/Feb2018000000_0.gzip,32602738,2018-09-11T04:05:38.000Z,    xx-daily
aaaaa/201802/Feb2018000001_0.gzip,32602738,2018-09-11T04:05:38.000Z,    xx-daily
aaaaa/201802/Feb2018000002_0.gzip,32602738,2018-09-11T04:05:38.000Z,    xx-daily
aaaaa/201802/Feb2018000003_0.gzip,32602187,2018-09-11T04:05:38.000Z,    xx-daily
aaaaa/201802/Feb2018000004_0.gzip,32602187,2018-09-11T04:05:39.000Z,    xx-daily
aaaaa/201802/Feb2018000005_0.gzip,32602187,2018-09-11T04:05:39.000Z,    xx-daily
aaaaa/201802/Feb2018000006_0.gzip,32578449,2018-09-11T04:05:39.000Z,    xx-daily

I need to split the file extension and create another column to populate the file extension value in the same csv file.

Need the output as below

col1                             ,col2     ,col3                        ,col4     , col5, col6, col7
aaaaa/                                      ,0         ,2018-03-16T09:31:42.000Z,   xx-daily.......
aaaaa/201802/                               ,0         ,2019-01-17T06:16:34.000Z,   xx-daily
aaaaa/201802/Feb2018000000_0.gzip, gzip     ,32602738,2018-09-11T04:05:38.000Z, xx-daily
aaaaa/201802/Feb2018000001_0.gzip, gzip     ,32602738,2018-09-11T04:05:38.000Z, xx-daily
aaaaa/201802/Feb2018000002_0.gzip, gzip     ,32602738,2018-09-11T04:05:38.000Z, xx-daily
William Pursell

This is a bit clunky, does not add the spaces that you seem to want, and introduces a blank column in those rows that do not have a file extension (I believe that is correct behavior, and it's easy enough to modify this to stop doing that if you like). However, under no circumstances would I condone writing back into the same file from which you are reading. Some implementations of awk provide a feature for doing so, but using it is misguided. Use a filter and write your output to a different file. If you need to, you can overwrite the original file.

awk '{c=split($1,a,"."); ext=c>1?a[c]:""; $2=ext OFS $2}1' FS=, OFS=, input-file

You can get better spacing with:

awk '{c=split($1,a,"."); ext=c>1?a[c]:""; $2=ext OFS $2}1' FS=, OFS=',\t' input

and you can avoid the empty column (but you really don't want to do this) with:

awk '{c=split($1,a,"."); if( c > 1) $2=a[c] OFS $2}1' FS=, OFS=',\t' input

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Get specific line from text file using just shell script

Download files using Shell script

How to get extension of a file in shell script

Find files of specific file types using shell script

replace a line in files present in different folders shell script

How to get file names and file size from tar files using shell script

How to get file name of files by using each loop in jquery

Get the last two letters of each line in a file using script shell

How to creat a symbolic link to all files with a specific extension from a specific directory using a shell script?

Inserting text at begining of each line of text file using shell script

using find in Bash Script and checking if file is present

How to get the URL from a file using a shell script

Shell script for append string to first of each row in column 6 of csv file

Shell Script: Using files based on part of file name

Using sed on files with certain extension inside shell script

Find files using shell script

check if file with certain file extension present in folder using batch script

How to test if a file starting with a particular prefix is present in a directory in shell script

Get File Size of files on Windows through Shell Script on Solaris/Unix

Script to rename a large number of files using the text in each file to rename

Rename all files in directory to line present in each file using python

In a bash shell script, how to rename multiple files, keeping only the numbers and the extension of the original file names?

I want to loop though each sub-directories & check if *.v or *.sv file is present or not , if it is present then do some operation using csh script?

Shell script to unzip files using file watcher

Finding a file extension in a string using shell script

Modify and add extension to filenames with shell script in a file

Get top 10 values from log file using shell script

Shell script loop with File extension is not working as expected

How to process file content differently for each line using shell script?