Change value in a column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change value in a column
# 1  
Old 03-10-2011
Change value in a column

Hello!
Please could anyone help me in writing a code to modify the value in column
two of a file with more than two tab-separated column (but the lines
may have a different number of columns, i.e. more or less columns),
according to the following conditions.

When the value of column two is "V", look in the whole field (string)
present in column three of that line to find if is there any of this
strings: IMP or VFIN or INF or SBJN or COND:ki. If one of these ir
found there, its value should be put in column two (instead of "V",
which was there).

Moreover, if in column three the string REL is present also, it should
be printed too, like this ":rel"at the end of the new value of column
two.

Look to the sample here below:

Code:
tolewa V 7/8-SG-SP VFIN PAST 7/8-SG-REL SVO EXT: APPL PASS :EXT"
trans="put out , remove" kilichotolewa
mwaka N 3/4-PL miaka
, COMMA ,
jenga V INF SVO kuijenga
anguka V 9/10-SG-SP COND:ki" trans=break down" ikianguka
 
to obtain:

tolewa VFIN:rel 7/8-SG-SP VFIN PAST 7/8-SG-REL SVO EXT: APPL PASS
:EXT" trans="put out , remove" kilichotolewa
mwaka N 3/4-PL miaka
, COMMA ,
jenga INF INF SVO kuijenga
anguka COND:ki 9/10-SG-SP COND:ki" trans=break down" ikianguka

Thank you very much!
mjomba
# 2  
Old 03-10-2011
You need gawk:

Code:
awk '/ V / {$0=gensub(/(.+) (V) (.*)(VFIN|INF|COND:ki|IMP|SBJN)(.+)/, "\\1 \\4 \\3 \\4\\5", "g")} 
             {if ($0~/REL/) $2=$2 ":rel" }1' infile

# 3  
Old 03-16-2011
Dear rdcwayx,
thank you for the code you wrote!

I slightly changed it as follows, and tried it on CygWin, but I got a message of error which I don't understand. Could you help me?
Thank you again!
Mjomba
Code:
Name@user ~/data
$ ./xml2tab.sh
./xml2tab.sh: line 44: syntax error near unexpected token `('
./xml2tab.sh: line 44: `      awk '/\tV\t/ {$0=gensub(/(.+)\t(V)\t(.*)(VFIN|SBJN
|INF |COND:ki|IMP|PRO)(.+)/, "\\1 \\4 \\3 \\4\\5", "g")}{if ($0~/REL/) $2=$2 ":r
el" }1' HCS_all.tab > HCS_all.tab.pul'

Name@user/data


Last edited by pludi; 03-16-2011 at 06:12 AM..
# 4  
Old 03-16-2011
Code:
`      awk '/\tV\t/ {$0=gensub(/(.+)\t(V)\t(.*)(VFIN|SBJN
|INF |COND:ki|IMP|PRO)(.+)/, "\\1 \\4 \\3 \\4\\5", "g")}{if ($0~/REL/) $2=$2 ":r
el" }1' HCS_all.tab > HCS_all.tab.pul'

Seems the command ` is not in pair of.
# 5  
Old 03-23-2011
Dear rdcwayx.
Could you explain, please, the meaning of the number which follow \\ after gensub ?
Thank you very much. Mjomba
# 6  
Old 03-23-2011
Here is the explain on how to use gensub function in gawk.

String Functions - The GNU Awk User's Guide

gensub(regexp, replacement, how [, target]) #

gensub is a general substitution function. Like sub and gsub, it searches the target string target for matches of the regular expression regexp. Unlike sub and gsub, the modified string is returned as the result of the function and the original target string is not changed. If how is a string beginning with ‘g’ or ‘G’, then it replaces all matches of regexp with replacement. Otherwise, how is treated as a number that indicates which match of regexp to replace. If no target is supplied, $0 is used.
gensub provides an additional feature that is not available in sub or gsub: the ability to specify components of a regexp in the replacement text. This is done by using parentheses in the regexp to mark the components and then specifying ‘\N’ in the replacement text, where N is a digit from 1 to 9. For example:

$ gawk '
> BEGIN {
> a = "abc def"
> b = gensub(/(.+) (.+)/, "\\2 \\1", "g", a)
> print b
> }'
-| def abc
As with sub, you must type two backslashes in order to get one into the string. In the replacement text, the sequence ‘\0’ represents the entire matched text, as does the character ‘&’.

The following example shows how you can use the third argument to control which match of the regexp should be changed:

$ echo a b c a b c |
> gawk '{ print gensub(/a/, "AA", 2) }'
-| a b c AA b c
In this case, $0 is used as the default target string. gensub returns the new string as its result, which is passed directly to print for printing.

If the how argument is a string that does not begin with ‘g’ or ‘G’, or if it is a number that is less than or equal to zero, only one substitution is performed. If how is zero, gawk issues a warning message.

If regexp does not match target, gensub's return value is the original unchanged value of target.

gensub is a gawk extension; it is not available in compatibility mode (see Options).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Change 10th column formatting.

Hi Experts, I created a powershell script to fetch user details from active directory and export it to a csv/excel. "abc","bcd","devloper;admin" "bcd","dca","tester;QA;admin" So here user abc is member of devloper and admin group and user bcd is a member of tester,QA and admin... (6 Replies)
Discussion started by: shekhar_4_u
6 Replies

2. UNIX for Beginners Questions & Answers

Duplicate and change a column

Hi, I have a file with list of items: a b c I would like to run a 1-liner (awk/perl) to duplicate and change the value of the existing column, i.e.: a a b b c c I can duplicate with awk: awk '{print $1 " " $1 }' but couldn't figure out how to do the character change, your help is... (5 Replies)
Discussion started by: yan1
5 Replies

3. Shell Programming and Scripting

Change Data base on Column

Base of last two column i want to change may data if Last two Column have A and C then Copy Column $4 to Column $3. Input :- DD142 0_1 DD142_A DD142_B A B DD142 1_1 DD142_B DD142_C B C DD142 2_1 DD142_A DD142_C A C DD142 3_1 DD142_A A DD142 3_2 DD142_A A DD142 4_1 DD142_B B ... (4 Replies)
Discussion started by: pareshkp
4 Replies

4. Shell Programming and Scripting

Change Lines into column

Hello how can I change lines into column? e.g. I have a Text file which looks like: Surename...... Address..... Phonenumber..... Zipcode.. Surename... Address.... Zipcode Surename... I want that Surename is in column 1 and every following line should be in additional colums.... (5 Replies)
Discussion started by: witchblade
5 Replies

5. Shell Programming and Scripting

Change column to row base on column 2

Hi Guys, Input.txt L194 A -118.2 L194 B -115.1 L194 C -118.7 L196 A 0 L196 C 0 L197 A -111.2 L197 B -118.9 L197 C -119.9 L199 A -120.4 L199 B -119.9 ... (2 Replies)
Discussion started by: asavaliya
2 Replies

6. Shell Programming and Scripting

awk or sed: change the color of a column w/o screwing up column spacing

Hey folks. I wrote a little awk script that summarizes /proc/net/dev info and then pipes it to the nix column command to set up column spacing appropriately. Here's some example output: Iface RxMBytes RxPackets RxErrs RxDrop TxMBytes TxPackets TxErrs TxDrop bond0 9 83830... (3 Replies)
Discussion started by: ryran
3 Replies

7. Shell Programming and Scripting

Change file content 4 column to one Column using script

Hi Gurus, I have file content sample: ,5113955056,,TAgent-Suspend ,5119418233,,TAgent-Suspend ,5102119078,,TAgent-Suspend filenames 120229H5_suspend, 120229H6_unsuspend I receive those files one of directory /home/temp/ I need following: 1. Backup first /home/temp/ file to... (5 Replies)
Discussion started by: thepurple
5 Replies

8. Shell Programming and Scripting

Change names in a column based on the symbols in another column

If the 4th column has - sign then the names in 3rd column has to change to some user defined names (as shown in output). Thanx input1 1 a aaaaa + 2 b bbbbb + 3 c ccccc + 4 d ddddd + 5 e eeeee + 6 f xxxxx + 8 h hhhhh +... (8 Replies)
Discussion started by: repinementer
8 Replies

9. Shell Programming and Scripting

State Change count in the column

Dear all, I have file like below shown. In that file i need to identify how many state change in the column "type".For example 94713357873 goes from type B --> 1 i need to count it.But when we count the x number should be uniq, cant get the count B-->1 state chage for different x numbers. so... (2 Replies)
Discussion started by: Nayanajith
2 Replies

10. UNIX for Dummies Questions & Answers

Change one column using sed??

Is there way to use sed to change only one occurence in a colon separated line? I would like to change a file from *:*:rex:rex *:*:simon:rex to *:*:rex:mabry *:*:simon:rex (2 Replies)
Discussion started by: rexmabry
2 Replies
Login or Register to Ask a Question