Skip to content
Advertisement

Deleting the first row and replacing the first column data with the given data

I have the directories name like folder1,folder2,folder3….folder10. Inside each directory there are many text files with different names.And each text files contain a text row on the top and two columns of data that looks like as below

  data_example1          textfile_1000_manygthe>written                        
                          20.0         0.53
                          30.0         2.56
                          40.0         2.26
                          45.59        1.28
                          50.24        1.95
                          
 data_example2          textfile_1002_man_gth<>writ
                         10.0          1.28    
                         45.0          2.58
                         48.0          1.58
                         12.0          2.69
                         69.0          1.59
                

Then what i want to do is== at first i want to remove completely the first row of all text files present inside the directories folder1,folder2,folder3….folder10.After that whatever the value in the first column of each text file present inside the directories, i want to replace that first column values with other values that is saved in separate one single text file named as “replace_first_column.txt” and the content of replace_first_column.txt file looks like as below and finally want to save all files in the original name inside same directories.

                   10.0
                   20.0
                   30.0
                   40.0
                   50.0
                   

I tried the code below: but it doesnot work.Hope i will get some solutions.Thanks.

   #!/bin/sh
   for file in folder1,folder2....folder10
   do
   sed -1 $files
   done
    for file in folder1,folder2....folder10
    do
    replace    ##i dont know here replace first column

Advertisement

Answer

Something like this should do it using bash (for {1..10}), GNU find (for + at the end) and GNU awk (for -i inplace):

#!/usr/bin/env bash
find folder{1..10} -type f -exec gawk -i inplace '
    { print FILENAME, $0 | "cat>&2" }     # for tracing, see comments
    NR==FNR { a[NR+1]=$1; print; next }
    FNR>1 { $1=a[FNR]; print }
' /wherever/replace_first_column.txt {} +
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement