subtitute specific column in line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting subtitute specific column in line
# 1  
Old 08-25-2008
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  
Old 08-25-2008
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  
Old 08-25-2008
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 07:00 AM..
# 4  
Old 08-25-2008
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  
Old 08-25-2008
Quote:
Originally Posted by redoubtable
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  
Old 08-25-2008
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  
Old 08-25-2008
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 09:56 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Overwrite specific column in xml file with the specific column from adjacent line

I have an xml file dumped from rrd file, that I want to "patch" so the xml file doesn't contain any blank hole in the resulting graph of the rrd file. Here is the file. <!-- 2015-10-12 14:00:00 WIB / 1444633200 --> <row><v> 4.0419731265e+07 </v><v> 4.5045912770e+06... (2 Replies)
Discussion started by: rk4k
2 Replies

2. Shell Programming and Scripting

Split certain strings in a line for a specific column.

Hi, i need help to extract certain strings/words from lines with different length. I have 3 columns separated by tab delimiter. like below Probable arabinan endo-1,5-alpha-L-arabinosidase A (EC 3.2.1.99) (Endo-1,5-alpha-L-arabinanase A) (ABN A) abnA Ady3G14620 Probable arabinan... (5 Replies)
Discussion started by: redse171
5 Replies

3. Shell Programming and Scripting

Help with print out line that have different record in specific column

Input file 1: - 7367 8198 - 8225 9383 + 9570 10353 Input file 2: - 2917 3667 - 3851 4250 + 4517 6302 + 6302 6740 + 6768 7524 + 7648 8170 + 8272 8896 + 8908 9915 - 10010 ... (18 Replies)
Discussion started by: perl_beginner
18 Replies

4. Shell Programming and Scripting

How to read particular line in file from specific column?

Hi...friends.... I want to create inventory...information for that I need to read some specific row say 2nd row from 1st 3 column and and write data with particular file used, I have some more column also but I need only 3 column data of first entry after header I attached sample file..those... (12 Replies)
Discussion started by: nex_asp
12 Replies

5. Shell Programming and Scripting

Counting rows line by line from a specific column using Awk

Dear UNIX community, I would like to to count characters from a specific row and have them displayed line-by-line. I have a file called testAwk2.csv which contain the following data: rabbit penguin goat giraffe emu ostrich I would like to count in the middle row individually... (4 Replies)
Discussion started by: vnayak
4 Replies

6. Shell Programming and Scripting

How to read the value from a specific line and column BASH

Hi All, I have the same problem as the one posted in https://www.unix.com/shell-programming-scripting/96097-how-read-value-specific-line-column-csh-variable.html but I'm using bash. Can anyone tell me how I have to modify the code to make it bash compatible? eval `awk 'NR==3{print "set... (5 Replies)
Discussion started by: f_o_555
5 Replies

7. Shell Programming and Scripting

subtitute value of certain raw and column with sed

Dear All, For example the content of data.txt file is: 1 1 23 2 1 42 3 2 52 4 2 62 5 1 77 6 1 88 7 2 99 8 1 100 Could I substitute 2 in second column with 1 using sed commad so that the data will be change as follow ? 1 1 23 2 1 42 ... (1 Reply)
Discussion started by: ariesto
1 Replies

8. UNIX for Dummies Questions & Answers

replacing string in a column on a specific line

hi, i currently have a file with columns similar to this customer name owed CID123 John 300 CID342 harry 500 at present i use use awk to find the amount owed by the customer using the customer ID (CID). if the customer spends more money how would i go about using sed/awk etc to... (2 Replies)
Discussion started by: skinnygav
2 Replies

9. Shell Programming and Scripting

How to read the value from a specific line and column in to a csh variable

Hi All, Although its a basic question the last 2 hours of googling and trying didnt help me to achieve what i want. Maybe some one can help me I have a text file text1.txt: blablablabla A B C D E F and i would like to read to read what is on position E (line 3 column 2) in a... (2 Replies)
Discussion started by: Radamez
2 Replies

10. UNIX for Dummies Questions & Answers

Retrieve line from a file based on a value in specific column

Hi, I have a file that has several values seperated by ":" 2006:John:Student:Football:Portugal:Cinema 2006:James:Engineer:Basket:Poland:Theatre 2007:Lucy:Diver:Gymnastic:England:Music 2007:Smith:Plumber:Basket:Spain:Poker I need make a filter based on the 5th field to find countries that... (1 Reply)
Discussion started by: efernandes
1 Replies
Login or Register to Ask a Question