生成动态文件名并对其执行一些操作并写入其他目录

生成动态文件名并对其执行一些操作并写入其他目录

我有一个测试文件夹,文件名如下所示:

100080_Accounting  Statistic-42005_04May2020_0000-04May2020_0100.csv  
110001_ABCD Traffic_04May2020_header_only.csv


cat '110001_ABCD Traffic_04May2020_header_only.csv'
ID,NAME,LOCATION
1,Vikrant,Gurgaon
2,Bharat,Noida
3,Raju,Hyderabad

cat '100080_Accounting Statistic-42005_04May2020_0000-04May2020_0100.csv'
ID,NAME,AGE,DEPARTMENT
4,Raju,22,Admin
5,Rajeev,23,Admin

我正在尝试提取文件名并将该文件名附加到文件的每个记录,同时从测试目录读取该特定文件。在将实际记录写入新创建的文件之前,我已在标题中添加了一个新的列名称“GroupName”,并希望将这些文件写入其他目录。

下面是我的 shell 脚本,其中逻辑按预期工作正常,但我无法找出将其写入具有相同文件名的其他目录的方法。此外,我在将列添加到第二个文件的标题时遇到问题。

#!/bin/bash

# Go to where the files are located
filedir=/home/vikrant_singh_rana/testing/*

#reading file from directory
for filename in $filedir; do
        #extracting modified file name from file
        b=$(basename "$filename" ".csv" | awk -F "_" '{print $2}' | awk -F "-" '{print $1}')
        #reading line by line from file
        first='yes'
        while read -r line;do
                #reading header from file
                header=$(head -n1 "$filename")
                if [[ $first == 'yes' ]]; then
                        #writing header with new column just once
                printf '%s,%s\n' "GroupName" "$header"
                first='no'
                else
                        #writing modified filename to each record of a file
                printf '%s,%s\n' "$b" "$line"
                fi
        done <"$filename"
        #echo $filename
        output_filename=$(basename "$filename")
        #echo $output_filename

#done
done > /home/vikrant_singh_rana/enrichment_files/enrichment_file.txt

我得到的输出如下:

cat /home/vikrant_singh_rana/enrichment_files/enrichment_file.txt

GroupName,ID,NAME,AGE,DEPARTMENT
Accounting Statistic,4,Raju,22,Admin
Accounting Statistic,5,Rajeev,23,Admin
GroupName,ID,NAME,LOCATION
ABCD Traffic,1,Vikrant,Gurgaon
ABCD Traffic,2,Bharat,Noida
ABCD Traffic,3,Raju,Hyderabad

我所期望的如下所示,文件名与我正在读取的文件名相同,输出应写入单独的文件。

file name as :110001_ABCD Traffic_04May2020_header_only.csv

    GroupName,ID,NAME,AGE,DEPARTMENT
    Accounting Statistic,4,Raju,22,Admin
    Accounting Statistic,5,Rajeev,23,Admin

file name as :100080_Accounting  Statistic-42005_04May2020_0000-04May2020_0100.csv 
    GroupName,ID,NAME,LOCATION
    ABCD Traffic,1,Vikrant,Gurgaon
    ABCD Traffic,2,Bharat,Noida
    ABCD Traffic,3,Raju,Hyderabad

答案1

我能够将输出写入文件并进行了如下更改:如果需要任何更改,请纠正我。

#!/bin/bash

# Go to where the files are located
filedir=/home/vikrant_singh_rana/testing/*
#reading file from directory
for filename in $filedir; do
        #extracting modified file name from file
        b=$(basename "$filename" ".csv" | awk -F "_" '{print $2}' | awk -F "-" '{print $1}')
        #reading line by line from file
        first='yes'
        output_filename=$(basename "$filename")
        while read -r line;do
                #reading header from file
                header=$(head -n1 "$filename")
                if [[ $first == 'yes' ]]; then
                        #writing header with new column just once
                printf '%s,%s\n' "CounterGroupName" "$header" > /home/vikrant_singh_rana/enrichment_files/"$output_filename"
                first='no'
                else
                        #writing modified filename to each record of a file
                printf '%s,%s\n' "$b" "$line" >> /home/vikrant_singh_rana/enrichment_files/"$output_filename"
                fi
        done <"$filename"
done

相关内容