append text to column in all files of directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting append text to column in all files of directory
# 1  
Old 06-14-2012
append text to column in all files of directory

Hi,

I want to append "chr" to all col 2 values of all files in a particular folder.
This is what I came up with but isnt working. Please help.

Code:
ls -1 * |
(
while read line
do
awk 'BEGIN {FS=OFS=":"} {$2="chr"$2;print $0}' $line > $line_new
done
)

Another question is, how to delete records from a file which have the same value in columns 3 through 39.

Thanks Much
# 2  
Old 06-14-2012
It is quite difficult to work from faulty code (syntax error on line 1).
Please post sample before and after data and explain the process in more detail.

Please post what Operating System and version you are running and what Shell this is.

Do you mean "append" or "prepend"?



For your "Another question", please open a new post and take note of the questions above before posting. What if you had to pay say £100,000 up-front to get the code written and had no contact with the programmers until after the program was written, what would you write in your Program Specification?
This User Gave Thanks to methyl For This Post:
# 3  
Old 06-15-2012
bash

Hi,

Try this one,
Code:
cd path
for file in *
do
   awk 'BEGIN{FS=":";}{$2="chr"$2;a[NR]=$0;}END{for(i=1;i<=NR;i  ){print a[i] >FILENAME;}}' ${file}
done

this code snippet will update the changes in original file. If you want to create a new file then redirect to new file name..
Please post sample data else we wont do blindly... Anyway here my assumptions are,
1. field separator is :
2. chr string prefix or suffix, here i prefixed.
Cheers,
Ranga:-)
This User Gave Thanks to rangarasan For This Post:
# 4  
Old 06-15-2012
Hi Methyl and Ranga,

I`m sorry for the inconvenience and many thanks for the help.

I`m using Red Hat Enterprise Linux 6 unix bash, thats all i know.

I have included the input and output this time. I need to do this with all files in the directory.

Input

Code:
Bak-2   1       711     T       C       38      6       1       1.28571
Bak-2   1       892     T       G       38      5       1       1
Bak-2   1       3279    G       T       30      3       1       1

Expected output

Code:
Bak-2   chr1       711     T       C       38      6       1       1.28571
Bak-2   chr1       892     T       G       38      5       1       1
Bak-2   chr1       3279    G       T       30      3       1       1

Ranga,

I ran your code, it is doing an infinite loop the printing line1 for file1 repeatedly.
Also I changed the delimiter to tabbed and used BEGIN{FS="\t";} in your code.

Output from Ranga`s code

Code:
Bak-2 chr1 711 T C 38 6 1 1.28571
Bak-2 chr1 711 T C 38 6 1 1.28571
Bak-2 chr1 711 T C 38 6 1 1.28571
Bak-2 chr1 711 T C 38 6 1 1.28571

Please help
# 5  
Old 06-15-2012
You need to set the output delimiter to TAB as well (FS=OFS="\t")
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 06-15-2012
Hi Scrutinizer,

I used this code


Code:
for file in *
do
   awk 'BEGIN{FS=OFS="\t"}{$2="chr"$2;a[NR]=$0;}END{for(i=1;i<=NR;i  ){print a[i] >FILENAME;}}' ${file}
done

It still gets into an infinite loop printing line1
# 7  
Old 06-15-2012
awk

Loop increment is missing there, so that the infinite loop occurs. I posted through mobile, something went wrong... Sorry for the inconvenience:-)
change the code as suggested by Scrutinizer... It will do the rest...
Happy weekend!
Cheers,
Ranga:-)
This User Gave Thanks to rangarasan For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Append pipe | at the end of all the rows except header n trailer for all the files under a directory

Hi Experts Need help... I am looking for a Unix script to append pipe | at the end of all the rows (except header and trailer)in all the files placed under the directory /interfaces/Temp e.g. Header row1 row2 row3 Trailer The script should read all the files under... (3 Replies)
Discussion started by: phani333
3 Replies

2. Shell Programming and Scripting

Append a specific keyword in a text file into a new column

All, I have some sample text file(.csv) in the below format. In my actual file there are at least 100K rows. date 03/25/2016 A,B,C D,E,F date 03/26/2016 1,2,3 4,5,6 date 03/27/2016 6,4,3 4,5,6 I require the following output where in the date appeared at different locations need to... (3 Replies)
Discussion started by: ks_reddy
3 Replies

3. UNIX for Dummies Questions & Answers

Append two lines of text to php.ini in the entire directory tree.e

I am looking to write a script that will read the php.ini files on my web host. If the two lines do exist do nothing. If not append two lines to the end of it then move on to the next directory and open the next php.ini file. I have the beginning of one that was given to me on another web site but... (6 Replies)
Discussion started by: Larrykh465
6 Replies

4. Shell Programming and Scripting

Append string to all the files inside a directory excluding subdirectories and .zip files

Hii, Could someone help me to append string to the starting of all the filenames inside a directory but it should exclude .zip files and subdirectories. Eg. file1: test1.log file2: test2.log file3 test.zip After running the script file1: string_test1.log file2: string_test2.log file3:... (4 Replies)
Discussion started by: Ravi Kishore
4 Replies

5. UNIX for Dummies Questions & Answers

Append text to files with a same pattern

Hi Folks, I wanted to know if i can use RegEx in a for-loop of a shell script. Here's a scenario, I have a set of files say x1, x2, x3..x9 in a directory(obviously with files other than this pattern). I want to append a line of text to all files that follow pattern x. Can someone help me out? I... (4 Replies)
Discussion started by: prithvirao17
4 Replies

6. Shell Programming and Scripting

Compare a common field in two files and append a column from File 1 in File2

Hi Friends, I am new to Shell Scripting and need your help in the below situation. - I have two files (File 1 and File 2) and the contents of the files are mentioned below. - "Application handle" is the common field in both the files. (NOTE :- PLEASE REFER TO THE ATTACHMENT "Compare files... (2 Replies)
Discussion started by: Santoshbn
2 Replies

7. Shell Programming and Scripting

Append variable texts to the beginning of each line in all files in a directory

I am writing a code to append some numbers in the beginning of each line in all the files present in a directory. The number of files are really huge. The files are numbered as 1.sco, 2.sco, 4.sco (Note: 3.sco is missing). The files currently look like this: 1.sco 2 3 5 6 6 7My task is to... (4 Replies)
Discussion started by: shoaibjameel123
4 Replies

8. UNIX for Dummies Questions & Answers

Append a line to single column text file

I would like to add a line to the end of a single column text file. How do I go about doing that? Input: BEGIN 1 2 3 Output: BEGIN 1 2 3 END Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

9. Shell Programming and Scripting

How to read and append certain files form directory

Hi ,i have a question ,if I am in sertain directory and I have 4 files *.c how can I read and append this files in file backup.bac Thanks find ./ -name "*.csh" | wc -l (2 Replies)
Discussion started by: lio123
2 Replies

10. Shell Programming and Scripting

search directory-find files-append at end of line

Hi, I have a command "get_data" with some parameters in few *.text files of a directory. I want to first find those files that contain this command and then append the following parameter to the end of the command. example of an entry in the file :- get_data -x -m50 /etc/web/getid this... (1 Reply)
Discussion started by: PrasannaKS
1 Replies
Login or Register to Ask a Question