Replacing a column in a text file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Replacing a column in a text file
# 1  
Old 11-28-2009
Replacing a column in a text file

Say I had a text file that contained four columns, like the following:

Mack Christopher:237 Avondale Blvd:970-791-6419:S
Ben Macdonor:30 Dragon Rd:647-288-6395:B

I'm making a loop that will replace the fourth column a line in the file with the contents of a variable 'access', but I have no idea how to do it. I've been trying to use awk, but I don't think I am doing it correctly. Does anyone have any ideas?
# 2  
Old 11-29-2009
Try:

Code:
awk '{ $NF="'$access'"; print; }'  file

# 3  
Old 11-29-2009
I tried running that code and it didn't work for me. the error has something to do with my access variable, which in this case is "Student Access":

awk: { $NF="Student
awk: ^ unterminated string
# 4  
Old 11-29-2009
Try this, be aware of the quotes around the variable:

Code:
awk -v var="$access" '{$NF=var}1' file

# 5  
Old 11-29-2009
Sorry for forgetting to mention that my columns were separated by a colon. So I took the code that you gave me and added an -F and this is what I have so far:

Code:
 awk -F ":" -v var="$access" '{$NF=var}1' file

Two things that I am still having trouble with though.

1) What does the 1 inside the single quotes do as part of awk (I'm trying to understand for future uses).

2) The output for the awk command looks like this"

Mack Christopher 237 Avondale Blvd 970-791-6419 Student Access

Is there a way to keep the colons?
# 6  
Old 11-30-2009
Try this:

Code:
awk -F":" -v var="$access" '{$NF=var}1' OFS=":" file

The 1 means "true" to awk and the default action is to print the current line, similar to:

Code:
{print}

# 7  
Old 12-08-2009
inverser 2 colonnes dans un texte sous éditeur

On peut aussi permuter 2 colonnes sous vi, donc interactivement.
Dans ton exemple où le séparateur de colonne est ":"

:%s/\(.*\):\(.*\):\(.*\):\(.*\)/\1:\3:\2:\4/

signifie

: après [esc] indique une action sur le texte
% l'action porte sur l'ensemble du texte
s l'action est une substitution
/ début de description de la ligne
\(.*\) un groupe contenant plusieurs caractères quelconque
: sauf : qui sert de séparateur
/ fin de la ligne et commencement de la description
de la réécriture
\1 recopie le premier groupe
: écrit le nouveau séparateur, ici c'est le même
etc

Hope, it's help you

---------------------------------------------------------
(°> Dhénin Jean-Jacques
/ ) 48, rue de la Justice 78300 Poissy
^^ Jean-Jacques@dhenin.fr
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing a column in a pipe delimited file

Hi, I have a pipe delimited file as below and I need to replace the 2nd column of each line with null values. 1|10/15/2011|fname1|lname1 2|10/15/2012|fname2|lname2 3|10/15/2013|fname3|lname3 Output file: 1||fname1|lname1 2||fname2|lname2 3||fname3|lname3 I tried this ... (2 Replies)
Discussion started by: member2014
2 Replies

2. Shell Programming and Scripting

Replacing a column in a file with a value

Hi , Could anyone help me for the below request . Suppose i have the data like below : 00006139 2066522 DURTS S 0 20121219 20121219 20121120 00006137 2515401 DURTS S 0 20121219 20121219 20121120 00004363 6030777 DURTS S 0 20121219 20121219 ... (2 Replies)
Discussion started by: Ravi Tej
2 Replies

3. Shell Programming and Scripting

Awk: Need help replacing a specific column in a file by part of a column in another file

Hi, I have two input files as File1 : ABC:client1:project1 XYZ:client2-aa:project2 DEF:client4:proj File2 : client1:W-170:xx client2-aa:WT-04:yy client4:L-005A:zz Also, array of valid values can be hardcoded like Output : ABC:W:project1 XYZ:WT:project2 (1 Reply)
Discussion started by: aa2601
1 Replies

4. UNIX for Dummies Questions & Answers

Replacing a specific column of a text file with another column

Hi, I have a text file in the following format: Code: 13412 NA06985 0 0 2 46.6432798439 4 4 4 4 13412 NA06991 NA06993 NA06985 2 48.8478948517 4 4 2 4 13412 NA06993 0 0 1 45.8022601455 4 4 2 4 13401 NA06994 0 0 1 48.780669145 4 4 4 4 13401 NA07000 0 0 2 47.7312017846 2 4 4 4 ... (2 Replies)
Discussion started by: evelibertine
2 Replies

5. UNIX for Dummies Questions & Answers

Replacing a specific column of a text file with another column

I have a text file in the following format: 13412 NA06985 0 0 2 46.6432798439 4 4 4 4 13412 NA06991 NA06993 NA06985 2 48.8478948517 4 4 2 4 13412 NA06993 0 0 1 45.8022601455 4 4 2 4 13401 NA06994 0 0 1 48.780669145 4 4 4 4 13401 NA07000 0 0 2 47.7312017846 2 4 4 4 13402 NA07019... (3 Replies)
Discussion started by: evelibertine
3 Replies

6. Shell Programming and Scripting

Replacing a column in a file

Hi, I want to replace a column in a file with a file that only has one column. Reading another post this is what I'm trying to do, The existing file has n columns and I paste the extra column using the paste command to make it n+1 paste file1 file2 > file3 Firstly I want to now remove... (3 Replies)
Discussion started by: lost.identity
3 Replies

7. Shell Programming and Scripting

Replacing column with another column of different file by awk

Hi All, I will really appreciate if you kindly lookinto my requirement below and provide me a solution First file format test1.txt qq ww rr tt ee ff qq ww rr tt ee ff Second file format text2.txt aa aa Now o/p I want as text1.txt's 4th column replaced... (5 Replies)
Discussion started by: Pratik4891
5 Replies

8. Shell Programming and Scripting

replacing text in a file, but...

Hi all, Very first post on this forums, hope you can help me with this scripting task. I have a big text file with over 3000 lines, some of those lines contain some text that I need to replace, lets say for simplicity the text to be replaced in those lines is "aaa" and I need it to replace it... (2 Replies)
Discussion started by: Angelseph
2 Replies

9. UNIX for Advanced & Expert Users

Replacing column with column of another file

Hi, I have two input files. File1: ID Name Place 1-234~name1~Newyork 1-34~name2~Boston 1-2345~name3~Hungary File1 is a variable length file where each column is seperated by delimitter "~". File2: ID Country 1-34<<11 SPACES>>USA<<7 spaces>> 1-234<<10 SPACES>>UK<<8 spaces>> ... (1 Reply)
Discussion started by: manneni prakash
1 Replies

10. UNIX for Dummies Questions & Answers

Replacing column with column of another file

Hi, I have two input files. File1: ID Name Place 1-234~name1~Newyork 1-34~name2~Boston 1-2345~name3~Hungary File1 is a variable length file where each column is seperated by delimitter "~". File2: ID Country 1-34<<11 SPACES>>USA<<7 spaces>> 1-234<<10 SPACES>>UK<<8 spaces>> ... (1 Reply)
Discussion started by: manneni prakash
1 Replies
Login or Register to Ask a Question