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 and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Escaping specific character in awk brainyoung Shell Programming and Scripting 8 12-16-2008 11:14 PM
How to extract first column with a specific character selamba_warrior Shell Programming and Scripting 3 05-22-2008 06:14 AM
count occurences of specific character in the file superprogrammer HP-UX 9 04-09-2008 12:05 PM
replace character in a string pattern and save the change in same file mihir0011 Shell Programming and Scripting 2 09-26-2007 06:31 PM
How to add character in specific position of a string? victorlung Shell Programming and Scripting 5 09-01-2006 11:33 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-21-2008
sdubey sdubey is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 6
How to change a specific character in a file

Hi,

I have a data file with following structure:

a|b|c|d|3|f1|f2|f3
a|b|c|d|5|f1|f2|f3|f4|f5

I want to change this data to:

a|b|c|d|3|f1;f2;f3
a|b|c|d|5|f1;f2;f3;f4;f5

Data in column 5 tells the number of following fields. All fields delimiter after the 5th column needs to be changed to ";"

Please help how can this be done in any scripting language sed, awk or perl?

Regards

Sandeep
  #2 (permalink)  
Old 02-21-2008
bobbygsk bobbygsk is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 90
File
==
a|b|c|d|3|f1|f2|f3
a|b|c|d|5|f1|f2|f3|f4|f5
Quote:
sed -e "s/|f/;f/g" -e "s/;f1/|f1/g" file
  #3 (permalink)  
Old 02-21-2008
jim mcnamara jim mcnamara is online now Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,804

Code:
awk -F'|' ' {OFS="|";
             six="";
             for(i=6;i<=NF;i++)
                { six=six $i ";" }
             print $1,$2,$3,$4,$5,six
           }' filename

input & output:
Quote:
/home/jmcnama> cat filename
a|b|c|d|3|f1|f2|f3
a|b|c|d|5|f1|f2|f3|f4|f5
/home/jmcnama> t.sh
a|b|c|d|3|f1;f2;f3;
a|b|c|d|5|f1;f2;f3;f4;f5;
  #4 (permalink)  
Old 02-21-2008
sdubey sdubey is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 6
Thanks Jim. It helped me in getting started. Just one followup: If the file is as:

a|b|c|d|3|f;;1;|f2;;0;|f3;;1;
a|b|c|d|5|m;;1;|x;;1;|f3;;0;|f4;;1;|f5;;1;

how can I change to

a|b|c|d|3|f;f2;f3;
a|b|c|d|5|m;x;f3;f4;f5;

Basically I need to starting from 6th column to the end of line, delete ";1;|" or ";0;|"

Thanks again for your help.

Regards

Sandeep
  #5 (permalink)  
Old 02-21-2008
sb008 sb008 is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2007
Posts: 384

Code:
sed -e "s/;[^|][^|]*//g" -e "s/|/;/g" -e "s/\([^;]*\);\([^;]*\);\([^;]*\);\([^;]*\);\([^;]*\);/\1|\2|\3|\4|\5|/" <file>


Last edited by sb008; 02-21-2008 at 04:48 PM..
  #6 (permalink)  
Old 02-21-2008
hanny hanny is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 3
A shorter one:


Code:
sed -e 's/;[0-9].//g' -e 's/;|/;/g'

Cheers
  #7 (permalink)  
Old 02-22-2008
sb008 sb008 is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2007
Posts: 384
Quote:
Originally Posted by hanny View Post
A shorter one:


Code:
sed -e 's/;[0-9].//g' -e 's/;|/;/g'

Cheers
Compliments, smart use of the double ";"
Closed Thread

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 On




All times are GMT -4. The time now is 05:15 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