The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
oneliner:sing SED on a specific column chaseeem Shell Programming and Scripting 8 07-15-2008 06:10 AM
How to extract first column with a specific character selamba_warrior Shell Programming and Scripting 3 05-22-2008 05:14 AM
How to read a specific column into variable victorcheung Shell Programming and Scripting 2 04-18-2008 01:49 AM
(cont) Retrieve line from a file based on a value in specific column efernandes UNIX for Dummies Questions & Answers 0 01-27-2007 01:00 PM
Retrieve line from a file based on a value in specific column efernandes UNIX for Dummies Questions & Answers 1 01-27-2007 11:04 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 08-25-2008
MomoChan MomoChan is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 7
subtitute specific column in line

Hi All,
I have problem to solve
aaaa,aaaaa,aaa,aaaa,aaa,aa ,aa
bbbb,bbbbbbbbb,bbbb,bbbbb ,bb

to
aaaa;aaaaa,aaa ;aaaa;aaa,aa ;aa
bbbb;bbbbbbbbb;bbbb;bbbbb ;bb

i try use sed to find and replace, but dont know how to replace specific column position.

can u help me??

thx for the comment.
  #2 (permalink)  
Old 08-25-2008
redoubtable redoubtable is offline
Registered User
  
 

Join Date: Aug 2008
Location: Portugal
Posts: 242
echo "aaaa;aaaaa;aaa ;aaaa;aaa;aa ;aa"|awk -F ";" '{$2="bbbbbbbb"; printf "%s;%s;%s;%s;%s\n", $1,$2,$3,$4,$5}'

awk -F sets the delimiter for columns and then each column is represented by $1 $2 $3 $4 $5. Change them as you see fit.
  #3 (permalink)  
Old 08-25-2008
MomoChan MomoChan is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 7
what i'm concern is, replace all commas delimiter to semicolon except several column
e.g
i have a file contains two lines:
aaaa,aaa,aa,aaaa,aaaa aa
cccc,ccccc ,cccc,cccc,cc

and i want change it into
aaaa;aaa,aa;aaaa;aaaa aa
cccc;ccccc ;cccc;cccc,cc

*sorry my english not well enough

Last edited by MomoChan; 08-25-2008 at 06:00 AM..
  #4 (permalink)  
Old 08-25-2008
redoubtable redoubtable is offline
Registered User
  
 

Join Date: Aug 2008
Location: Portugal
Posts: 242
echo "aaaa,aaa,aa,aaaa,aaaa aa"|sed -e 's/a/c/g' | awk -F "," '{printf "%s;%s%s;%s;%s,%s\n", $1,$2,$3,$4,substr($5,0,4),substr($5,6,2)}'

I hope you can understand the code. Like I said with the code I already had gave you it was possible to change any field with appropriate tweaking.
  #5 (permalink)  
Old 08-25-2008
MomoChan MomoChan is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 7
Quote:
Originally Posted by redoubtable View Post
echo "aaaa,aaa,aa,aaaa,aaaa aa"|sed -e 's/a/c/g' | awk -F "," '{printf "%s;%s%s;%s;%s,%s\n", $1,$2,$3,$4,substr($5,0,4),substr($5,6,2)}'

I hope you can understand the code. Like I said with the code I already had gave you it was possible to change any field with appropriate tweaking.
not change the 'a' letter to 'c' like u mention above,.
and i modify to
echo "aaaa,aaa,aa,aaaa,aaaa aa" | awk -F "," '{printf "%s;%s%s;%s;%s,%s\n", $1,$2,$3,$4,substr($5,0,4),substr($5,6,2)}'

from ur code, it will
change
aaaa,aaa,aa,aaaa,aaaa aa
into
aaaa;aaaaa;aaaa;aaaa,aa

the result line length reduced,... it should be
aaaa;aaa,aa;aaaa;aaaa aa
  #6 (permalink)  
Old 08-25-2008
redoubtable redoubtable is offline
Registered User
  
 

Join Date: Aug 2008
Location: Portugal
Posts: 242
Just add a space in printf format strings:

echo "aaaa,aaa,aa,aaaa,aaaa aa" | awk -F "," '{printf "%s;%s%s ;%s;%s,%s\n", $1,$2,$3,$4,substr($5,0,4),substr($5,6,2)}'
  #7 (permalink)  
Old 08-25-2008
MomoChan MomoChan is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 7
in my input sample
aaaa,aaa,aa,aaaa,aaaa aa

i just want to change commas in position 5,12,17 into semicolon..
no change in data

i try to make an array and make a script, see below

set -A dlm -- 5 12 17
echo ${#dlm[*]}
inc=0
echo "cat FILENAME | sed \ " > ascript
while [ $inc -lt ${#dlm[*]} ] ; do
cmd=" -e '/^\(.\{${dlm[$inc]}\}\),\(.*\)/ s//\1\;\2/g' \ "
echo $cmd >> ascript
let inc=inc+1
done
chmod 775 ascript
ascript


but still problem occurs,
is there somtin wrong with my script ??

Last edited by MomoChan; 08-25-2008 at 08:56 AM..
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 01:39 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