The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Read value from particular position in file. krishnarao Shell Programming and Scripting 2 05-15-2008 03:49 AM
How to check a word position in a file ? tibo Shell Programming and Scripting 7 01-03-2008 11:20 PM
Sorting a flat file based on multiple colums(using character position) cucubird Shell Programming and Scripting 8 07-24-2006 09:47 PM
find the position in a file and insert the data there isingh786 HP-UX 5 04-11-2006 06:31 PM
How to insert strings at certain position whatisthis Shell Programming and Scripting 14 11-14-2005 03:40 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 08-07-2007
Registered User
 

Join Date: Aug 2007
Posts: 8
How to insert at a particular position in flat file

Hi All,
I have a flat file with ~ as de-limiter (e.g: aaa~ba a~caa~0~d~e)
What I want is check if the 4th character is 0 and replace it with say 4. So now it becomes : aaa~ba a~caa~4~d~e.

I have to do this for the whole file, but the delimiter position remains the same, not the character length.

Please help.
Abhi.
Reply With Quote
Forum Sponsor
  #2  
Old 08-07-2007
dj -------
 

Join Date: Feb 2007
Location: Cochin/Bangalore, India
Posts: 435
Try this..

Code:
awk -F"~" '{if ($4==0) $4=4; print $0;}' filename
Reply With Quote
  #3  
Old 08-07-2007
Klashxx's Avatar
HP-UX/Linux/Oracle
 

Join Date: Feb 2006
Location: Almerķa, Spain
Posts: 383
To start:
Code:
pattern=$1
rep_string=$2
awk -F\~ -v pat=${pattern} -v rep=${rep_string} 'BEGIN{OFS=FS}{if ( $4 == pat){sub(/'"${pattern}"'/,rep,$4)}print}' txt

Last edited by Klashxx; 08-07-2007 at 01:59 AM.
Reply With Quote
  #4  
Old 08-07-2007
Registered User
 

Join Date: Jul 2007
Posts: 5
cat filename | cut -d "~" -f 4|sed -e 's/0/4/'
Reply With Quote
  #5  
Old 08-07-2007
Registered User
 

Join Date: May 2007
Posts: 211
Code:
awk -F'~' 'BEGIN{OFS="~";}{ if ($4 != 4){$4=4;} print }' your_file
As per the req it should have been like

Code:
awk -F'~' 'BEGIN{OFS="~";}{ if ($4 == 0){$4=4;} print }' your_file

Last edited by lorcan; 08-07-2007 at 02:06 AM. Reason: Changing as per req
Reply With Quote
  #6  
Old 08-07-2007
Registered User
 

Join Date: Aug 2007
Posts: 8
Quote:
Originally Posted by jacoden View Post
Try this..

Code:
awk -F"~" '{if ($4==0) $4=4; print $0;}' filename
Hi Jacoden

I tried it , but i have lost my de-limiters '~' and got only space instead.

Abhi.
Reply With Quote
  #7  
Old 08-07-2007
Registered User
 

Join Date: Aug 2007
Posts: 8
Quote:
Originally Posted by lorcan View Post
Code:
awk -F'~' 'BEGIN{OFS="~";}{ if ($4 != 4){$4=4;} print }' your_file
Thanks a lot this works.

Abhi.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 09:09 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0