Unix Script for replacing text in files

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unix Script for replacing text in files
# 1  
Old 08-27-2009
Unix Script for replacing text in files

Dear Experts,

I am new to unix shell programming. I need to write a script which needs to edit 4 text files.

File 1:- PCH_Test.txt
File 2:- PCT_Test.txt
File 3:- PCY_Test.txt
File 4:- PCW_Test.txt


First it needs to get into PCH_Test file and prefix the 6th coloumn with PCH i.e, replace 6th column with PCHXXX for all the records in the file.


My Desired Output:-

2009001,2009,XP0000AB20,XPA,GT01, PCHCSP,127.500
2009001,2009,XP0000AB20,XPD,GT04, PCHCSK,192.500
2009001,2009,XP0000AB20,XPE,GT13, PCHCSK,472.500
2009001,2009,XP0000AB20,XPF,GT16, PCHCSJ,787.500
2009001,2009,XP0000AB20,XPH,GT19, PCHSH,172.500

Like this it needs to get into PCT, PCY and PCW files and prefix the 6th column with corresponding names...like PCTXXX in PCT file, PCYXXX in PCY file and PCWXXX in PCW file....
# 2  
Old 08-27-2009
for file in *; do var=`echo $file | cut -c1-3`; perl -pi -e s/\,C/\,"$var"C/g $file; done

Last edited by cabrao; 08-27-2009 at 06:53 AM..
# 3  
Old 08-27-2009

awk -F, 'OFS=","{$6="PCH"$6}1' file

just replace PCH with whatever you like
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX script to append multiple text files into one file based on pattern present in filaname

Hi All-I am new to Unix , I need to write a script. Can someone help me with a requirement where I have list of files in a directory, I want to Merge the files if a pattern of string matches in filenames? AAAL_555A_ORANGE1_F190404.TXT AAAL_555A_ORANGE2_F190404.TXT AAAL_555A_ORANGE3_F190404.TXT... (6 Replies)
Discussion started by: Shankar455
6 Replies

2. Shell Programming and Scripting

How can i run sql queries from UNIX shell script and retrieve data into text docs of UNIX?

Please share the doc asap as very urgently required. (1 Reply)
Discussion started by: 24ajay
1 Replies

3. Shell Programming and Scripting

Finding a text in files & replacing it with unique strings

Hallo Everyone. I have to admit I'm shell scripting illiterate . I need to find certain strings in several text files and replace each of the string by unique & corresponding text. I prepared a csv file with 3 columns: <filename>;<old_pattern>;<new_pattern> ... (5 Replies)
Discussion started by: gordom
5 Replies

4. Shell Programming and Scripting

Script to create a text file whose content is the text of another files

Hello everyone, I work under Ubuntu 11.10 (c-shell) I need a script to create a new text file whose content is the text of another text files that are in the directory $DIRMAIL at this moment. I will show you an example: - On the one hand, there is a directory $DIRMAIL where there are... (1 Reply)
Discussion started by: tenteyu
1 Replies

5. Shell Programming and Scripting

Extended replacing of nonspecific strings in text files [beware complicated !]

Well, to make another post at this helpful forum :b::D: I recently tried something like this, I want to replace all those numberings/letters that are located between <string>file://localhost/var/mobile/Applications/ and /Documents/</string> numberings =---- replace with: first... (6 Replies)
Discussion started by: pasc
6 Replies

6. UNIX for Dummies Questions & Answers

Script for replacing text in a file based on list

Hi All, I am fairly new to the world of Unix, and I am looking for a way to replace a line of text in a file with a delimited array of values. I have an aliases file that is currently in use on our mail server that we are migrating off of. Until the migration is complete, the server must stay... (8 Replies)
Discussion started by: phoenixjc
8 Replies

7. Shell Programming and Scripting

Replacing text from multiple files at multiple location

Hi, I have many files scattered in all different folders. I want to replace the text within all the files using a single command ( awk, sed...) Is it possible? example find all the files in which there is text "memory" and replace it with "branded_memories". the files can be at the... (2 Replies)
Discussion started by: rudoraj
2 Replies

8. Shell Programming and Scripting

Simple 'sed' script for replacing text

Hi All, So I found a simple sed command to replace text in a file (http://www.labnol.org/internet/design/wordpress-unix-replace-text-multiple-files/1128/): sed -e 's/OLDtext/NEWtext/' -i file(s) Because I'm lazy and don't want to remember this each time I want to do this, I wrote the following... (4 Replies)
Discussion started by: ScKaSx
4 Replies

9. Shell Programming and Scripting

Replacing lines in text files

Hi, I have 2 sets of text files. I need to take a field from a certain line in set 1 and put it in the same place in set b. The line appears once per file, in different places but is a set format and has the unique word "ANTENNA" in it and is always 81 characters long. Example from set a: ... (7 Replies)
Discussion started by: Jonny2Vests
7 Replies

10. Shell Programming and Scripting

replacing contents of files from a bash script

I'm writing a shell script and I need to replace the contents of a configuration file based on what is passed to the script...can I replace expressions in a file from a bash shell script? (2 Replies)
Discussion started by: HumanBeanDip
2 Replies
Login or Register to Ask a Question