Skip to main content
Spelling and clarity
Source Link
terdon
  • 252.9k
  • 69
  • 481
  • 720

Please help to writeI want a Perl one liner to add-liner that checks whether the file name in startingfirst fields of thean input file is the file's name and, if it isn't, adds the file name doesn't exist as the first column on every line.

Example written in shell :

for f in *file*.csv; do  file_column=`cat ${f} | awk -F',' '{print$1}'`  if [ $file_column != ${f} ]  then  sed -i "s/^/$f,/" $f 2>/dev/null;  fi done 

inBut the approach above example trying to take the first column and checking if it's same as file name , if it's same doing nothing else addingwhich checks whether the file name asis present in the first column.

But the above approach and adds it if it isn't, is taking ~3 Hours for 4 Laks files, As per my understanding perl. I understand that Perl is faster infor file operationoperations.

Perl which i haveThe Perl command I tried  :

perl -p -i -e 's/^/Welcome to Hell,/' file*.csv 

pleasePlease help me to add the logiclogic to check if it already exist or not in file before doingwhether the field exists already and only change if it doesn't.

Input : file1.csv col1,col2,col3 data1,data2,dat3 Output: file1.csv file1.csv,col1,col2,col3 file1.csv,data1,data2,data3 

or if here is any faster way please suggest. Perl one liner because it's part of another shell script so tiny call will be better i guess (suggest please)

Please help to write a Perl one liner to add the file name in starting of the file if the file name doesn't exist as first column.

Example written in shell :

for f in *file*.csv; do file_column=`cat ${f} | awk -F',' '{print$1}'` if [ $file_column != ${f} ] then sed -i "s/^/$f,/" $f 2>/dev/null; fi done 

in the above example trying to take the first column and checking if it's same as file name , if it's same doing nothing else adding the file name as first column.

But the above approach taking ~3 Hours for 4 Laks files, As per my understanding perl is faster in file operation.

Perl which i have tried  :

perl -p -i -e 's/^/Welcome to Hell,/' file*.csv 

please help me to add the logic to check if it already exist or not in file before doing the change.

Input : file1.csv col1,col2,col3 data1,data2,dat3 Output: file1.csv file1.csv,col1,col2,col3 file1.csv,data1,data2,data3 

or if here is any faster way please suggest. Perl one liner because it's part of another shell script so tiny call will be better i guess (suggest please)

I want a Perl one-liner that checks whether the first fields of an input file is the file's name and, if it isn't, adds the file name as the first column on every line.

Example written in shell :

for f in *file*.csv; do  file_column=`cat ${f} | awk -F',' '{print$1}'`  if [ $file_column != ${f} ]  then  sed -i "s/^/$f,/" $f 2>/dev/null;  fi done 

But the approach above, which checks whether the file name is present in the first column and adds it if it isn't, is taking ~3 Hours for 4 Laks files. I understand that Perl is faster for file operations.

The Perl command I tried:

perl -p -i -e 's/^/Welcome to Hell,/' file*.csv 

Please help me add the logic to check whether the field exists already and only change if it doesn't.

Input : file1.csv col1,col2,col3 data1,data2,dat3 Output: file1.csv file1.csv,col1,col2,col3 file1.csv,data1,data2,data3 

or if here is any faster way please suggest. Perl one liner because it's part of another shell script so tiny call will be better i guess (suggest please)

given input output details
Source Link
William R
  • 609
  • 3
  • 8
  • 16

Please help to write a Perl one liner to add the file name in starting of the file if the file name doesn't exist as first column.

Example written in shell :

for f in *file*.csv; do file_column=`cat ${f} | awk -F',' '{print$1}'` if [ $file_column != ${f} ] then sed -i "s/^/$f,/" $f 2>/dev/null; fi done 

in the above example trying to take the first column and checking if it's same as file name , if it's same doing nothing else adding the file name as first column.

But the above approach taking ~3 Hours for 4 Laks files, As per my understanding perl is faster in file operation.

Perl which i have tried :

perl -p -i -e 's/^/Welcome to Hell,/' file*.csv 

please help me to add the logic to check if it already exist or not in file before doing the change.

Input : file1.csv col1,col2,col3 data1,data2,dat3 Output: file1.csv file1.csv,col1,col2,col3 file1.csv,data1,data2,data3 

or if here is any faster way please suggest. Perl one liner because it's part of another shell script so tiny call will be better i guess (suggest please)

Please help to write a Perl one liner to add the file name in starting of the file if the file name doesn't exist as first column.

Example written in shell :

for f in *file*.csv; do file_column=`cat ${f} | awk -F',' '{print$1}'` if [ $file_column != ${f} ] then sed -i "s/^/$f,/" $f 2>/dev/null; fi done 

in the above example trying to take the first column and checking if it's same as file name , if it's same doing nothing else adding the file name as first column.

But the above approach taking ~3 Hours for 4 Laks files, As per my understanding perl is faster in file operation.

Perl which i have tried :

perl -p -i -e 's/^/Welcome to Hell,/' file*.csv 

please help me to add the logic to check if it already exist or not in file before doing the change.

or if here is any faster way please suggest.

Please help to write a Perl one liner to add the file name in starting of the file if the file name doesn't exist as first column.

Example written in shell :

for f in *file*.csv; do file_column=`cat ${f} | awk -F',' '{print$1}'` if [ $file_column != ${f} ] then sed -i "s/^/$f,/" $f 2>/dev/null; fi done 

in the above example trying to take the first column and checking if it's same as file name , if it's same doing nothing else adding the file name as first column.

But the above approach taking ~3 Hours for 4 Laks files, As per my understanding perl is faster in file operation.

Perl which i have tried :

perl -p -i -e 's/^/Welcome to Hell,/' file*.csv 

please help me to add the logic to check if it already exist or not in file before doing the change.

Input : file1.csv col1,col2,col3 data1,data2,dat3 Output: file1.csv file1.csv,col1,col2,col3 file1.csv,data1,data2,data3 

or if here is any faster way please suggest. Perl one liner because it's part of another shell script so tiny call will be better i guess (suggest please)

Source Link
William R
  • 609
  • 3
  • 8
  • 16

How to add column in the beginning of file using perl?

Please help to write a Perl one liner to add the file name in starting of the file if the file name doesn't exist as first column.

Example written in shell :

for f in *file*.csv; do file_column=`cat ${f} | awk -F',' '{print$1}'` if [ $file_column != ${f} ] then sed -i "s/^/$f,/" $f 2>/dev/null; fi done 

in the above example trying to take the first column and checking if it's same as file name , if it's same doing nothing else adding the file name as first column.

But the above approach taking ~3 Hours for 4 Laks files, As per my understanding perl is faster in file operation.

Perl which i have tried :

perl -p -i -e 's/^/Welcome to Hell,/' file*.csv 

please help me to add the logic to check if it already exist or not in file before doing the change.

or if here is any faster way please suggest.