The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
grep unix.com with google



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Reply
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-28-2009
Registered User
 

Join Date: Nov 2009
Posts: 7
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 (permalink)  
Old 11-29-2009
dj - the student
 

Join Date: Feb 2007
Location: Singapore/Bangalore/Cochin
Posts: 667
Try:


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

  #3 (permalink)  
Old 11-29-2009
Registered User
 

Join Date: Nov 2009
Posts: 7
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 (permalink)  
Old 11-29-2009
Moderator
 

Join Date: Feb 2007
Location: The Netherlands
Posts: 4,670
Try this, be aware of the quotes around the variable:


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

  #5 (permalink)  
Old 11-29-2009
Registered User
 

Join Date: Nov 2009
Posts: 7
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 (permalink)  
Old 11-30-2009
Moderator
 

Join Date: Feb 2007
Location: The Netherlands
Posts: 4,670
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 (permalink)  
Old 12-08-2009
Registered User
 

Join Date: Dec 2009
Posts: 1
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
Sponsored Links
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help parsing and replacing text with file name mycoguy UNIX for Dummies Questions & Answers 2 08-07-2009 02:05 PM
replacing text in a file, but... Angelseph Shell Programming and Scripting 2 12-06-2008 12:46 AM
Replacing Text in Text file cgilchrist Shell Programming and Scripting 3 06-30-2008 11:32 PM
Replacing column with column of another file manneni prakash UNIX for Advanced & Expert Users 1 06-25-2008 12:27 AM
Replacing column with column of another file manneni prakash UNIX for Dummies Questions & Answers 1 06-25-2008 12:20 AM



All times are GMT -4. The time now is 12:31 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0